Kostur programa
using System;
class Program {
static void Main() {
}
}
Kreće od Main.
Ispis
Console.WriteLine("Zdravo");
Console.Write(x);
WriteLine prelazi u novi red.
Tipovi
int // ceo broj
double // decimalni
char // 'A'
string // "tekst"
bool // true/false
var = C# pogodi tip.
Unos
int n = int.Parse(
Console.ReadLine());
ReadLine vraća tekst → Parse.
Operatori
+ - * / %
7 / 2 = 3 // ceo deo
7 % 2 = 1 // ostatak
Za decimale koristi double.
Poređenja
== !=
< > <= >=
&& (i) || (ili)
== poredi, jedno = dodeljuje.
if / else
if (x >= 5) {
Console.WriteLine("ok");
} else {
Console.WriteLine("ne");
}
Uslov u zagradama ( ).
for petlja
for (int i = 0; i < 5; i++) {
Console.WriteLine(i);
}
početak ; uslov ; korak
while petlja
int i = 0;
while (i < 3) {
Console.WriteLine(i);
i++;
}
Menjaj i da se ne vrti zauvek.
Niz
int[] a = {5, 4, 3};
a[0]; // 5
a.Length; // 3
Length, indeksi od 0.
List
List<int> v =
new List<int>();
v.Add(5);
v.Count; // 1
Lista koja raste; .Count.
Metoda
static int Zbir(int a, int b) {
return a + b;
}
void = ne vraća ništa.
Klasa (OOP)
class Pas {
public string ime;
public void Laji() { }
}
Pas p = new Pas();
Nacrt → objekat sa new.