BASICS OF C Variables are storage locations for

BASICS OF C#

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

Variable types Value types Directly contain data Cannot be null Reference types Contain references to objects May be null int i = 123; string s = "Hello world"; i s 123 "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 Signed Unsigned Character Floating-point Logical object, string sbyte, short, int, long byte, ushort, uint, ulong char float, double, decimal bool Predefined types are simply aliases for system-provided types For example, int == System. Int 32

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

C# Iteration Constructs Loop over data for-loop • for(init; condition; increment) foreach • Special form for enumerations (IEnumeration) while-loop • while (condition) { do something } do while loop • do { …something… } while (cond);
![foreach loop Simplify access to Lists, Arrays, . . . String[] list = new foreach loop Simplify access to Lists, Arrays, . . . String[] list = new](http://slidetodoc.com/presentation_image_h2/8dc9495f098c49ab0aa65be915c48ed5/image-8.jpg)
foreach loop Simplify access to Lists, Arrays, . . . String[] list = new String[]{“A“, “B“, “C“}; foreach (String s in list) { Console. Write. Line(s); }

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

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

Const , read. Only & 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. public class My. Class { public const double PI = 3. 14159; }

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

Const , read. Only & static 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… public class Car { public static int Number. Of. Wheels = 4; } int i = Car. Number. Of. Wheels;

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

C# Parameter Modifiers By value(default) void Foo (String. Builder x) { x = null; } String. Builder y = new String. Builder(); y. Append ("hello"); Foo (y); Console. Write. Line (y==null);

C# Parameter Modifiers Reference parameters void Foo (ref String. Builder x) { x = null; } String. Builder y = new String. Builder(); y. Append ("hello"); Foo (ref y); Console. Write. Line (y==null);

C# Parameter Modifiers Output parameters void Foo (out int x) { x = 10; } int y; Foo (out y); Console. Write. Line (y);
![C# Parameter Modifiers Parameter arrays void Show. Numbers (params int[] numbers) { foreach (int C# Parameter Modifiers Parameter arrays void Show. Numbers (params int[] numbers) { foreach (int](http://slidetodoc.com/presentation_image_h2/8dc9495f098c49ab0aa65be915c48ed5/image-19.jpg)
C# Parameter Modifiers Parameter arrays void Show. Numbers (params int[] numbers) { foreach (int x in numbers) { Console. Write (x+" "); } Console. Write. Line(); } int[] x = {1, 2, 3}; Show. Numbers (x); Show. Numbers (4, 5);

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

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

Structs e. g. struct Person { public string name; public System. Date. Time birth. Date; public int height. In. Cm; public int weight. In. Kg; }

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