Basics of C#

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

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.


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 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

 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 String[]{“A“, “B“, “C“};
foreach (String s in list)
{
Console.WriteLine(s);
}
C# Control Flow
 if (condition) {
}
 switch (variable) {
case a:
case b:
}
C# Access Modifiers

 Access modifiers change the visibility of an entity

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.

public class MyClass


{
public const double PI = 3.14159;
}
Const , readonly & static
 readOnly
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 MyClass
{
public readonly double PI = 3.14159;
}
public class MyClass
{
public readonly double PI;
public MyClass()
{
PI = 3.14159;
}
}
Const , readOnly & 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 NumberOfWheels = 4;
}

int i = Car.NumberOfWheels;
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 (StringBuilder x)
{
x = null;
}
StringBuilder y = new StringBuilder();
y.Append ("hello");
Foo (y);
Console.WriteLine (y==null);
C# Parameter Modifiers
 Reference parameters
void Foo (ref StringBuilder x)
{
x = null;
}
StringBuilder y = new StringBuilder();
y.Append ("hello");
Foo (ref y);
Console.WriteLine (y==null);
C# Parameter Modifiers
 Output parameters
void Foo (out int x)
{
x = 10;
}
int y;
Foo (out y);
Console.WriteLine (y);
C# Parameter Modifiers
 Parameter arrays
void ShowNumbers (params int[] numbers)
{
foreach (int x in numbers)
{
Console.Write (x+" ");
}
Console.WriteLine();
}
int[] x = {1, 2, 3};
ShowNumbers (x);
ShowNumbers (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.DateTime birthDate;
public int heightInCm;
public int weightInKg;
}
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.

You might also like