Professional Documents
Culture Documents
Basics of C#
Basics of C#
Basics of C#
Variables
Variables are storage locations for values in C#.
A variable has a variable name and
int i = 123;
string s = "Hello world";
i 123
s "Hello world"
Variable types
Value types
Primitives int i;
Enums enum State { Off, On }
Structs struct Point { int x, y; }
Reference types
Classes class Foo: Bar{...}
Interfaces interface IFoo: IBar {...}
Arrays string[] a = new string[10];
Delegates delegate void Empty();
Predefined Types
C# predefined types
Reference object, string
Signed sbyte, short, int, long
Unsigned byte, ushort, uint, ulong
Character char
Floating-point float, double, decimal
Logical bool
Predefined types are simply aliases for
system-provided types
For example, int == System.Int32
Boxing and Unboxing
public everybody
private for this class only
protected for this class and
subclasses only
internal for this assembly only
protected internal class, subclasses, and
assembly
Const , readOnly & static
Const
A constant member is defined at compile time and
cannot be changed at runtime. Constants are declared
as a field, using the const keyword and must be
initialized as they are declared. e.g.
Static Members
Use of the static modifier to declare a static
member, means that the member is no longer tied
to a specific object. This means that the member
can be accessed without creating an instance of the
class. Only one copy of static fields and events
exists, and static methods and properties can only
access static fields and static events.
Example…
int i = Car.NumberOfWheels;
C# Parameter Modifiers