Thursday, February 4, 2010

Anonymous Types

The ability to create new types 'on the fly' is done using Anonymous Types.

All of this happens at compile time, so anonymous types are still strongly typed. In reality, the compiler automatically creates a class that represents the anonymous type.

Example: var product = new {Name = "C# Rocks!", Price = 3};

-> When an anonymous type is assigned to a variable, that variable must be initialized with the var construct.

-> Anonymous types are reference types that derive directly from object.

-> If two or more anonymous types have the same number and type of properties in the same order, the compiler treats them as the same type and they share the same compiler-generated type information.

-> Anonymous types cannot contain unsafe types as properties.

Note: While anonymous types might not seem all that useful on the surface, they are the cornerstone for LINQ. Without anonymous types, LINQ would not be able to create types dynamically. This is what allows LINQ to query for an arbitrary set of data, without first having the structure declared.

Implicitly typed local variable in C#

Implicitly typed local variable is a variable that can be declared without specifying the .NET type explicity. The type of that variable will be inferred by the complier from the expression on the right side of initialization statement.

Examples:

var i = 5;

When the compiler sees this in the code, it tries to figure out the type of the variable based on the value you assigned it. Hence, if you call GetType() on i, the type returned is actually System.Int32.

The following will give you an error because the compiler can't figure out what type the variable is:

var i = null;

You have to initialize the variable with something that the compiler understands so it can figure out the type. Calling GetType() on firstName here would give you System.String:

var firstName = "David";

Limitations:

-> The declarator must include an initializer. Unlike normal declarations, you can’t declare the implicitly type variable without initializing.

-> The compile-time type of the initializer expression cannot be the null type.

-> If the local variable declaration includes multiple declarators, the initializers must all have the same compile-time type.The implicitly-type local variable cann’t be initialized with different types more than one time. You can’t assign the string to varaible “test” after initializing with integer value “1".

-> Cannot initialize an implicitly-typed local variable with an array initializer like var test = { 1, 2, 3 }; Can only use array initializer expressions to assign to array types, like a new expression var test1 = new[] { 1, 2, 3 };

-> an implicitly typed local variable cannot be Nullable variable which means the following syntax to create a Nullable variable in C# will not work with implicitly typed variables - var? age = null;

-> Implicit Typed Variable cannot be used as class members such as field. They can only be used as local variables inside methods.

Extension Methods

It is a special kind of static method that allows you to add new methods to existing types without deriving, recompiling or modifying the original type. The extension methods are called as if they were instance methods from the extended type.

public static class MyExtensionClass
{
public static int Multiply(this int num)
{
return num * num;
}
}

class Program
{
static void Main(string[] args)
{
int i = 3;
Console.WriteLine(i.Multiply());
Console.ReadLine();
}
}

Here are the few things we should keep in mind while using Extension Methods:

-> This keyword has to be the first parameter in the extension method parameter list. The type of the first parameter will be the type that is extended.
-> Extension methods can't access the private methods in the extended type.
-> Extension methods can be defined only in a static class.
-> If you want to add new methods to a type, you don't have the source code for it, the ideal solution is to use and implement extension methods of that type.
-> If you create extension methods that have the same signature methods inside the type you are extending, then the extension methods will never be called. Extension methods cannot be used to override existing methods.
-> If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced i.e. extension methods.
-> The concept of extension methods cannot be applied to fields, properties or events.

Thursday, November 19, 2009

Microsoft SharePoint Server 2010 Beta is now available

Now you can evaluate the Beta product to simplify your IT environment and give you more control.

There are two versions of SharePoint Server 2010 Beta. Installation of the SharePoint Server 2010 Beta requires one of the following product keys:

1. SharePoint Server 2010 (Enterprise Client Access License features)
2. SharePoint Server 2010 for Internet Sites, Enterprise

The SharePoint Server 2010 Beta software is available in the following languages:
Chinese (Simplified), English, French, German, Japanese, Russian and Spanish

Note: Don't forget to check the "System Requirements" before downloading.

Microsoft Office 2010 Beta is available now

Microsoft Office Professional Plus 2010 Beta is now available for Tech enthusiasts to download. Office 2010 lets you work how, when, and where you want, letting you get things from a PC, the Web, and even a smartphone. It includes the following:

Word, OneNote, InfoPath, PowerPoint, Access, SharePoint Workspace, Outlook, Publisher, Communicator and Excel.

It is around 685 MB download and here is the link:

Microsoft Office Professional Plus 2010 Beta

Before downloading, you should review the System requirements for Office 2010.

Other Microsoft Beta products available:

Microsoft Visio 2010

Microsoft SharePoint Server Enterprise 2010 Beta

Microsoft SharePoint Server for Internet Sites Enterprise 2010 Beta