FCI-Helwan blog

Just another FCI-H weblog

What’s New in C# 2.0 ? Part 2

In the name of ALLAH

Welcome in Part2 (Sorry it will be Long Post Coz it Will be the Final Part )
Iterators allow you to easily create enumerable classes, classes that allow you to iterate through their collection of values through a foreach loop. Classes that implement the IEnumerable interface allow you to use foreach loop.
To use IEnumerable interface we must implement GetEmumerator Method which return IEnumerator object which, this object allow foreach loop to work the way it does.
The new iterator pattern makes use of the yield keyword , from this moment we don’t have to create and implement an IEnumerable object , yield does all of this.
Example : –
If we tried to Store 100 000 string in array of string then we tried to retrieve the array’s values using foreach loop one by the old way and the second with the Yield Key word.
Here is the difference with Yield it will take 93 milliseconds and with the Old Way 343 milliseconds.

Syntax:-

yield keyword in foreach loop

Yield Var;

the Advantages :-

Good Running Time
Save Code and increases its readability

Nullable Types

The Nullable Type Can Represent the Range of Value for any type plus it can hold the value null

Example : –
Nullable can Hold Values in this Range from 2147483648 to 2147483647 in addition to Null Value

another Example : – Nullable can hold The Value True or False ( false set as default in the normal Bool ) in addition to Null Value .

but what is its importance ?
assume you are working in database application
a Boolean field in a database can store the values true or false, or it may be undefined.

Syntax:
int? x = 10; or double? d = 4.108; ….. and so on

the most important Properties in nullable variables : –

The HasValue property returns true if the variable contains a value, or false if it is null.
The Value property returns a value if one is assigned, otherwise
a System.InvalidOperationException is thrown.

The last Thing to say about nullable Type that it can work on Value and Ref. Type.

Static Class

Static class was developed in c# 2.0 to prevent the problem of working with The classes which have Private constructors by using Reflection.
Use The Keyword static in the declaration of the class to prevent the class instantiation by Reflection

Syntax :-
static class Class1

Anonymous Methods

Hint :- To understand Anonymous Methods you must understand Delegates Very Good. I will depend here that you Can use Delegates.

Earlier( in previous versions of C# ) we were handling the events like this

myButton.Click += new EventHandler(MyButton_OnClick);

public void MyButton_OnClick(object sender, EventArgs e)
{
// do something
}
Here we first added a delegate to myButton’s Click event. The delegate is made to reference the MyButton_OnClick() method which is defined earlier.

Anonymous methods allow you to define un-named methods ( any block of code )
Like this

this.myButton.Click += delegate(object sender, EventArgs e)
{
MessageBox.Show(“yes”);
};

The Advantages: –

Anonymous methods can be useful when we only want to use our event handler to point to only one method. It can save code size and make it more readable if used appropriately.

mmm i reached the End Of This Post .. but i will be back Soon isA with C# 3.0 .. So Keep Waiting 🙂

Advertisements

January 30, 2007 - Posted by | C#

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: