December 31, 2009

MSVBRUN, MSVCRT, CLR and now DLR. WTH?

Yes. Many of us will ask the question, what the hell is this? Well, these are MS short-forms for various runtimes. Visual Basic Runtime (MSVBRUN), Visual C++ Runtime (MSVCRT), .NET Common Language Runtime (MSCOREE and MSCORWKS). There are many more runtimes, like Java Runtime Environment (JRE) and alike.

But what is Runtime?

As computers can only understand Zero and One, it becomes tedious to write programs using these bits. Just imagine writing a Notepad or WordPad using 0s and 1s. So, we have so called programming languages like C/C++, VB, Java which allows us to write applications and business logic in developer readable form. Once these applications are written in any of these languages, these applications are compiled into ‘Object Code’ or sometimes ‘Intermediate Code’. However, still these machines are so dumb that they can’t understand this code. So, we need some kind of translator, who can convert the object code into binary which can be understood by these machines. So, basically runtime converts the compiled object code into 0s and 1s and machines executes the command.

But, this is just a small part of these Runtimes. Apart from this code execution, some runtimes are smart enough to provide various services. Below mentioned are some of the services provided by .NET CLR

  1. Automatic Memory Management:
    This is like a waiter in a hotel. He’ll first put empty dishes in front of us to serve food (for example, ‘allocation’ for resources) and will clean the table by removing those dishes after use (which is called Garbage Collection).
  2. Cross Language Support:
    It is just like having your own translator who understands all the languages under the sun. Which simply means, using CLR (and with the help of Common Type System in .NET), developers can easily call, debug, handle exceptions in any .NET supported language without a hassle. That is – language interoperability is possible between C#, VB.NET, etc… without writing any special code.
  3. Security Provisioning:
    CLR also provides a features called Code Access Security [or CAS in MS terms :) ], which checks the code for vulnerability and permissions it has been granted, before it actually executes it. So, if I give you one EXE which is a screen-saver but behind scene it is formatting a hard-drive. Using CAS, you’ll get warning about such behaviour of the code. And you’ll be saved :)

But now, with .NET Fx 4.0, MS is introducing a new term ‘DLR’.

DLR is nothing but Dynamic Language Runtime. Just like CLR helps to execute and manage execution of ‘statically typed languages’ like C# and VB.NET, DLR will help to execute and manage ‘dynamically typed languages’. There are many dynamic languages and we’ve seen and using one dynamic language since a long time. Remember JavaScript? Where var was dynamic. One facet of dynamic languages is they can identify or discover objects during runtime, whereas in case of statically typed languages developers need to define all the types well in advance (i.e. during design time). In case of JS, the variable var can decide the type of object during runtime or execution. e.g.

var x = "5";

alert(typeof x);


image 



Just like that, now .NET supports new languages - IronRuby and IronPython. But, why do we need these languages? What SO dynamic in it?



As MSDN states, following are the advantages of dynamic languages:





  • The ability to use a rapid feedback loop (REPL, or read-evaluate-print loop). This lets you enter several statements and immediately execute them to see the results.





  • Support for both top-down development and more traditional bottom-up development. For example, when you use a top-down approach, you can call functions that are not yet implemented and then add underlying implementations when you need them.





  • Easier refactoring and code modifications, because you do not have to change static type declarations throughout the code.





But, this doesn’t mean that C# and VB are out of business now. Rather,  DLR has provided  ‘dynamic’ type to C# and VB to support dynamic behaviour in these languages. Now, existing .NET languages C# and Visual Basic can create dynamic objects and use them together with statically typed objects. For example, C# and Visual Basic can use dynamic objects for HTML, Document Object Model (DOM), and .NET reflection.



As of now, DLR sits on top of CLR and provides facilities to languages like IronRuby, IronPython, C# and VB.NET.



Because of DLR, there many more opportunities for developers. Its going to provide new ways to address different issues, provide more flexible (or should I say ‘dynamic’) way to write code and build rapid applications.



