Thursday, April 18, 2013

Some Windows Forms magic in .NET

1. Multithreading support – While there are many other excellent features of the language, multithreading support is what tops my list. The reason is that the threading mechanism is so much intuitive and easier to implement than many other languages like C++ or even java.
What other language allows you to start a thread, then pause it for 5 seconds, then wait for 7 seconds for it to complete, then instantly terminate it if it doesn't complete and implement this in just 4 lines of code!

Other multithreading features like mutex, lock, Monitor and Event are just as intuitive to use. The end result is that even the average programmer who doesn't have an iron grip over thread programming ends up coding a pretty decent multithreaded solution in C#.


2. Rich Framework Library – Though theoretically C# is a language in its own right, it is almost inseparable with the .NET Framework that it is so closely integrated with. Yet another reason I prefer C# and .NET so much is that it comes pre-bundled with such a comprehensive library (Framework Class Library), that you hardly ever need to look elsewhere to implement any feature you want – be it anything:
  1. Networking/Socket programming (System.Net and System.Net.Sockets)
  2. File I/O (System.IO)
  3. Graphics (System.Drawing)
  4. Multithreading (System.Threading)
  5. Remoting (System.Runtime.Remoting)
  6. Database interaction (System.Data, System.Data.SqlClient)
    Moreover, the library integrates so well with Windows environment that you never have to worry about performance as it happens in case of Java. You can concentrate entirely on writing your business-logic without worrying about the underlying low-level plumbing of these things which .NET does seamlessly for you.
3. Elegance in language – Especially for someone who admires the compactness and preciseness of the C family of languages and at the same time, is also familiar with the rapid development cycles of Visual Basic language, C# offers an excellent blend of the two worlds. In fact, this was the very purpose when C# was initially designed in 1999. To bring the elegant syntax of C++ to VB programmers, and to bring the RAD nature of VB to C++.


4. Delegates – Though not many people are very fond of this feature, in my humble opinion, the language allows you to do almost everything you can do with pointers in C/C++ without the corresponding risk associated with referencing/dereferencing with pointers. With delegates, you can define a simple prototype for a function, and then use it as a custom datatype that points to any actual function that matches the prototype.



5. Automatic memory management: Though it is not a feature of C# language per se, I cannot overstate its importance nevertheless. I still remember how tedious it is to manage memory in C language using malloc, realloc and dealloc from my college days. Though the situation improved with Visual Basic, the solution wasn't quite robust since each VB program ran in its own unmanaged process and any loopholes in a single part of your program can bring the whole thing crashing down. Such things no longer happen in C#. The CLR takes care of allocating memory to your objects and there is a garbage-collector which automatically disposes any objects you forgot to set as null. So graceful!! (Though a caveat is that this grace isn't quite available when dealing with COM components and you have to manually call Marshal.ReleaseComObject() to dispose your COM objects, but thats another story).

6. Networking, Sockets and Stuff: At work, I have to deal with several networking projects that typically involve socket programming – sending/receiving a bunch of raw binary data on specific ports using TCP/UDP sockets. I can never imagine the situation if there were no C# to help by providing its feature-filled classes in System.Net and System.Net.Sockets namespaces that do the job seamlessly. Here are some of the classes that I frequently use:

    1. HttpWebRequest - An easy to use wrapper that handles all the low level plumbing required to create an Http web request. All you have to provide is the url as input.

    2. HttpWebResponse - Once the request is created, calling GetReponse() on the request object will return HttpWebResponse which is equally helpful. This object has GetResponseStream() which gives a NetworkStream object from which we can seamlessly read the received data in whatever format we like viz. raw-bytes,  strings, xml, etc.

    3. TcpClient, TcpServer, Socket - Sometimes, the scenario is not as simple as sending a simple web request and reading the response. If you want to handle all the low level details yourself, then these three classes will be the most helpful to you.

Lately, I'm working on an opensource project called scavenger, an internet download manager coded in C#. I've made extensive use of all these classes in that. 


7. WinForms, WPF and Stuff: Using WinForms is a breeze if you have ever developed anything in Visual Basic 6 or a similar tool. The most useful thing about WinForms is its rich set of .NET controls ranging from TreeViews, ListViews, Progressbars, Toolbars, Split containers, tabbed containers, layout panels – you name it. GUI development was never that easier. I haven't used WPF quite, but from what I've heard, I surmise that it must be same there as well.

5 comments:

  1. How to download vb.net?
    Where is the link?

    ReplyDelete
  2. Here is the link for visual studio downloads:

    http://www.microsoft.com/visualstudio/eng/downloads

    ReplyDelete
  3. Hi Prahlad,

    Cool blog ,nice article in very detail .Thanks for sharing the link.





    Dotnetnuke cms








    ReplyDelete
  4. thank u for sharing such good and valuable information on DotNet...a good training center for DOTnet Training.

    online training for Dot Net

    ReplyDelete



  5. Hi there, awesome site. I thought the topics you posted on were very interesting. I tried to add your RSS to my feed reader and it a few. take a look at it, hopefully

    I can add you and follow.


    Dot Net Training in Chennai

    ReplyDelete