Sunteți pe pagina 1din 4

Limbajul C#:

(deocamdata facem aplicatia pt. PC pana ajungem la partea unde ne trebuie Emulatorul, adica la interfata cu utilizatorul)

Simboluri si Punctuatie:

Punctuatie: , ; :
Comentariu: //, /* continut */, ///, /** continut */
Directiva de preprocesor: #
Bloc de cod: { continut }
Generic: < continut >
Tipuri NULLe: ?
Caracter de scapare/evadare: \cod
Operatori: + - / * % < > ?? () s.a.m.d.

Intr-un limbaj natural, o expresie reprezinta un context de fraze si clauze ce sunt prezentate coerent si se termina folosind semnul
punct (.). In C# orice declaratie se termina cu semnul punct si virgula (;) si reprezinta o colectie de expresii si comenzi specifice
limbajului. C# este un limbaj free-form (forma libera) astfel ca nu conteaza daca intre x = 2 + si 1; exista spatii astfel ca e totuna:
x=2+

1;
e totuna cu x = 2 + 1;

Sintaxa punct (Dot syntax sau Dot punctuation) reprezinta relatia dintre menbrii. Ea se foloseste pt. a definii metodele (membrii)
unor tinte (clase sau obiecte, namespace-uri). Un exemplu este:
System.Windows.Forms.MessageBox.Show("A nice day!")

Comentariile se pot face folosind // - pt. comentarii pe un singur rand, /* */ - pt comentarii pe mai multe randuri si comentarii
speciale /// si /** */ folosite pt. generarea de cocumentatie ajutatoare pt. clasele create.

Directivele de preprocesor se marcheaza cu #: #comanda expresie


Lista de directive disponibile:
#define #undef
#if #else #elif #endif
#line #error #warning
#region #endregion #pragma
Sunt documentate in detaliu in books\Microsoft Press - Programming Microsoft Visual CSharp 2005 the language - jan.2006.chm

Blocul de cod se delimiteaza cu {} ca si in C, C++ si Java.

Genericele (Generics) sunt tipuri cu parametrii, iar parametrii reprezinta viitoare tipuri. Genericele au fost create pt. ca ele isi
pastreaza esenta. Este ca un fel de template pe baza caruia programatorul poate extinde continutul. Cum Array este un fel de
generic implementat, el este un tip, iar ca parametrii poate lua variabile de alte tipuri, deci un array (tablou) poate contine variabile
de tip Array sau String sau diverse tipuri proprii. Se marcheaza prin adaugarea de <T> dupa definirea clasei, a metodelor etc.
Mai multe detalii in cartea mentionata mai sus.

Tipuri NULLe sunt tipuri de variabile ce nu aloc nici un fel de date variabilei: valuetype? identifier;
Un exemplu: int? i; I va fi o variabila de tip intreg dar nu va contine date.
In versiunea 1.0 si 1.1 a limbajului C#, acest tip nu a existat. Insa a fost propus pt. inegrare in versiunea 2.2 si uitel! Normal orice
variabila primea o valoare aleatoare, insa cand trebuia sa se foloseasca cu alte tehnologii, de ex. cu baze de date unde e
implementat tipul NULL, nu exista o cale usoara pt. programa.

Caractere de scapare (Escape characters) sunt folosite pt. a introduce intr-un sir text un caracter special, ca linie noua spre ex. sau
pt. a introduce caractere Unicode sau Hex.
Apostrof (Single quote) \'
Ghilimele (Double quote) \"
Backlash \\
Null (byte-ul null) \0
Alert \a
Backspace \b
Form feed \f
New line \n // se foloseste pt. a crea o linie noua
Carriage return \r // se foloseste impreuna cu \n pt. a crea o linie noua specifica textului bogat pt. app. Windows
Horizontal tab \t
Unicode character \u
Vertical tab \v
Hexadecimal character(s) \x

