Posted in .Net Basics, C# 4.0

yield in C#

yield – another useful tool in C#. This keyword is used in a combination with return.

What it does basically ??

It is helpful in the iterations like for and foreach. You can only use this on enumerators, such as one that returns IEnumerable. You can use it to “save the state” of the function after the return. After the yield is reached, and the value is returned, the foreach loop will call the function again, but you will pick up right where you left off. Let me show you this with an example:

public class MyList
{
    //using System.Collections;
    public static IEnumerable<int> GetPower(int number, int exponent)
    {
        int counter = 0;
        int result = 1;
        while (counter++ < exponent)
        {
            result = result * number;
            yield return result;
        }
    }

    static void Main()
    {
        // Display powers of 2 up to the exponent 9:
        foreach (int i in GetPower(2, 9))
        {
            Console.Write("{0} ", i);
        }
    }
}
/*
Output:
2 4 8 16 32 64 128 256 512
*/

Please note the use of generic and define the return type as enumerable of int type, this performs well as it avoid boxing and unboxing of the object the loop is returning. If you use only IEnumerable it will do box-unbox at the time of returning the element.

The yield keyword signals to the compiler that the method in which it appears is an iterator block. The compiler generates a class to implement the behavior that is expressed in the iterator block. In the iterator block, the yield keyword is used together with the return keyword to provide a value to the enumerator object. This is the value that is returned, for example, in each loop of a foreach statement.

Practically useful ??

Yes, you can use yield in several ways. For example, you can use it as a filter for displaying random HTML code in your ASP.NET website. Internally, you can select random elements from an array and yield them, such as for advertisements.

It also used with the “break” keyword.

I hope this help someone.

For more details yields in C# Reference on MSDN.

Posted in .Net Basics, C# 4.0

dynamic type in C# 4.0

The type dynamic was introduced with C# 4.0 long back but I have used this very recently in one of my project, so just want to share my experience with this wonderful feature of C# 4.0.

dynamic

The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. It behaves like type object in most circumstances, however the operations that contain expressions of type dynamic are not resolved or type checked by the compiler.

Variable of type dynamic is compiled into variable of type object, and that is why the dynamic type is exists at compile time but not at runtime.

How to use dynamic – a simple example:

class Program
{
    static void Main(string[] args)
    {
        dynamic dyn = 1;
        object obj = 1;
        System.Console.WriteLine(dyn.GetType());
        System.Console.WriteLine(obj.GetType());
    }
}

output:

System.Int32
System.Int32

you will see the output returns the same type as both the variables are having int value assigned. If you want to see the difference between dyn and obj at compile time, try following lines and compile the code.

dyn = dyn + 3;
obj = obj + 3;

The compiler raised an error for attempted addition of an integer and an object in expression obj + 3. But, no error is reported for dyn + 3. The expression that contains dyn is not checked at compile time because the type of dyn is dynamic.

For more information about the dynamic type http://msdn.microsoft.com/en-us/library/dd264741(VS.100).aspx and Using Type dynamic 

Posted in Silverlight

Lazy Initialization

Today I was searching for some tips to improve performance of my Silverlight application and I came across a feature called Lazy Initialization.

Continue reading “Lazy Initialization”