Language Enhancements In C# 3.0 and VB 9

There are so many new features added as Language Enhancement for C# 3.0 and VB 9.0 in Visual Studio 2008 (along with .NET Framework 3.5). Here, I’m compiling a quick list of important ones and will share some resources from where you can learn more about the same.

  1. Local Type Inference
    Visual Studio uses type inference to determine the data types of local variables declared without data-type. The compiler infers the type of the variable from the type of the initialization expression. This enables you to declare variables without explicitly stating a type, as shown in the following example.
    String str1 = "Mayur Tendulkar";
    var str2 = "Mayur Tendulkar";

    For more information visit following links:
    http://msdn2.microsoft.com/en-us/library/bb384937.aspx

  2. Object and Collection Initializers
    Object and Collection Initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor
    class Person
    {
    String Name { get; set; }
    String EMailID { get; set; }
    }
    class PersonImplementation
    {
    static void CreatePerson()
    {
    //Object Initializer Used
    Person p = new Person { Name = "Mayur Tendulkar", EMailID = "mayur.tendulkar@gmail.com" };
    //Collection Initializer (with Object Initializer) Used
    List per = new List
    {
    new Person() { Name = "ABC", EMailID = "abc@abc.com"},
    new Person() { Name = "XYZ", EMailID = "xyz@xyz.com"}
    };
    }
    }

    For more information, visit following link:
    http://msdn2.microsoft.com/en-us/library/bb384062.aspx

  3. Anonymous Types
    Anonymous types enable you to create objects without writing a class definition for the data type. Instead, the compiler generates a class for you. The class has no usable name, inherits directly from Object, and contains the properties you specify in declaring the object. Because the name of the data type is not specified, it is referred to as an anonymous type.
    var Student = new { Name = "Mayur", LastName = "Tendulkar"};

    For more information, visit following link:
    http://msdn2.microsoft.com/en-us/library/bb397696.aspx

  4. Auto Implemented Properties
    Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field can only be accessed through the property’s get and set accessors.
    class Female
    {
    string Name { get; set; }
    int Age { private get; set; } //Write only
    int Mobile { get; private set; } //Read only
    }

    For more information visit following link:
    http://msdn2.microsoft.com/en-us/library/bb384054.aspx

  5. Partial Method
    Partial methods enable the implementer of one part of a class to define a method, similar to an event. The implementer of the other part of the class can decide whether to implement the method or not. If the method is not implemented, then the compiler removes the method signature and all calls to the method. Therefore, any code in the partial class can freely use a partial method, even if the implementation is not supplied. No compile-time or run-time errors will result if the method is called but not implemented.
    // Definition in file1.cs
    partial void onNameChanged();

    // Implementation in file2.cs
    partial void onNameChanged()
    {
    // method body
    }

    For more information visit following link:
    http://msdn2.microsoft.com/en-us/library/wa80x488.aspx

In this blog post we seen 5 new concepts that are included in VS2008 (.NET 3.5) and in next post, we’ll see some more of them.

State and Notifications with Windows Mobile

In last post, we got to know about .NET framework using a real-life example of Hotel Industry. There are so many technologies that help us to make our life much easier and hence we can take real life examples to understand them – inside out. I love to explain some of them as I encounter few of them in my day-to-day life – as a hobbyist programmer, as a student, as an intern and as an individual. So, it will help you to understand those “geeky” worlds easily, even if you are novice.
Here, in this blog roll, I’m going to talk about THE BEST feature that is available in Windows Mobile platform – State and Notification APIs OR you may heed it as SNAPI when you will be surrounded by geeks.
Let’s take an example over here. Let’s suppose you are hungry, and then you may open the refrigerator to check, if anything is there to eat. This is like you are checking the STATE of your refrigerator.
But sometimes, even if you’re not hungry and if your mom is leaving for her job, he may tell you, that, there is ice-cream in the fridge, if you want, take it. In this case, she is NOTIFYING you about it.
There are many such events happen on your Windows Mobile that you may want to know, when those happened, what is the status of your device then, like that. SNAPI helps you to monitor such things.
Let’s take a simple example of Incoming Call. Suppose, you want to receive a Message from your device, whenever there will be an Incoming Call. Ok. When there will be an incoming call, you’ll get notified by ringtone or by vibrations. But, here, just to understand the technology, let’s take this example.
So, there are few steps that you need to follow, when building SNAPI based application.
Here, I’m writing a code, which will give you the idea about how you can build such kind of application.

Imports Microsoft.WindowsMobile
'This namespace is required to run Windows Mobile application.
Imports Microsoft.WindowsMobile.Status
'This namespace is required to use SNAPI classes and functions.
Public Class frmMain
Dim state As SystemState = New SystemState(SystemProperty.PhoneIncomingCall)
'First Create object of SystemState and provide PhoneIncomingCall property as a parameter
'Then create a method, which will be called for evey notification OR state change
Private Sub IncomingCallAlert()
'This method is supposed to be called everytime, when there will be new Incoming Call
If MessageBox.Show("Incoming Call...") = Windows.Forms.DialogResult.OK Then
'If there is an Incoming Call, then just show a Message Box with Alert
Me.Hide()
'After showing, hide the form.
End If
End Sub
'Now launch the State and Notification service, whener application is launched.
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Start the notification process, when main form is loaded
AddHandler state.Changed, AddressOf IncomingCallAlert
'Add event handler, so whenever, there will be incoming call,
'IncomingCallAlert() method will be called.
End Sub
End Class

