You are on page 1of 6

Access Modifiers

Class can be accessed by classes in: Public protected Internal private


[*] [*]
Another assembly Yes No
[*] [*]
Same assembly Yes Yes
Containing class Yes Yes Yes Yes
Class derived from containing class Yes Yes Yes No

Declaring variables

System.Int32 age = new System.Int32;


int age = 17;
System.Int32 age = 17;
include a trailing letter: float requires F or f; double has an
optional D or d; and decimal requires M or m.
ex :
decimal pct = .15M;
myChar = 'B'; // 'B' has an ASCII value of 66
myChar = (char) 66; // Equivalent to 'B'
myChar = '\u0042'; // Unicode escape sequence
myChar = '\x0042'; // Hex escape sequence
myChar = '\t'; // Simple esc sequence
short i16 = 200;
i16 = 0xC8 ; // hex value for 200

Declaring Arrays
int[] array = new int[5];
This array contains the elements from array[0] to array[4].
The new operator is used to create the array and initialize the array
elements to their default values. All the array elements are
initialized to zero.
int[] array3;
array3 = new int[] { 1, 3, 5, 7, 9 };

Multi-dimension Arrays
two-dimensional array of four rows and two columns
int[,] array = new int[4, 2];

new operator to assign an array to the variable


int[,] array = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

array of three dimensions, 4, 2, and 3


int[, ,] array1 = new int[4, 2, 3];

Jagged Arrays (Dynamic Array)


int[][] array = new int[3][];
array[0] = new int[3]; or array[0] = new int[] {4,5,6};
array[1] = new int[2]; or array[1] = new int[] {4,5};
array[2] = new int[4]; or array[2] = new int[] {4,5,6,7};
Static Classes
A class can be declared static, indicating that it contains only static members.
Or
If the static keyword is applied to a class, all the members of the class must be
static.

It is not possible to create instances of a static class using the new keyword.

Static classes are loaded automatically by the .NET Framework common language
runtime (CLR) when the program or namespace containing the class is loaded.

They cannot be inherited.

Static Members
Static members are initialized before the static member is accessed for the first time,
and before the static constructor.

A static method, field, property, or event is callable on a class even when no instance
of the class has been created.

If any instances of the class are created, they cannot be used to access the static
member.

Only one copy of static variables and events exists, and static methods and
properties can only access static fields and static events.

Constructors
You can prevent a class from being instantiated by making the constructor private
Constructors can be marked as public, private, protected, internal, or protected
internal.
A constructor can be declared static using the static keyword.
Static constructors are called automatically, immediately before any static fields are
accessed, and are normally used to initialize static class members.

Destructors
A class can only have one destructor.
Destructors cannot be inherited or overloaded.
Destructors cannot be called. They are invoked automatically.
A destructor does not take modifiers or have parameters
Empty destructors should not be used.
When the destructor is called, the garbage collector is invoked to process the queue.
If the destructor is empty, this simply results in a needless loss of performance.
The programmer has no control over when the destructor is called because this is
determined by the garbage collector.
If it considers an object eligible for destruction, it calls the destructor and reclaims
the memory used to store the object.
Destructors are also called when the program exits.
It is possible to force garbage collection by calling GC.Collect method, but in most
cases, this should be avoided because it may result in performance issues
The destructor implicitly calls Finalize on the object's base class. Therefore, the
preceding destructor code is implicitly translated to

protected override void Finalize()


{
try { // cleanup statements... } finally { base.Finalize(); }
}

Virtual Functions
Used to modify a method, property, or event declaration.
Allow methods to be overridden in a derived class.
Virtual member can be changed by an overriding member in a derived class.
By default, methods are non-virtual and U cannot override a non-virtual method.
U cannot use the virtual modifier with the static, abstract and override modifiers.
Virtual member cannot be private.
If the method in the derived class is not preceded by new or override keywords,
the compiler issues a warning and the method will behave as new method.
An override declaration cannot change the accessibility of the virtual method.
Both the override method and the virtual method must have the same access level
modifier.

If the method in the derived class is preceded with --


new - it is defined as independent of the method in the base class.
override - objects of the derived class will call override method rather than the base
class method.

this keyword refers to the current instance of the class.


Cannot refer this in a static method.
base keyword refers to the instance of the base class.
Cannot refer base in a static method.

Abstract
An abstract class cannot be instantiated.
An abstract class may contain abstract methods and accessors
A non-abstract class derived from an abstract class must include actual
implementations of all inherited abstract methods and accessors
An abstract method is implicitly a virtual method.
Sealed
The sealed modifier can be applied to classes, instance methods and properties.
A sealed class cannot be inherited.
A sealed method overrides a method in a base class, but itself cannot be overridden
further in any derived class.
When applied to a method or property, the sealed modifier must always be used
with override.

Interfaces
An interface is similar to an abstract base class: any non-abstract type inheriting the
interface must implement all its members.
An interface cannot be instantiated directly.
Interfaces can contain events, indexers, methods and properties.
Interfaces contain no implementation of methods.
Classes and structs can inherit from more than one interface.
An interface can itself inherit from multiple interfaces

Structures
The struct type is used for representing lightweight objects.
U cannot declare a default (parameterless) constructor or a destructor for a struct.
Copies of structs are created and destroyed automatically by the compiler, so a
default constructor and destructor are unnecessary.
U cannot inherit struct.
A struct can implement interfaces.
A struct is a value type, while a class is a reference type.

when a struct is passed to a method, a copy of the struct is passed.


when a class instance is passed, a reference is passed.

Exception Handling
1.Handled
2.Unhandled

2.There are two types of exceptions


Exceptions generated by an executing program.
Exceptions generated by the common language runtime.

Herarchy – Object -> Exception -> SystemException -> InvalidCastException


-> IOException
try
{
Code where error can be generated
….
} catch(Exception e)
{ Console.Writeline(e);}
try
{ ..

} catch(IndexOutOfRangeException e)
{ Console.Writeline(e);}
catch(Exception e)
{ Console.Writeline(e);}

Exception Object Base class None (use a derived


for all class of this
exceptions. exception).

SystemException Exception Base class None (use a derived


for all class of this
runtime- exception).
generated
errors.

IndexOutOfRangeException SystemException Thrown by Indexing an array


the runtime outside its valid
only when range:
an array is arr[arr.Length+1]
indexed
improperly.

NullReferenceException SystemException Thrown by object o = null;


the runtime o.ToString();
only when a
null object is
referenced.

try
{
..
..
}catch (Exception e)
{Console.WriteLine("Error: {0}",e);}
finally
{Console.WriteLine("This statement is always executed.");}

try
{
..
..
}finally
{Console.WriteLine("This statement is always executed.");}
try
{
Code where error can be generated
….
if (cond) { throw new Exception }
…..
} catch(Exception e)
{ Console.Writeline(e);}

Nested try catch


try
{
..
..
try
{
..
..
}catch (Exception e)
{Console.WriteLine("Error: {0}",e);}
finally
{Console.WriteLine("This statement is always executed.");}
..
..

}catch (Exception e)
{Console.WriteLine("Error: {0}",e);}
finally
{Console.WriteLine("This statement is always executed.");}

You might also like