ASP.Net

New Features in C# 4.0

Posted on Updated on

Visual Studio 2010 is packed with new and enhanced features that simplify the entire development process from design to deployment. Customize your workspace with multiple monitor support. Create rich applications for SharePoint and the Web. Target multiple versions of the .NET Framework with the same tool. Eliminate the dreaded “no repro” problem with IntelliTrace. And much more.

Visual Studio 2010 is here!And of course this means that C# 4.0 is also here. Let’s do a quick review of the new language features added in this release.

Dynamic

The dynamic keyword is a key feature of this release. It closes the gap between dynamic and statically-typed languages. Now you can create dynamic objects and let their types be determined at run time. With the addition of the System.Dynamicnamespace, you can create expandable objects and advanced class wrappers, and you can provide interoperability between different languages, including dynamic ones. Here is one quick example:

dynamic contact = new ExpandoObject();

contact.Name = “Patrick Hines”;

contact.Phone = “206-555-0144”;

Covariance and Contravariance

Variance on generic type parameters in interfaces and delegates is another important feature of this release. It doesn’t add much new functionality, but rather makes things work as you expected them to in the first place. The major advantage is hidden in this simple line, which didn’t compile until C# 4.0:

IEnumerable<Object> objects = new List<String>();
The ability to implicitly convert references for objects instantiated with different type arguments makes it much easier to reuse code. Read the Covariance and Contravariance FAQ to learn more about this feature.

Optional (or Default) Parameters

People have been asking for this feature since C# 1.0. Three versions later, it’s finally here.

Now you can assign a default value to a parameter right within the method declaration. The user of the method can either pass a value or simply skip the argument. In the latter case, the default value is passed to the method.

Method declaration:

public static void SomeMethod(int optional = 0) { }

Method calls:
SomeMethod(); // 0 is used in the method.

SomeMethod(10);

Named Arguments

The order of parameters in a method declaration and the order of arguments you pass when calling the method don’t need to match anymore. You can provide arguments in any order you like by specifying parameter names in a method call. This might also improve the readability of your code.

var sample = new List<String>();

sample.InsertRange(collection: new List<String>(), index: 0);

sample.InsertRange(index: 0, collection: new List<String>());

Read more about optional parameters and named arguments on MSDN.

Improved COM Interop

The introduction of the dynamic keyword, optional parameters and named arguments enables improvement of COM interop. So, no more ugly code like this:

var excelApp = new Excel.Application();

// . . .

excelApp.get_Range(“A1”, “B4”).AutoFormat(

Excel.XlRangeAutoFormat.xlRangeAutoFormatTable3,

Type.Missing, Type.Missing, Type.Missing,

Type.Missing, Type.Missing, Type.Missing);

You can now simply write the following:

excelApp.Range[“A1”, “B3”].AutoFormat(

Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);

By the way, this code uses one more new feature: indexed properties (take a closer look at those square brackets after Range.) But this feature is available only for COM interop; you cannot create your own indexed properties in C# 4.0. For more information about new COM interop features, once again, refer to MSDN.

What Else?

Of course, C# benefits not only from new language features, but also from improvements to its integrated development environment (IDE) and to the .NET Framework.

Here are some links for further reading:

Happy coding
free counters

Advertisements

Date Formatting in C#

Posted on

Hello all.

hope doing well …

here is another tip related to C# programming …. i found it useful so sharing with you people …   this is how you can format date in different format in asp.net / C#

the usage is as follows

<%= String.Format(“{specifier}”, DateTime.Now) %>

where the possible specifiers are given below.

Specifier Description Output
d Short Date 15/05/2010
D Long Date 15 June 2010
t Short Time 21:08
T Long Time 21:08:59
f Full date and time 15 June 2010 21:08
F Full date and time (long) 15 June 2010 21:08:59
g Default date and time 15/06/2010 21:08
G Default date and time (long) 15/06/2010 21:08:59
M Day / Month 15 June
r RFC1123 date Tue, 15 June 2010 21:08:59 GMT
s Sortable date/time 2010-06-15 21:08:59
u Universal time, local timezone 2010-06-15 21:08:59Z
Y Month / Year June 2010
dd Day 15
ddd Short Day Name Sun
dddd Full Day Name Sunday
hh 2 digit hour 09
HH 2 digit hour (24 hour) 21
mm 2 digit minute 08
MM Month 04
MMM Short Month name Jun
MMMM Month name June
ss seconds 59
tt AM/PM PM
yy 2 digit year 07
yyyy 4 digit year 2010
: seperator, e.g. {0:hh:mm:ss} 09:08:59
/ seperator, e.g. {0:dd/MM/yyyy} 15/06/2010

happy development 🙂

free counters