C# 4.0 + Visual Studio 2010 Beta + Windows 7 RC

Here we are with the Visual Studio 2010 Beta installed on Windows 7 RC, the most coolest and latest stuff out yet 🙂

In this post, I wanted to share some glimpse at Visual Studio 2010 beta along with some demonstration of one of the feature of C# 4.0 🙂 For Windows 7, I will write another detail post about the new features and what I like most and why it can be the best windows till yet!

So, coming back to VS 2010, If you start Visual Studio 2010 Beta, you will notice a major change in UI of Visual Studio with a nice, interactive and customizable startup page :

VS 2010 Beta Start up

Yes, It’s UI is built on Windows Presentation Foundation (WPF). So let’s go ahead and create a new project:

As you can see we have a similar kind of option, as in VS 2008, is available to select target framework version which is by default 4.0 🙂 and as you can see we have Silverlight templates available so now no need to install separate Silverlight 2 tools for VS :). Let’s move forward and create a new C# Console Application:

You will notice that editor itself is in WPF along with the intellisense menu but works almost as identical. So let’s try one of the feature of C# 4.0 🙂

I’m going to implement a method that takes dynamic typed object and that object will invoke a method which will be resolved at runtime!

DemoCSharpFeature

Didn’t get? Keep reading 🙂 Here we have a DemoCSharpFeature class that have a static method having a dynamic parameter “obj”. Now inside that method, I have written obj.ABC() which can be resolved only at runtime because at compile time, compiler does not know what will be the actual type and it will be resolved dynamically at runtime and hence compiler will not raise a compile time error here 🙂

I have created another DemoClass which have method ABC.

Main Method 

As you have noticed that I have created an object of DemoClass which contains method “ABC” and passed to CheckDynamicParameter. The demoObj parameter will be resolved at runtime and it will find ABC method in it. Thanks to dynamic type in C# 4.0, otherwise I would have used Reflection here or static typed object 🙂

Let’s hit F5!

image

The beauty is that even if I remove method ABC from DemoClass, the program will still compile because it will expect that when dynamic is resolved at runtime it will find the method ABC and if not found, a Runtime Binder Exception would raised.

That’s all for today! If you want to download Visual Studio 2010 Beta, visit Visual Studio on MSDN

Implicit/Explicit Implementation of Interface in C#

In C#, we have two ways to implement interfaces:

  1. Implicit Implementation
  2. Explicit Implementation

To see the difference, Let’s say we have an interface:

public interface IDemoInterface
{
string Name { get; set; }
void DemoMethod();
}

As we implement this interface, we will notice that Visual Studio or Visual C# Express gives us an option like

If we select Implement Interface ‘IDemoInterface’ (which is implicit one) we will see the generated code like:

class Class1 : IDemoInterface
{

#region IDemoInterface Members

public string Name
{
get
{ //some implementation }
set
{ //some implementation }
}

public void DemoMethod()
{
//method implementation
}

#endregion
}

whereas in contrast if we have selected Explicitly implement interface then we would have seen an implementation like

class Class2 : IDemoInterface
{
#region IDemoInterface Members

string IDemoInterface.Name
{
get { //some implementation }
set { //some implementation }
}

void IDemoInterface.DemoMethod()
{
//method implementation
}

#endregion
}

So you must have noticed that in explicit implementation, the interface name is explicitly mentioned with the property and the method but of course that is not the only difference 🙂

Then what does it mean? and what is the real difference?

Well explicit implementation allows the interface members to be accessible only when the object is cast to interface type 🙂 Means that when I will instantiate an object of Class 1, I will be able to access interface members in contrast to Class 2 in which I have to cast the object to that particular interface. Not clear, consider the code below:

In contrast, when you create object of Class 2 you will not be able to see Interface members

until or unless you cast that object into interface type…

image

When interface members are explicitly implemented, they are not accessible through class instance instead through interface instance.

Beside this another noticeable point is that when implemented explicitly the members’ name become InterfaceName.MemberName and hence multiple interfaces with same member name can implement in the class whereas this is not possible in the implicit implementation. A disadvantage of Explicit implementation is that when used with value types, Boxing can cost performance.

.NET Day

Adil Last weekend I participated as speaker in “.NET Day” – A full day free event for students, organized by DevNext and IEEE-CS (FAST-NU) at FAST- National University of Computing and Emerging Sciences, Karachi, Pakistan.

I’m very glad to see the interest of students and their feedback. I talked on “Visual Studio 2008 Tips and Tricks” and more specifically Visual C# IDE. Visual Studio is one of the most powerful IDE and developers usually don’t know the features available that can really enhance their productivity.

Following were the areas discussed:

  • Code Navigation
  • Refactoring
  • Code Snippets
  • Debugging Techniques
  • Visual Studio SDK

Shield In the end, I got thank you shield from IEEE-CS, FAST-NU 🙂 I would like to thank every one who attended my session and also to IEEE-CS for working in collaboration with DevNext.

For details about the event, please visit “DevNext – .NET Day Event Summary

Microsoft Most Valuable Professional (MVP) Award 2009

On 1st July, I received a fantastic news from Microsoft. It gives me pleasure to share with you all that I have been awarded Microsoft Most Valuable Professional (MVP), in Visual C# area, for my contribution to the community.

I would like to thank all the people who supported me specially my family and friends. Here I would also like to thank two people specially. First, Wajahat Abbas for giving me the boost and encouraging me to participate as speaker on Microsoft and other platforms. Second, I would like to thank Rizwan Ahmed a.k.a RizwanSharp (MVP) from the core of my heart for his support, appreciation, believe and trust in me 🙂 Thank you Rizwan, you anticipated this day quite earlier :$

Finally, Thanks to Microsoft and Ruari plint (MVP Lead) for the award and support. I will definitely improve and will contribute more to the C# community in the upcoming days 🙂

To visit my profile on MVP site, please click here.

For those who don’t know much about MVPs, Here is for you:

“Microsoft Most Valuable Professionals (MVPs) are exceptional technical community leaders from around the world who are awarded for voluntarily sharing their high quality, real world expertise in offline and online technical communities. Microsoft MVPs are a highly select group of experts that represents the technical community’s best and brightest, and they share a deep commitment to community and a willingness to help others.” – MVP website