Tuesday, 14 February 2012

Nullable Types

Nullable types:

A Nullable type was introduced in C# 2.0 as a value type. Microsoft introduced these types for solving the problem of assigning default values to value types at the time of value type declaration.

Nullable types allow the value types that they will accept the null values instead of default values.

’?’ modifier is used in Nullable types after the data types and that allows the null values for that type.

The type before the ‘?’ modifier is known as underlying type of Nullable types.

The underlying type of Nullable types is either a value type or any parameter that is considere to value types but it won’t be reference type

int?,double ? are valid for Nullable types but string? is not a Nullable type

The underlying type must be itself a Nullable type.

If we define an object like T?, it is a alias for system.nullable

An object of a Nullable type has two public properties:

· Hasvalue:

Hasvalue property is used for bool types. If an instance’s Hasvalue is true, then that is said to be non null. If Hasvalue is false then that instance has null value .

The default value for Hasvalue is false.

· Value:

If we assign any value to instance of a Nullable type,then value property returns that value. Otherwise it throws an exception.

There is no default value for this property.

Example:

Class N

{

Static void main (string[] args)

{

int? n = null;

if (n.Hasvalue == true)

{

Console.writeLine (“n value is”+ n.value );

}

else

{

Console.writeline ( “n value is null”);

}

}

}

Anonymous Types

Anonymous types:

Anonymous types are introduced in C# 3.0 which permits object initializer to create anonymous type by using the ‘var’ keyword.

Anonymous type is a class which is created by CLR automatically at run time and this class is not visible by users.

These types can reduce the code that need to access the class properties.

Class does not need to define properties, CLR will taken at run time.

Example:

Class AT

{

Static void main (string[] args)

{

Var i=new { no= 1 , name= “ ravi”, phoneno = 9985585867};

Console. Write ( i. no + “ “ +i. name + i.phoneno );

Console.ReadLine ();

}

}

In above example,we use ‘var’ keyword to define properties for class AT in single line of code .so anonymous types are used to define properties of class in simple manner.

Partial classes and Partial methods

Partial classes:

Partial classes are the classes which are dividing into no.of classes with the same name.partial classes can declared by the keyword”partial” .

partial classes are needed when we are doing big projects that we can develop the class by many developers at a time.

The compiler combines all parts of the partial class into one at complietime and exicute that class.

Example:

Public partial class Display

{

Console.writeline(“this is first part of partial class”);

}

Public partial class Display

{

console.writeline(“this is second part of partial class”);

}

Partial methods:

A Partial class must contain atleast one partial method.

One partial method contains signature and other may contain implementation of the method.without implementation the compiler does n’t execute any partial method.

We must define these by ” partial” modifier

Partail methods can be static and they must have rerturn type.

Example:

Partial class P

{

Partial void print():

Partial class p

{

Partial void print()

{

Console.writeLine(“this is partial method”);

}

}

Public static void main()

{

P a=new P();

a.print();

}

}

Sealed Classes


Sealed classes are classes which are not inheritable.To prevent the class from inheritance we use “sealed” keyword before the class name.

If we use sealed keyword to any method that method can not be inheritable.

It can overrides the parent class method,but itself cannot be override in other classes.

Example:

Sealed class B

{

Public int i;

Public int j;

}

Class program

{

B s=new B();

s.i=345;

s.j=135;

console.writeline(“i={0},j={1}”,s.i,s.j);

}

Exceptional handling

In C#, exceptional handling provides an environment that will handle the errors in the code by using three different blocks.

· Try

· Catch

· Finally

Try block:

In try block,we can place the code which will have errors.

Syntax:

try

{

Statements://code in which error occured

}

Catch block:

Catch block handles the exception if it occurs.catch block is optional.if there is an error in try block then

only control goes to catch block.

In this block, we can handle the exception which occurred in try block.

Syntax:

Catch(exception e)

{

Statements;//handling the exception

}

Finally block:

Finally block is mandatory in exceptional handling in C #.this block exicutes irrespective of error occurred in try block.

If thereis no error in try block then control goes to finally block ,else goes to catch block and then comes to finally block.

Syntax:

finally

{

Statements;//code that will run irrespective of error

}

DELEGATES

Delegates:

Delegates are types that they describes the signature of the method.delegates give reference to a function or method.

A delegate declared by delegate keyword.

We must declare the parameters through delegate.

Example:

Class delegateExample

{

delegate int D(string s);

Public int A()

{

return 0;

}

Public int B()

{

return 0;

}

Public void static void main(string []args)

{

D d1=new D(A);

D d2=new D(B);

String mystring=”hello world”;

d2(mystring);

d1(mystring);

}

}

Constructors


Constructors are the special methods invoked when an object is first created.

The constructor is created with class name.

Some distinct characteristics of constructors:

Constructors do not have any return type.

Constructors are always public.

Constructor is automatically declared by CLR, whenever the class is instantiated.

Example:

Class A

{

A ()//constructor name same as class name

{

//body

}

}