Caracterul Verbatim se foloseste pt. a trata sirul text asa cum este. Spre exemplu daca avem sirul “C:\Program Files\” el trebuie
scris “C:\\Program Files\\” pt. ca \ reprezinta un caracter de scapare iar ultimul \ evadeaza ultima “ si da o eroare cum ca sirul nu a
fost inchis. Caracterul verbatim este @ si el se adauga ca prefix sirului: string s = @”C:\Program Files\”;.

Operatorii:
- Unari:
Unary Plus + variable=+5; 5
Unary minus - variable=-(-10); 10
Boolean Negation ! variable=!true; false
Bitwise 1's complement ~ variable=~((uint)1); 4294967294
Prefix Increment ++ ++ variable; 11
Prefix Decrement -- -- variable; 10
Postfix Increment ++ variable ++; 11
Postfix Decrement -- variable --; 10
Cast Operator () variable =(int) 123.45; 123
Function Operator () FunctionCall(parameter); return value
Array Index Operator [] arrayname[iIndex]; nth element
- Binari:
Assignment = variable=10; rezulta 10
Binary Plus + variable=variable + 5; 15
Binary Minus - variable=variable - 10; 5
Multiplication * variable=variable * 5; 25
Division / variable=variable / 5; 5
Modulus % variable=variable % 3; 2
Logical And & variable=5 & 3; 1
Logical Or | variable=5 | 3; 7
Bitwise XOR ^ variable=5 ^ 3; 6
Bitwise Shift Left << variable=5 << 3; 40
Bitwise Shift Right >> variable=5 >> 1; 2
Null Coalescing ?? variableb=variablea??5 2
- Operatori de compunere:
Addition Assignment += variable+=5;
Subtraction Assignment -= variable-=10;
Multiplication Assignment *= variable*=5;
Division Assignment /= variable/=5;
Modulus Assignment %= variable%=3;
And Assignment &= variable&=3;
Or Assignment |= variable|=3;
XOR Assignment ^= variable^= 3;
Left-Shift Assignment <<= variable<<=3;
Right-Shift Assignment >>= variable>>=1
- Operatori buleeni:
Equals ==
Not Equal !=
Less Than <
Greater Than >
And (Short Circuiting) &&
Or (Short Circuiting) ||
And &
Or |
Less Than or Equal <=
Greater Than or Equal >=
Logical XOR ^
- Operatorii ternari (abrevierea if else):
conditie buleana?expresie daca se evalueaza adevarat:expresie daca se evalueaza fals
variabila>5?Console.WriteLine(">5"):Console.WriteLine("<= 5");
- operatorii pointerilor (disponobili in programarea unsafe):
Asterisk Operator1 * declararea unui pointer
Asterisk Operator2 * referirea la un pointer
Ampersand Operator & obtinerea unei adrese
Arrow Operator -> referirea la un pointer si acces de membru

Cuvinte cheie (id = identificator, exp = expresie, bool = bulean, op = operator):


abstract1 abstract class id Clasa ce nu poate fi instantiata (utila ca variabila complexa globala).
abstract2 abstract return id Metoda nu este implementata in clasa curenta.
as id as type Formeaza identificatorul la un tip cunoscut sau NULL.
base base.id Acceseaza menmbrul lasei de baza.
break Break Iasa din bucla curenta sau din switch.
case case eticheta Un caz al unui Switch cu eticheta pe post de identificator.
catch catch(filtru) Filtreaza exceptiile aruncate dintr-un bloc try.
checked1 checked(exp) Daca expresia este supraincarcata rezulta o exceptie.
checked2 checked { bloc decl.} Acelasi lucru doar ca expresia se face intr-un bloc declarativ.
class class identity Defineste o clasa cu identitatea identity.
const const type identity Declara o constanta sau atribut constant.
continue Continue Sare la sfarsitul buclei si continua o nou iteriatie.
default default Este eticheta din switch la care se sare in cazul in care exp. evalueaza fals.
delegate delegate return method Defineste un tip ce detine pointeri de functii.
do do { } while(exp) Defineste o bucla do cu testul iterativ la sfarsit.
else else { } Altfel dupa if
enum enum enumname Defineste tipul enumerare.
event event delegate_id id Defineste un eveniment numit id de tipul delegat_id (oricare o fi el).
explicit explicit operator conversiontype Folosind operatorul de conversatie definite de user necesita explicit.
extern extern return method O metoda implementata extern.
false false O valoare buleana.
finally finally {statement_block } Perechea lui try. Trebuie sa contina clean up sau must exit.
fixed fixed(declaration) Fixeaza un pointer in memorie si previne colectorulde gunoi sa se atinga de el.
for for(initializers;bool_expression;iterators) O bucla for.
foreach foreach(element in enumerable_collection) O bucla foreach (element in tip_de_enumerare).
get get Accesor de metoda sau proprietate.
goto1 goto id Sare la o eticheta id.
goto2 goto case id Sare la o eticheta numita id in interiorul unui switch.
goto3 goto default Sare la o default in interiorul unui switch.
implicit implicit operator conversiontype (vezi explicit)
in foreach(element in enumerable_collection) (vezi foreach)
interface interface identifier Defineste o interfata.
internal internal identifier Internal este un tip sau metoda accesibila doar in assembly-ul curent.
is object is type Expresia evalueaza la true daca obiectul este de tipul declarat altfel la false.
lock lock(object) { } Protejare intr-o zona partajata critica.
namespace namespace identifier Creates a namespace called identifier.
new1 new identifier(constructor)Allocates memory for a class identifier on a managed heap and returns a
reference to the object.
new2 new return method Method hides the same method in the base class.
null null Sets unused references and nullable value types to null.
object object Object is an alias for System.Object, which is the base class to all .NET objects
(value or reference type).
operator operator operator_name Implements a user-defined operator_name in the current type.
readonly readonly type identifier Identifier is a field name and can be initialized at declaration or in a constructor
only.
ref ref type parameter The actual parameter is passed into the method and can be modified.
Parameter must be initialized before function invocation.
return return object Returns an object from a method. Methods that return void can have an empty
"return;".

sealed sealed identifier Identifier is a class name and is not inheritable.


set set Assignment method of property member.
sizeof sizeof(valuename) Sizeof returns the size of a value type; supported only in unsafe mode.
stackalloc stackalloc type [exp] Allocates an array of type on the stack; supported only in unsafe mode.
Expression determines the size of the array.
static static return method Method is class-bound and not associated with a specific instance.
struct struct identity Defines a structure named identity.
switch switch(exp) { } Control is transferred to either the matching switch label, if any, or to the
default case in the statement_block.
this This The this object is a reference to the current object.
throw throw object Throws a user-defined exception. Object should be derived from
System.Exception.
true True True is a Boolean value.
try try { } Try guards for exceptions in the statement_block.
typeof typeof(object) Returns the type of the object.
unchecked1 unchecked(exp) Expression is truncated if overflowed.
unchecked2 unchecked { } If an expression within statement block is overflowed, the expression is
truncated.
unsafe1 unsafe class identifier Class identifier can have unsafe code such as pointers.
unsafe2 unsafe return method The method can contain unsafe code such as pointers.
using1 using identifier Identifier is a namespace. The using keyword makes the namespace implicit.
using2 using(identifier) block IDisposable.Dispose, which is the explicit destructor, is called on identifier when
block is exited.
virtual virtual method Virtual makes the method overridable in a derived class.
void1 void method A void return means that the method does not return a value. The method can
omit a return statement or have an empty return.
void2 void *identifier Identifier is a pointer name. A void pointer is a conventional and typeless
pointer; supported only in unsafe mode.
volatile volatile fieldname Accesses to a volatile fieldname are immediate, which is useful in a
multithreaded environment.
where class classname<param> where constraint The where clause sets constraints on the classname. It must
be a generic class.
while while(bool_exp) { } The statement_block of the while loop is repeated until the boolean_expression
is false.
yield yield iterator_value In the enumerator pattern, the yield keyword adds values to be enumerated.