Resources:




  1. Complete documentation about DLR is available at CodePlex.


  2. MSDN Magazine Article on DLR: here


  3. MSDN library documentation about DLR: here



Namaste



Mayur Tendulkar | www.mayurtendulkar.com

December 22, 2009

New Features in VB.NET

One of my friend, Pradnya, has written a very nice blog-post on what’s new in next release of Microsoft Visual Studio 2010. These are basically Integrated Development Environment (IDE) enhancements and you can find it here.

However, in this blog-post, I’m going to talk about new language or framework features introduced in .NET Fx 4.0 with VB.NET – to be precise.

With the release of VS2008 and .NET Fx 3.5, there were lot of new language enhancements and features extensions to framework, like LINQ, Extension methods, Lambda expressions, Expression trees, Auto-Implemented properties, Object and Collection initializer. However, some of these features were not common to C# and VB. Like Auto-Implemented Properties were not there in VB, but were available only in C#. You can find my blog-post Language Enhancements in C# 3.0 and VB9, which I had written, when VS2008 was released.

With the advent of Visual Studio 2010, .NET Framework 4.0, now these languages will compliment each other. Almost all language features are now common to these languages. Let’s visit some of these new features.

  1. Implicit Line Continuation [In VB only]:
    In VB, each statement ends at the end of line. However, in previous versions of VB, if developers wanted to part the code in multiple lines, they had to use (_) character. Following code snippet will show you this. But, with this release of VB.NET, now developers can write code in multiple lines, without requiring any special character. The code can be written easily as:
    Untitled-2
    However, there are some rules. Developers can’t just start a new line from anywhere in between the code. The next line can be started:
    After a comma, after open parenthesis like “(“ or before closing parenthesis “)”.
    You can find more information about this here on MSDN
  2. Auto-Implemented Property [In VB, Was there in C#]:
    As MSDN says: Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property. So, that means, now I don’t need to create a private variable don’t need to write getter and setter to ‘encapsulate’ it.
    Visual Basic compiler will automatically create this private variable for you. So, you can create a Auto-Implemented property by writing:
    Untitled-3 
    So, one may ask, what will be the name of this private  variable and can I access it? Well, the private variable, also called as ‘backing field’, will have same name as that of property, with underscore (_) prefixed. This field can be accessed in code and in debug window, but will not be available in IntelliSense.
    For more information, visit: MSDN
  3. Collection Initializer [In VB.NET, Was there in C#]:
    In C# 3.0, we had collection initializer. It is nothing but a shor-cut to create and initialize the collection. So, if I use VS1010 with new features in VB.NET, now I can write it like:
    sol2
    And this will create a one dimensional array of strings. So, this is just one another shortest method to create and initialize arrays and collections.
    You can find other useful information here at MSDN

So like, there are some more features to framework, which includes addition of few more classes to Base Class Library (BCL), some enhancements to Garbage Collector, and Dynamic Language support. We’ll see these features soon.

Till then, enjoy coding.

Namste

Mayur Tendulkar | www.mayurtendulkar.com

December 14, 2009

WCF Se Maine Pyar Kiya Tak :)

Today, we’re going to understand a new concept and for that matter, you must be familiar to Bollywood and movies like ‘Maine Pyar Kiya’ (MPK). You can say this as a pre-requisite.

In MPK, there was a situation, where hero and heroine were separated a distance apart. Then heroine used to send ‘Love Letters’ using pigeon postal service. And she used to sing a song for that matter – ‘Kabootar Ja Ja Ja

Untitled-1

Now, Windows Communication Foundation (WCF) is a unified programming model to build distributed applications. In our scenario, Sallu is providing his ‘best love’ services (pyar, daulat, shoharat, etc…) and Bhyagyashree is going to consume those.

Here hero and heroine are at two distant places and we call it as end-points. And, if they want to communicate, they must have ABCs i.e.

  1. A for Address on which she will send a letter, look at Sallu’s address
  2. B for Binding which defines how they communicate – here ‘OnAirBinding’ or Pigeon is used
  3. C for contract, the contract between Sallu and Bhagyashree says, if Bhagyashree wants Sallu to bestow all romantic poems, letters on her, she must send a love letter to him, kind of.

This scenario depicts the basic peer-to-peer communication in WCF (oops Bollywood movies). Here, in this mode of communication Salman is service provider and Bhagyashree is consumer, but sometimes situation can be other way round. And it’s very much possible.

image

In this new picture, what do you understand?

Well, WCF is not just about peer-to-peer applications. But can be used in many different applications as well. In this case Salman is a service provider (or publisher) and Aish, Kat and Bhyagyashree are service consumers (or subscribers). They are registered with Salman for his ‘best’ services.

But to provide these services, everyone needs to have ABCs.

  1. A – Address. Everyone will have a unique address for communication purpose
  2. B – Binding. Everyone will have their own mode of communication. In previous case it was Pigeon service, now it’s Email, Phone, Mobile and what not. WCF provides bindings like Http, TCP/IP, etc...
  3. C – Contract. All these actresses will have their own terms and conditions to work with Salman. And these terms and conditions will be put into contracts.

The main advantage of WCF is – it can communicate with any other technology. That means, WCF can communicate with .NET Managed based applications, Native applications, J2EE based applications.

To consume WCF services, client creates a proxy object of the service (mostly called as ‘ServiceClient’). In our scenario, all the gals will be having their own ‘personal copy’ of Salman Khan; however, original Salman is totally different. It’s like Salman will be having clones to enjoy himself with his girlfriends. Remember Dev Anand in ‘Teen Devian’? Nice idea, if we could implement in our world.

Probably, I’ll share some technical examples in next few posts. But, after this, you can see the how WCF works.

Enjoy.

Namaste

Mayur Tendulkar | www.mayurtendulkar.com

December 7, 2009

Must Read Guest Post – Electrical Concepts in Bollywood

I always wanted to share this story with you all. The story is about the analogies, or to be precise Latch-On, I've made for electrical concepts. This happened, when I was pursuing my Diploma in Electrical Engineering, on a Teacher's day, one of my teachers asked me to teach something to the students. And rashly, I thought of this story and everyone liked it. Now, I always make such latch-ons for everything which I want to remember for life.

In electrical, we talk a lot about positive terminal, negative terminal, potential difference, direction of current, and direction of electrons, resistance and what not. Let's see, how our Bollywood movies represent electrical engineering.

In a typical Bollywood film, there is this Hero, Shahrukh Khan (A Positive terminal or pole), who is always attracted to this heroine called Kajol (A Negative terminal or pole). And as you know, guys always run behind girls. Just like that Shahrukh was always running behind Kajol. Yes, you can see lots of movies of them, K3G, KKHH, DTPH, DDLJ and many more. If you're a movie freak, you'll understand what I mean when I say K3G.

And here in electrical engineering current flows from positive terminal towards negative terminal.

Now, there are some movies, in which Shahrukh had a one sided affection towards Kajol and Kajol was not at all interested in him. This is doleful one sided affair. And can be found in movies like Anjam, Darr, etc… In case of electrical engineering, Diode represents this one side affection. When Diode is used, current can flow ONLY in one direction. Either Hero loves a Heroine OR Heroine loves a Hero. No two sided traffic is allowed.

However, there were some movies like Sapaney (at that time called as Right Angled Triangle of Love) and Duniya Dilwalon Ki (at that time known as Equilateral Triangle of Love), which has one heroine and two heroes chasing her. No wonder, I find the similarities in Transistor, which is a three terminal (P-N-P) device. And may be sometime N (heroine in this case) more infatuated with left P (Prabhudeva) or sometimes left P (Arvind Swami).

Now, in typical Bollywood film, there is villain, ladaki ka baap. Yes, girl's father is always against the love life of these kids. In electrical engineering we have same effect called Resistance, which actually resists OR opposes the flow of current i.e. from hero chasing his dream girl or vice a versa.

Now, there are some effects of electric current. These effects are magnetic effect and heating effect. Well, as hero and heroine are attracted towards each other, represents the magnetic effect of their current, oops, should I say love? And for heating effect, I better close my mouth over here and give you an opportunity to think about. And build more concepts from here. :P

Seriously, my funda is if you want to learn something, you need to make it look like with your daily life. Or at least with the things you enjoay, may be its food, movies, whatever you enjoy the most. Just like I made this resemblance of Bollywood and Electrical, I've another analogy of Microsoft .NET framework and hotel industry. You can check it out in my previous posts.

You need to make it weird; you need to make it simple. This will help you to clear your concepts and understand the things more transparently. Mugging up is not the solution.

I hope you enjoyed it.

Namaste

Mayur Tendulkar http://www.mayurtendulkar.com/

December 2, 2009

Language Wars

When I started programming, one of my friend and I always had a fight over programming languages. He used to be a nerd and preacher of C++ language whilst, I'm being tenderfoot, always favored Visual Basic aka VB.

He insisted that, being a machine level, 'PURE' language, C++ is the most powerful while I believed that VB is most simple, easy to understand and hence can deliver the commands more transparently. Hence, VB is more powerful. But indeed, these were kid-fights.

But, I can see these fights and they still exist in industry too. Many people debate over C#, VB, Ruby, Python and many more languages. However, when it comes to C# and VB.NET, how does it matter? Both of these languages are going to target the same CLR, use the same CTS and the same BCL. And at the end of the day, it's going to be MSIL. Or to go at deeper level, it's just going to be 0s and 1s.

Let's say, if you go to the hotel and order the food in Marathi or Hindi, you're going to get the same thing, which you've ordered. So, if you ordered Pizza, you're not going to get Dosa.

Nevertheless, guys, it's not about the power or purity or level of a language, but it's always about the ease. How easily, I can order the system to perform some tasks? It's the matter of simplicity. If someone is trained on English since childhood, definitely, he'll prefer English in conversation. Same ways, if you know VB since ages, you'll prefer VB in your projects. However, there can be some business reasons behind choosing a particular language.

But, in .NET parlance, it really doesn't matter, whether you choose VB or C#. Though, previously there were few features available in C# only (like Automated Properties) are now available in VB as well (like Line continuation). And just like that the features dominated by VB are now integral part of C#.

With the release of Visual Studio 2010 and .NET Framework 4.0, developers will have a CHOICE of language. Most of the features are common to both, VB and C#. In subsequent posts, we'll see the new features introduced in these languages. Meanwhile you can revisit my previous entries to see what was new in previous versions on C# and VB.

I remember one story of Akbar and Birbal and would like to narrate it over here.

One day a guy came to Akbar's darbar and challenged Birbal to guess his mother tongue. He was so fluent in every language, that every one was shocked. Every one was worried about, how Birbal is going to guess that guy's mother tongue.

But friends, wait a sec. He is Mr. Birbal.

At night, when everyone was sleeping, Birbal went to Akbar and said, "Boss, do you wanna know that guy's mother-tongue, then come with me". Both of them went to the guest house, where that guy was enjoying a sound-sleep.

Suddenly, Birbal poured a bucket full of water on that guy's head and that guy woke up and shocked. The first word came out of his mouth was "You A****E".

Birbal said, "Bingo!!! Boss, this is your mother tongue. Whenever people get hurt, delighted or want to express their emotions/feelings, they use their mother tongue."

And thus, once again Birbal won this challenge.

I hope you enjoyed this story. J

Namaste

Mayur Tendulkar | www.mayurtendulkar.com