Archive

Archive for the ‘C#’ Category

C# is and as operators

Saturday, July 8, 2006 21 comments

The below excerpt is from the book: Applied Microsoft .Net Framework Programming by Jeffrey Richter. If you are a C# programmer and you are not aware of is and as operators in C#, then reading the following extract would be very useful.

C# offers another way to cast using the is operator. The is operator checks whether an object is compatible with a given type, and the result of the evaluation is a Boolean: true or false. The is operator will never throw an exception. The following code demonstrates:

System.Object o = new System.Object();

System.Boolean b1 = (o is System.Object); // b1 is true.

System.Boolean b2 = (o is Employee); // b2 is false.

 

If the object reference is null, the is operator always returns false because there is no object available to check its type. The is operator is typically used as follows:

if (o is Employee) {

Employee e = (Employee) o;

// Use e within the ‘if’ statement.

}

 

In this code, the CLR is actually checking the object’s type twice: the is operator first checks to see if o is compatible with the Employee type. If it is, then inside the if statement, the CLR again verifies that o refers to an Employee when performing the cast. Because this programming paradigm is quite common, C# offers a way to simplify this code and improve its performance by providing an as operator:

Employee e = o as Employee;

if (e != null) {

// Use e within the ‘if’ statement.

}

 

Read more…

Categories: C#

Presentation – C# 3.0 & LINQ – By Raimond Brookman

Saturday, April 1, 2006 Leave a comment

As i was browsing through the various blogs to find more information on what new features i can expect as part of C# 3.0, i came across this presentation made by Raimond Brookman at the DevDays 2006. It is a very interesting presentation, which gives us enough information as to what is coming up as part of C# 3.0 and LINQ. I would suggest you to go through the presentation in order to get a quick preview of the enhancements that we are going to see in future version of .Net.

Click here to download the presentation.

Categories: C#, LINQ

C# 3.0

Saturday, April 1, 2006 Leave a comment

C# 3.0 Language Enhancements

C# 3.0 introduces several language extensions that build on C# 2.0 to support the creation and use of higher-order, functional-style class libraries. The extensions enable construction of compositional APIs that have equal expressive power of query languages in domains such as relational databases and XML. Get more information, and try hands-on labs here.

Categories: C#