Você está na página 1de 3

C# Basics

A variable is simply a named location in which a value is held whilst the program runs

A statement is an instruction to perform one particular operation

In the case of C# you can lump statements together to form a lump of program which does one particular task. Such
a lump is called a method.

The names that you invent to identify things are called identifiers

The words which are part of the C# language itself are called keywords

Objects that are part of the framework we are using (eg. Console. ) ( Object.Method(parameters) )

A class is a container which holds data and program code to do a particular job. A C# program is made up of one or
more classes.

To declare variables at the start of program:

Give the type of the data, followed by a list of the names you want the variables to have

Eg. String widthString, heightString, etc… ;

Storing Numbers

Integer literal values vs Real values

“Real” are numbers that’s not integers. A float literal is a real number with an f after it and double literal dosent.

Storing Text

char: holds a single character (enclose in single quotes ‘ )


string: holds a string of text (enclose in double quotes “ )

To output verbatim string: put @ infront of label

Eg. @"\x0041BCDE\a" will be printed as \x0041BCDE\a

Declaring variables

Usage of identifiers:

Do not use numbers ahead of alphabets.

Assignment statements: gives a value to a specified variable eg. int charAge = 30 ;

Changing the Type of Data


Conversion from string to double (eg.)

string XString = console.readline();

double X = double.parse(XString);

Casting

Its possible to “widen” a value, but not “narrow” it.

Widening: int X = 1 ; float Y = X ; will work fine

Narrowing: double X = 1 ; float Y = X ; will not work

However, through the use of casting, “narrowing” can be done [With the use of brackets]

Eg. double X = 1.5 ; float Y = (float) X ;

This is to ‘tell’ the complier that you are aware of the potential loss of info by narrowing, and thus it will compile

BAD PRACTICE: Eg. int X ; (int) X = 1.999 ; will result in X printing a value of 1 and ignoring whatever rounding.

Controlling Program Flow


Relational Operators Combining Logical Operators
== Equals && And
!= Not equal || Or
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal
! not

Você também pode gostar