Monday, September 26, 2011

Internals of .NET Objects and Use of SOS

Well, now getting deeper into the facts, lets talk about how objects are created in .NET and how type system is laid out in memory for this post in my Internals Series.  As this is going to be very deep dive post, I would recommend to read this only if you want to kill your time to know the internal details of .NET runtime and also you have considerable working experience with the CLR types and type system.


Recently I have been talking with somebody regarding the actual difference between the C++ type system and managed C# type system. I fact the CLR Type system is different from the former as any object (not a value type) is in memory contains a baggage of information when laid out in memory. This makes CLR objects considerable different from traditional C++ programs.

Classification of Types

In .NET there are mainly two kind of Types.
  1. Value Types (derived from System.ValueType)
  2. Reference  Type (derived directly from System.Object)
Even though ValueTypes are internally inherited from System.Object in its core, but CLR treats them very differently.  Indeed from your own perception the Value Types are actually allocated in stacks (occationally) while reference types are allocated in Heaps. This is to reduce the additional contension of GC heaps for Heap allocation, GC cycles, occasional call to OS for additional memory needs etc. The object that is allocated in managed Heap is called Managed Object and the pointer that is allocated in stack to refer to the actual object in heap is called Object Reference (which is sometimes called as Managed Pointer). 

Saturday, September 24, 2011

Internals of Interface and its Implementation

As many of my followers requested me to write few things that I missed out from the Internals Series, I should continue with it. In this post, I will cover the internals of Interface implementation and mostly talk about explicit interface implementation, as most of the developers seems to be in confusion with it. I hope you will like the post.

Beginning from the basics, Interfaces are the most important part of any application. Interfaces are language construct that does not implement anything but declares a few members upfront. Generally we use interfaces to create a contract between the two or more communication agents. Another important thing that everyone would be knowing already, Interfaces are meant to be implemented. That means whenever you are creating a class, all the members that were there in the interface are meant to be implemented completely. .NET (or probable any other standard language) disallows the creation of objects on types that are not fully defined. Hence abstract classes also coming into play here. They are classes that have few members undefined or abstract. Once you don't have concrete implementation, you cannot create an instance of a type. Notably, you can say "Interface is a types that does not belong to the System.Object or implement it when it reside inside an assembly". But ironically you could also says that once the type is implemented, it would probably inherit from System.object by default.


Monday, September 12, 2011

Writing a Custom ConfigurationSection to handle a Collection

Configuration is one of the major thing that you need to keep in mind while building any application. Either its an Windows Forms application or a Web site, configuration file is always needed. We write all the configuration that are needed to be changed after it is being deployed in confugration files. It is an XML File which lists all the configuration blocks itself and also allows you to define your own custom configuration sections yourself. Today I am building my own custom configuration section and show how easily you can build yourself.

While dealing with Configurations, there are two things that you need to address

  1. ConfigurationSection
  2. ConfigurationElement
  3. ConfigurationElementCollection

For most of the simple configurations, it is pretty much common to use these two classes, but when you need more complex configuration block, like appsettings which actually puts a Collection of ConfigurationElements, you might need to use ConfigurationElementCollection to hold the collection of ConfigurationElement. 

Thursday, September 1, 2011

31 Tips for the month on Threading, WPF, MEF, ASP.NET


Daily .NET Tips is aiming to sharing useful coding tips and tricks for .NET Developers. This site completely design for sharing Tips and Tricks, useful Code Snippet which anyone use in daily development work and targeted anything related with .NET.   This month we have wide range of tips including Threading, MEF, WPF and MVC. In this post I am quickly listing down all the tips which are published over the month August 2011. And the most important point to mention, among those 31 tips that has been posted on DailyDotnettips this month 22 is coming from me. 

  1. Working with AggregateCatalog in MEF
  2.  Working with DirectoryCatalog in MEF
  3. Working with AssemblyCatalog in MEF
  4. Working with TypeCatalog in MEF
  5. How to use DebuggerTypeProxy while debugging your sensitive Type
  6. ASP.NET Calendar Control as Outlook Calendar
  7. Initialize assemblies using PreApplicationStartMethod for ASP.NET 4.0 Application
  8. Async Lambda Expression
  9. Playing with ASP.NET List Controls using jQuery
  10. How to allow user to input html in ASP.NET MVC?
  11. Using Mutex to avoid deadlocks
  12. Writing inline Code in WPF
  13. What is SynchronizationContext all about
  14. ThreadLocal storage in .NET
  15. Barrier in .NET 4.0
  16. Use of Interlocked in Race Condition
  17. Call ASP.NET Page Methods using your own AJAX
  18. Use of SpinLock for ThreadLocking
  19. Using ReaderWriterLock over Monitor for Thread Locking
  20. What is the use of IsBackground property of Thread?
  21. Enable Address Level Debugging in Visual Studio
  22. Get List of all Control Types in WPF
  23. How to apply simple faded transparent effects on WPF controls ?
  24.  Use Visual Studio Server Explorer to Add New or Existing SQL Server CE Database
  25. Co-Ordinated Thread Shutdown with and without using CancellationTokenSource
  26. Generate thousand of request
  27. Writing a Stretchable ContentControl in WPF
  28. How to Retrieve WPF Visual Tree Programmatically ?
  29. What is Visual Tree and Logical Tree in WPF?
  30.  Dealing with HWND in WPF
  31. Hosting a WPF control inside a Windows Form
To get regular updates visit http://dailydotnettips.com and follow @dailydotnettips at Twitter.

I hope you would like the posts. Stay tune for more. 

Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!