C Sharp and yield keyword

Hi,

 

One of the cool and yet most unknown feature of the C# is the yield keyword. The yield keyword is used in an iterator block to provide a value to the enumerator object or to signal the end of the iteration. When used the expression is evaluated and returned as a value to the enumerator object. Note the expression has to be implicitebly convertible to yield type of the iterator. Here is an example

 

Continued...

Working with Asp.Net profile properties to save user specific information

Hi,

Many a times we need to keep unique information of the user in the web application so that the information can be used for the user when required. For example, you can allow the user to choose the background image of the site. Or even let the user select a theme from the many themes in the site.
[Note: The multiple theme property has already been incorporated in my site. You can choose the Change The Theme button in the left top to change the theme of this site.]

We normally want to store some values for individual user, so as to allow personalization of the site for individual user. These profile values need to be stored in database so that they can be retrieved and used when the user visits again.

Continued...

C sharp Difference between the out and ref type parameters

Hi,

In C Sharp(C#) we can have three types of parameters in a function. The parameters can be In parameter (which is not returned back to the caller of the function), Out parameter and ref parameter (where by a reference to the variable is passed back).

The last two type (out and ref) mentioned looks quite similar in nature. Both parameters are used to return back some value to the caller of the function. But still there is a small but important difference between them. Both of the parameter type has been kept in the C# language for specific scenario.

The main difference between the two types is in the rule for definite assignment of them.

When we use the out parameter, The program calling the function need not assign a value to the out parameter before making the call to the function. The value of the out parameter has to be set by the function before returning the value.

Continued...

C Sharp 3.0 and Anonymous Types

Hi,

I have been using the new Orcas Beta 2 and dot net frame work for some time now and have been really enjoying the features of the C Sharp. One of the new features also includes Anonymous types. It first thought there might not be much use of this feature and will only be a syntactic sugar for us. But after playing with Dot net framework 3.5 for a while I understand the importance of this new feature. Let me also tell that this feature will be most useful in case you are querying through the new LINQ (Language Integrated Query).

With the help of the anonymous types we can define a type without declaring it from before hand. The main difference between the defined types and anonymous types is that we leave the type name in the anonymous types. The compiler will parse the syntax and create a standard CLR type itself for the definition of the type. Here is an example of an anonymous type.

var blog =  new

Continued...

What is the difference between a delegate and an event?

Hi

A few days back one of my colleague got confused with the difference between a delegate and event. We can do the same thing with both a delegate and an event. There only seems to be a syntactical difference between the 2. So he searched and found the answer to the fact.

Here is the difference between a delegate and an event.

Delegate:

public class VikramDel

{

public delegate void VikramExampleDelegate(int num1,string str1);

public VikramExampleDelegate VikramDeleageteCallback;

}

Event:

public class VikramEvent

{

public delegate void VikramExampleEvent(int num1,string str2);

Continued...

New features of C Sharp 3.0 LINQ (Language Integrated Query)

Hi

LINQ (Language Integrated Query) is the composition of many standard query operators that allow us to work with data of any datasource in a very intuitive way. LINQ provide compile time checking of query and the ability to debug through query.

To show a very basic example of what can be done with the help of LINQ I am using and in memory generic list collection that queried by LINQ.

Continued...

New Language feature in C # 3.0 Object Initializer

Hi

Another of the new language feature in the C sharp 3.0 is the object Initializer. Types within the .NET Framework rely heavily on the use of properties.  When instantiating and using new classes, it is very common to write code like below.

   Employee  employee = new Employee();
   employee.FirstName = "Vikram";
   employee.LastName = "Lakhotia";
   employee.Age = 24;

I know we can also create a constructor and pass the value as the parameters. But lets say we do not have any constructor.

With C# 3.0 the above code can be simplified an written like this.

Continued...

New GCCollectionMode in ORCAS

Hi

With orcas there is a new addition to the GC class. There is a new overload being added to the GC class Collect method. The new overload takes two parameters, int Generation and GCCollectionMode.

Here generation is the heighest generation to collect. The value can be anything between 0 and GC.MaxGeneration. Mode can be either default, Forced or Optimized. Default has the same effect as calling the GC.Collect without providing any mode. Forced mode guarantee's a collection for all generation upto and including generation provided in earlier parameter

Continued...

New language feature in c# 3.0 Implicitly typed local variables

Hi

Another of the new feature in The C# 3.0 will be the declaration of implicitly typed local variables. This means that we can declare any local variable with the type var and it type will be inferred from the expression that is used to declare it.

This means that if we declare a local variable with type var (and there is no type named var in the scope) the declaration becomes implicitly typed local variable. Hence the following lines of code will be possible and correct in C# 3.0

var int1 = 10;

var decimal = 2.45;

var string = “Vikram Lakhotia”;

var intArray = new int[] {1,4,8,0,3};

Continued...

New feature of C Sharp “Implicitly Typed local variable”

Hi

Another of the new feature of C# 3.0 is the implicitly typed local variable. For this a new keyword var is introduced in the C# list of keyword. The keyword allows the developer to declare variables without specifying its type. Hence the following lines will compile in C Sharp 3.0(This will not compile in C Sharp 2.0 or below).

Continued...

New “Extension Methods” feature in C sharp 3.0

Hi

Extension Methods are really cool new features in the C# 3.0. With extension methods we can attach new function to the existing (even the value types). It does not matter if do not have access to the type, we can still add new methods to the type.

For example I can write an extension method to trace, response or print every element of the collection. This is very simple to be done. Here is the example of the trace method.

public static class Ext

{

            public static void Trace<T>(this ICollection<T> col)

            {

Continued...

New Language feature in Csharp 3.0 – Extension method

Hi

Another new language feature of the C# 3.0 is the extension method. This feature will also be available on the next version of the VB.Net. Extension methods allow developer to add new methods to a public contract of an existing CLR type without sub classing the type.

Extension Methods help blend the flexibility of "duck typing" support popular within dynamic languages today with the performance and compile-time validation of strongly typed languages.

A simple example of the Extension method would be to add some validation to the string class. Lets say when we want to validate that a string is a valid Email or not we normally use another class with a static method to validate it. The typical example would be

string email = Request.QueryString["email"];

if ( EmailValidator.IsValid(email) ) { }
  

Continued...

New language feature in C Sharp Orcas “Automatic Properties”

Hi,

In this post I will talk about the new features of C#3.0. These new features are really helpful to develop a product fast. Today when we code with C# we are used to code string and properties for it in the class. The code is simple

Private int _age;

Public int age

{

            get{ return _age; }

            set{ _age=value; }

}

Continued...
 
Copyright © 2006 - 2008 Vikram Lakhotia