You can find more information about SNAPI on following links.
Besides the example, you can do much more things with SNAPI. To know more about SNAPI on MSDN,
click here
You can get more information about SNAPI properties like PhoneIncomingCall and others, for this
click here.
There is a webcast on building SNAPI enabled Windows Mobile Applications on MSDN Events. To view this webcast
click here.
There is a very nice article written by Don Sorcinelli, to read,
click here.
I hope, this will give you a clear idea about SNAPI and help you to build nice applications for Windows Mobile.
Happy Coding J

.Net With Example

In my territory, people know me as Mr. Example. I like to learn technology by referring some real world examples. And believe me, its fun. Whenever I see any new technology, I try to find some examples which will better express that technology. In this blog entry, I would like to elucidate .Net Framework, the 7 year old technology, which surely has altered our lives. I’ll explain it with respect to hotel business. And it’ll be fun. Yes. I mean it.

Once you go through this blog, you’ll never ever forget, ‘what is .Net framework.’ So, without wasting much time, let’s start quickly.

I presume that the reader of this blog has a basic understanding of Object Oriented Programming Concepts, like classes, objects, types, etc… Before moving ahead I would also like to tell you that framework is nothing but the methodology or guidelines set forth to execute any task. Hence Microsoft .Net Framework is nothing but a methodology which can be implemented to build applications. Now, Runtime is a host which will allow us to execute the application. To run any kind of application we require a runtime or an environment. In .Net it is Common Language Runtime or simply CLR. In proceeding example, our runtime or the host environment is a hotel or restaurant. And we’ll see different functionalities of CLR inside this hotel.

Whenever, we visit any hotel or restaurant we’re served by people, who execute different roles, like Gate Keeper – who opens the door and invite us with a pleasant smile, Reservations’ Manager – who checks for reservations or seating arrangements, Captain – who takes the order, Waiter – who will serve the food, Cleaner – who cleans the dishes, etc… These different roles represents the Common Language Runtime i.e. CLR in .Net parlance. How? Let’s see.

When you are at the door, Gate Keeper welcomes you. Same ways, when you double click any application written in .Net, the .Net Framework runtime will create a process for it. That means, it will start to execute that application.

When you enter into restaurant, Reservations’ Manager, with a smile on his face, asks you about reservations, if you have made any. And if not, how many people are with you? Where you would like to seat in the restaurant premises e.g. pool side, in bar, in lounge etc. Same ways, .Net framework, will allocate memory for your application to run, which depends on how many variable (or objects) you have used in your application. So, if you have created one integer object and one string objet, it’ll allocate a space for two objects in the memory. That means it’ll do the seating arrangements.
Once you take the seats, the MENU will be provided to you by Captain. This menu is like Base Class Library (BCL). Sometimes it is also called as Framework Class Library (FCL). So, as you have different food items in MENU, you have different types and structures in BCL. So, you choose items from menu and customize your own dish, likewise you choose different types (or classes) to build your application. Also, as you can add pinch of salt ‘n pepper to make the dish delicious, you can create your own types (or classes) which will allow you to fully customize your application.

Now, you are ready to order. In this case, you order some food items and Captain takes note of it. He then forwards this order to Chef. Now imagine, Captain has written the order in English and Chef doesn’t understand it. What will happen? But this is not the case. In hotel, although there can be staff members who knows various languages, there will be one common language, common code that everyone follows. So, it’s for sure, that if you ordered a Pizza, you’ll receive a Pizza. Just like that, in .Net Framework, irrespective of language chosen for development purpose (e.g. C#, VB.Net, etc…) all code is converted into Microsoft Intermediate Language (MSIL) and CLR understands this MSIL.

So, when you order Pizza, this order is taken by Captain (say in English) and forwarded to Chef in a format that he understands (say in French) and ultimately you’ll receive the same output. In the same fashion, you write a code in C# which is converted to MSIL while compilation and when you execute the application, the MSIL is converted to a language which machine (on which application is running) can understand. That is 0s and 1s of machine language.

Now, it’s time for Waiter to serve the food on table. So, CLR will execute the code and you’ll be able to see output on your machine.

In small restaurants, one Waiter serves two or more tables. There can be many reasons to it, e.g. less number of staff. This might incur into interchanging of orders. Sometimes, Waiter may get confused and serve the wrong meal. This phenomenon in technology is called DLL Hell. In this case, you include some resources (like classes which are stored in dynamic link library files i.e. dll files) and if the runtime doesn’t find that resource or any link to that dll file, then it’ll give you the Blue Screen of Death (BSOD). This terminology was called DLL Hell. But in .Net this issue has been resolved. In .Net every application executes in its own world with all the required resources kept in the same folder. So, there is no chance of missing any resource.

So, when you are finished with any particular dish, Waiter or Cleaner will clean the table to accommodate any new dishes you have ordered. Likewise, in CLR, has a nice feature which is called Garbage Collector. So, when you use objects of any types (or classes) to customize your application, while execution CLR reserves memory for them. This Garbage Collector, looks for unused objects. If it finds something, it’ll clear those objects and provide more memory to your application.

As you can call a Waiter to clean the table you can call Garbage Collector to clean the memory resources. It’s pretty simple.

If during your stay in the restaurant, if you find something irritating, you call Manager and report it. In the same way, you can use Exception Handling, which will report you, if there is any error in the application execution.

So, at last, when you’re done with your lunch or dinner you are ready to leave the restaurant. Same ways, when the execution is over, CLR will terminate the application. This will also remove any objects (that you have used in your application) from memory.

Here, we have seen, what are the different features that Microsoft .Net Framework provides us with the help of Hotel example.

In next blog, I’ll come up with some new technology – with some new fundas…

Happy Coding :)