




























































































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Prepara i tuoi esami
Studia grazie alle numerose risorse presenti su Docsity
Prepara i tuoi esami con i documenti condivisi da studenti come te su Docsity
Trova i documenti specifici per gli esami della tua università
Preparati con lezioni e prove svolte basate sui programmi universitari!
Rispondi a reali domande d’esame e scopri la tua preparazione
Riassumi i tuoi documenti, fagli domande, convertili in quiz e mappe concettuali
Studia con prove svolte, tesine e consigli utili
Togliti ogni dubbio leggendo le risposte alle domande fatte da altri studenti come te
Esplora i documenti più scaricati per gli argomenti di studio più popolari
Ottieni i punti per scaricare
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Appunti sul linguaggio di programmazione C#
Tipologia: Dispense
1 / 122
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!





























































































In offerta
(fonti: www.tutorialspoint.com/csharp/csharp_tutorial.pdf )
using System; namespace HelloWorldApplication { class HelloWorld { static void Main(string[] args) { /* my first program in C# */ Console.WriteLine("Hello World"); Console.ReadKey(); } } }
import --> using
package --> namespace
poi se voglio aggiungere altre librerie (assembly) lo faccio nei riferimenti (reference) del progetto: clicco a destra sul progetto, e poi su "Aggiungi riferimento"
(il nome del file può essere diverso da quello della classe)
(la definizione di una classe può essere suddivisa fra file diversi: in questo caso si usa il modificatore partial )
Ho fatto il progetto con Visual Studio 2013 come Windows desktop | Console Application lo ha messo in: C:\Users\stefano\Documents\Visual Studio 2013\Projects\ConsoleApplication
Classi e identificatori di elementi pubblici (campi e metodi): PascalCase
elementi non pubblici (e variabili locali): camelCase
campi privati: _camelCase
costruttori: PascalCase (perché uguali a nome classi)
int num; num = Convert.ToInt32(Console.ReadLIne());
using System; struct Books { private string title; private string author; private string subject; private int book_id; public void getValues(string t, string a, string s, int id) { title = t; author = a; subject = s; book_id = id; } public void display() { Console.WriteLine("Title : {0}", title); Console.WriteLine("Author : {0}", author); Console.WriteLine("Subject : {0}", subject); Console.WriteLine("Book_id :{0}", book_id); } };
public class testStructure {
public static void Main(string[] args) { Books Book1 = new Books(); /* Declare Book1 of type Book / Books Book2 = new Books(); / Declare Book2 of type Book / / book 1 specification / Book1.getValues("C Programming", "Nuha Ali", "C Programming Tutorial", 6495407 ); / book 2 specification / Book2.getValues("Telecom Billing", "Zara Ali", "Telecom Billing Tutorial", 6495700 ); / print Book1 info / Book1.display(); / print Book2 info */ Book2.display(); Console.ReadKey(); } }
strutture: aggregati di dati non omogenei + metodi, ma sono statiche
Class vs Structure Classes and Structures have the following basic differences:
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
using System; namespace EnumApplication { class EnumProgram { enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat }; static void Main(string[] args) { int WeekdayStart = (int)Days.Mon; int WeekdayEnd = (int)Days.Fri;
Console.WriteLine("Monday: {0}", Days.Mon); Console.WriteLine("Monday: {0}", WeekdayStart); Console.WriteLine("Friday: {0}", WeekdayEnd); Console.ReadKe } } }
Output: Monday: Mon Friday: 1 Friday: 5
incapsulazione in Java: campi privati e metodi getter e setter pubblici
incapsulazione in C# campi privati e proprietà pubbliche
quando usate le proprietà vengono usate sintatticamente come fossero campi, ma in realtà sono dei metodi di accesso pubblici a proprietà private
using System; class Student { private string code = "N.A"; private string name = "not known"; private int age = 0 ; // Declare a Code property of type string: public string Code { get { return code; } set { code = value; } } // Declare a Name property of type string: public string Name { get { return name; } set
Output: Student Info: Code = 001, Name = Zara, Age = 9 Student Info: Code = 001, Name = Zara, Age = 10
get, set e value sono keyword (value una keyword contestuale)
se il metodo set non è implementato la proprietà è in sola lettura.
Esempio
class MyBaseClass { // virtual auto-implemented property. //Overrides can only provide specialized behavior // if they implement get and set accessors. public virtual string Name { get; set; }
// ordinary virtual property with backing field private int num; public virtual int Number { get { return num; } set { num = value; } } }
class MyDerivedClass : MyBaseClass { private string name;
// Override auto-implemented property with ordinary property // to provide specialized accessor behavior. public override string Name { get { return name;
set { if (value != String.Empty) { name = value; } else { name = "Unknown"; } } }
}
set { name = value; } } // Declare a Age property of type int: public override int Age { get { return age; } set { age = value; } } public override string ToString() { return "Code = " + Code +", Name = " + Name + ", Age = " + Age; } public static void Main() { // Create a new Student object: Student s = new Student(); // Setting code, name and the age of the student s.Code = "001"; s.Name = "Zara"; s.Age = 9 ; Console.WriteLine("Student Info:- {0}", s); //let us increase age s.Age += 1 ; Console.WriteLine("Student Info:- {0}", s); Console.ReadKey(); } }
Output:
Student Info: Code = 001, Name = Zara, Age = 9 Student Info: Code = 001, Name = Zara, Age = 10
passaggio per riferimento2:
public void swap(int x, int y, out int somma) { somma = x+y; }
nel Main int s n.swap(3, 4, out s);
N.B. nel metodo è necessario assegnare un valore a somma; nel Main non è necessario inizializzare s
public override int area(int base = 1, int altezza =2) { return (base * altezza / 2); } }
int a = area(3,4); int a = area();
int a = area(3);
int a = area(altezza:4); int a = area(altezza:4, base:3);
nullable: you can assign normal range of values as well as null values
using System; namespace CalculatorApplication { class NullablesAtShow { static void Main(string[] args) { int? num1 = null; int? num2 = 45 ; double? num3 = new double?(); double? num4 = 3.14157; bool? boolval = new bool?(); // display the values Console.WriteLine("Nullables at Show: {0}, {1}, {2}, {3}", num1, num2, num3, num4); Console.WriteLine("A Nullable boolean value: {0}", boolval); Console.ReadLine(); } } }
output: Nullables at Show: , 45, , 3. A Nullable boolean value:
foreach
using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[ 10 ]; /* n is an array of 10 integers / / initialize elements of array n / for ( int i = 0 ; i < 10 ; i++ ) { n[i] = i + 100 ; } / output each array element's value */ foreach (int j in n ) { int i = j- 100 ; Console.WriteLine("Element[{0}] = {1}", i, j); i++; } Console.ReadKey(); } } }
for (int i = 0; i < v.Length; i++) v[i] = i; for (int i = 0; i < v.Length; i++) Console.Write(v[i]+" "); Console.WriteLine(" ");
array multidimensionali
using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { /* an array with 5 rows and 2 columns/ int[,] a = new int[ 5 , 2 ] {{ 0 , 0 }, { 1 , 2 }, { 2 , 4 }, { 3 , 6 }, { 4 , 8 } }; int i, j; / output each array element's value */ for (i = 0 ; i < 5 ; i++) { for (j = 0 ; j < 2 ; j++) { Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]); } } Console.ReadKey(); } } }
invocato quando un oggetto sta per essere raccolto dalla garbage collection. Usato per liberare risorse usate dall'oggetto.
costruttore: NomeClasse
distruttore: ~NomeClasse