BASICS OF C#

Variables
Variables are storage locations for values in C#. A variable has a variable name and a data type associated with it.

string s = "Hello world".Variable types Value types † † Directly contain data Cannot be null Contain references to objects May be null int i = 123. i s 123 "Hello world" Reference types † † .

.. Reference types † Classes † Interfaces † Arrays † Delegates delegate void Empty(). . y.Variable types Value types † Primitives † Enums † Structs int i.. On } struct Point { int x..} string[] a = new string[10].} interface IFoo: IBar {. } class Foo: Bar{. enum State { Off.

long byte.Predefined Types C# predefined types † Reference † Signed † Unsigned † Character † Floating-point † Logical object. int == System. double. decimal bool Predefined types are simply aliases for system-provided types † For example. ushort. int. ulong char float. uint. short. string sbyte.Int32 .

e. int i = 25. e.Boxing and Unboxing Boxing: automatic conversion of a value-type in a reference-type.g. . int j = (int)o.g. Unboxing: conversion of a reference-type into a value-type. Object o = i.

increment) foreach ‡ Special form for enumerations (IEnumeration) while-loop ‡ while (condition) { do something } do while loop ‡ do { «something« } while (cond). . condition.C# Iteration Constructs Loop over data for-loop ‡ for(init.

.foreach loop Simplify access to Lists. foreach (String s in list) { Console.WriteLine(s). ´C´}. ´B´.. . String[] list = new String[]{´A´. Arrays. } .

C# Control Flow if (condition) { } switch (variable) { case a: case b: } .

subclasses. and assembly .C# Access Modifiers Access modifiers change the visibility of an entity public private protected internal protected internal everybody for this class only for this class and subclasses only for this assembly only class.

public class MyClass { public const double PI = 3.g. e.Const . readOnly & static Const A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field. } .14159. using the const keyword and must be initialized as they are declared.

public MyClass() { PI = 3. e. } } .14159. in a constructor as well being able to be initialized as they are declared. } public class MyClass { public readonly double PI. The difference is that a readonly member can be initialized at runtime.g. readonly & static readOnly A read only member is like a constant in that it represents an unchanging value.Const . public class MyClass { public readonly double PI = 3.14159.

. readOnly & static Static Members Use of the static modifier to declare a static member.Const . means that the member is no longer tied to a specific object. and static methods and properties can only access static fields and static events. Only one copy of static fields and events exists. This means that the member can be accessed without creating an instance of the class.

NumberOfWheels. . } int i = Car.Example« public class Car { public static int NumberOfWheels = 4.

C# Parameter Modifiers Parameter modifier change the way parameters are passed -(none) by value (default) -out value from method assigned to parameter -ref by reference -param open parameter list .

WriteLine (y==null). .Append ("hello").C# Parameter Modifiers By value(default) void Foo (StringBuilder x) { x = null. } StringBuilder y = new StringBuilder(). y. Foo (y). Console.

. Foo (ref y). Console.WriteLine (y==null). y.Append ("hello"). } StringBuilder y = new StringBuilder().C# Parameter Modifiers Reference parameters void Foo (ref StringBuilder x) { x = null.

C# Parameter Modifiers Output parameters void Foo (out int x) { x = 10.WriteLine (y). . Console. } int y. Foo (out y).

Write (x+" "). 2. . 5).C# Parameter Modifiers Parameter arrays void ShowNumbers (params int[] numbers) { foreach (int x in numbers) { Console. } Console. ShowNumbers (4. 3}. } int[] x = {1. ShowNumbers (x).WriteLine().

enum Weekday { Monday. . Thursday. Saturday. Friday.Enumerations An enumeration is a data type that enumerates a set of items by assigning to each of them an identifier (a name). Tuesday. while exposing an underlying base type for ordering the elements of the enumeration. Sunday }. but can be any one of the integral types except for char.g. e. Wednesday. The underlying type is int by default.

but there are important differences.Structs Structures (keyword struct) are light-weight objects. and even implement interfaces. . methods. Structs are similar to classes in that they can have constructors.

Structs e.DateTime birthDate.g. public int weightInKg. struct Person { public string name. } . public System. public int heightInCm.

even if you don't want one. Structs always have a default constructor. Structs cannot support inheritance. Classes allow you to hide the constructor away by using the "private" modifier. which means they behave differently when passed into methods as parameters. .Structs v/s Class Structs are value types while classes are reference types. whereas structures must have one.

Sign up to vote on this title
UsefulNot useful