Monday, January 5, 2009

Partial classes in .Net Framework 2.0

Partial Classes


Today we shall see a new feature “Partial Classes” which was added to .Net 2.0

As the name suggests this allows us to create a part of a class in one file and the rest in another file. There is no restriction to the number of files which we use to code a single class. While declaring a partial class the keyword “partial” should be mentioned in all the files.



Let us see an example of how to declare a partial class.


File1.cs

public partial class MyClass

{

public MyClass()

{

//Constructor code goes here.

}

}

File2.cs

public partial class MyClass

{

//Methods and other implementation code goes here.

}


On compilation the C# compiler automatically detects parts of the partial classes in various files and builds them into a common assembly file.


Practical uses of Partial Classes


  • Split the class when the implementation code is very big, exceeding 1000 lines.
  • Place the properties, variables & constants in one class and the actual implementation in another file.
  • Use a different file for machine generated file and a different one for manual code, this improves readability and avoids the risk of changing the machine generated code by mistake.
  • In general source code is placed in a “Source Safe” environment like VSS or Clear Case, which allows only one person to modify a particular file at any given point in time. When the class file is very large and if different developers need to work on different methods of the class then we can split the class into multiple partial classes and the developers can work on the file which contains their methods.
  • Apart from classes the “partial” concept can also be applied to Interfaces and Structure definition.

While splitting the class into multiple parts we can make one part appear as the key file (usually the file with the implementation) and the other files to appear as its supporting files in the VS.Net editor. To do this we will have to make a few modifications to the project file (.csproj)


MyClass

File2.cs

MyClass



After modifying the project file as above, the solution explorer will display “File2.cs” as a main file and the file “File1.cs” will appear as a supporting file as a child node under “File2.cs”


File2.cs

| File1.cs


Thus partial classes allow us to split the content of a single class into multiple files and VS.Net allows us to organize the files based on priorities.

Thursday, January 1, 2009

Welcome to http://csharp-guide.blogspot.com

Hi

To begin with “Wish you all a Happy New Year 2009 !!!

I am starting this Blog on this New Year on Jan’01 2009



In this blog i am planning to elaborate the various advanced features available in .Net across various version, I plan to use c# as the programming language to explain the new features. Welcome you all to join this journey with me.



The .Net framework has evolved over a period of time, starting with Framework 1.0 version there were a series of version upgrades like 1.1, 2.0, 3.0 & 3.5. Every version upgrade brings with it a range of new features to enhance development and to make life easier. Also every new version brings new features to assist the changing business needs of the world.



It is evident that the features added are to satisfy the changing business needs, for example we see that more and more support is being added to XML and Web based development in each new version on .Net, this is because XML has become an easy and reliable way of sharing data between various heterogeneous systems. Obviously the Web is taking new dimensions with every passing day, eCommerce has brought in revolution in the retail world by enabling end users to purchase any thinks in the world with the click of a button. Due to these changing needs, Microsoft is adding more and more support to Web development and XML in every passing version of the .Net Framework.



To begin with we shall start with a new concept called “Generics” added to .Net Framework 2.0



Generics



In .Net individual objects are type safe, meaning that we cannot just cast an object and assign it to another of a different type, the compiler performs type-safety checks while assigning values to variables.



clsCars objCars = new clsCars();

clsTrucks objTrucks = new clsTrucks();



The following assignment throws an error

objCars = (clsCars) objTrucks;



This type safe checking is restricted to individual objects only; collections do not have this restriction, hence collections can hold any type of objects, for example



ArrayList objVehicle = new ArrayList();

objVehicle.Add(objCars);

objVehicle.Add(objTrucks);



Now we do not have any control over the type of object which is stored in the collection, and there is no way in which we can cast and re-use each of the objects stored in the collection.



This limitation is overcome in .Net 2.0 with the help of Generics, Generics allow collections to be type safe at compile time. The System.Collections.Generics namespace enables use of Generics in .Net 2.0. The System.Collections.Generic, contains the classes that support this concept, like the List, Queue, Stack, LinkedList etc.



Generics are similar to 'Templates' in C++, which makes it possible to design classes which take in a generic type and determine the actual type later on.



As the name suggests Genreics are neutral collection, not bound to any type initially, once a generic is declared we can use it for any datatype, but we can use it to store items of only one specific datatype at any given point of time, thereby making them type safe.



Syntax for declaring Generics



clsMyGereric objGeneric = new clsMyGeneric();



Here T is the datatype which we want the Generic to hold and this can be determined at compile time.



Example



Declaring the Generic Class:

public class clsMyGeneric 
{
  T t;
  public T Val{get{return t;}set{t=value;}}
}



Here ‘T’ is a generic datatype, we can replace ‘T’ with any datatype at compile time.



Using the Generic class:


clsMyGeneric objName = new clsMyGeneric();

//

objName,Val = “Name1”

//

clsMyGeneric objAge = new clsMyGeneric();

//

objAge.Val = 35;



Generic Collections


Now that we have defined individual Generics, we need a Generic collection to store these objects, let us see how to define Generic collections.



System.Collections.Generic.List objNameList
= new System.Collections.Generic.List();



Now we can add any variable of type string to the above defined collection, but remember that we can add only objects of string datatype to the collection, if we try to add objects of a different type then an error will be throws, thereby we have achieved type safe collections in .Net 2.0



objNameList.Add(objName.Val) -- Correct

objNameList.Add(“Name2”) -- Correct

objNameList.Add(objAge.Val) -- Throws a exception, since the value is of type int.



To Summarize, a Generic type/class is one which by declaration is generic/neutral by datatype and can hold any datatype, but only one data type based on how it is initialized, similarly a Generic collection in one which can hold objects of only one datatype and this type can be determined at compile time.


We have come to the end of my first blog post, hope this information was useful to you, keep watching this post for more updates soon ...