Monthly Archives: May 2012

IEnumerable vs IEnumerator

1) IEnumerable actually uses IEnumerator. i.e IEnumerable uses IEnumerator internally through GetIEnumerator.
2) The biggest difference is states, i.e. IEnumerable does not remember the cursor state i.e currently row which is iterating through, where I enumerator does.
IEnumerator exposes few methods which uses the state like:
MoveNext()
Current()

Advertisements

Leave a comment

Filed under C#

Abstract method vs Virtual method

Abstract method Virtual method
1) Abstract method doesn’t have any body defined. 1) Virtual method have their body defined otherwise it will give compile-time error message “<VirtualMethod()> must declare body because it is not defined abstract, extern, or partial.
2) Mandatory to implement them. 2) Not mandatory to override them.
3) They can be defined in abstract classes only. 3) They can be defined in any instance class.

Leave a comment

Filed under .Net, C#

Difference b/w String and string ?

They both are similar datatype as “string” is the alias of “String”. The original datatype class is String (System.String). Below is few more alias we have in .net:

Leave a comment

Filed under C#

Generics

Generics has the below features:

— makes logic independent of DataTypes.

— strongly types as Array.

— flexible of DataTypes like arraylist without boxing & unboxing.

Below is the project which show use of two Generic classes CoreDemo & CoreDemoDual:

Generic Classes


2 Comments

Filed under .Net, C#

Nullable Types & Why we need them?

The .NET Framework stores value types in the stack and implicitly initializes them to their default values. For example, an int gets initialized to 0, a bool to false, and so on.
In database missing information in records represented by null values. So the problem arises when fetch data from the data base, how to represent missing null value in our program because primitive data only stores real values in C#.

Nullable variable= null;
or
int? variable= null;

You cannot create a nullable type based on a reference type. (Reference types already support the null value.)

Use the ?? operator to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;

Properties
Nullable types has two important property
Hasvalue – indicate that variable contains real values or not. It returns false if variable contains no value and true if some real values is stored in it.
Value – Return real value stored in variable. It throws InvalidOperationException exception.
Method
GetValueOrDefault – Return value stored by Nullable type variable or the value passed in function.
There are two overload of this function.
Syntax
GetValueOrDefault() – returns real value or the default value of object.
GetValueOrDefault(T) – returns real value or the value passed.
Pseudocode Code for above problem
//variable define
DateTime? deathdate = null;

//for the people who is already dead
if( deathdate.Hasvalue) // checking for the nullable varialbe has value or not
age = deathdate.Value – birthdate;
//for the people who is till alive
else
age = todays date – birthdate;

Leave a comment

Filed under C#