Blog Stats
  • Posts - 104
  • Articles - 0
  • Comments - 162
  • Trackbacks - 383

 

Partial Classes with C# 2.0

With C# 2.0 it is possible to split definition of classes, interfaces and structures over more than one files.
This feature allows you to do a couple of fancy things like:

1- More than one developer can simultaneously write the code for the class.

2- You can easily write your code (for extended functionality) for a VS.NET generated class. This will allow you to write the code of your own need without messing with the system generated code.

There are a few things that you should be careful about when writing code for partial classes:

1- All the partial definitions must preceede with the key word "Partial".
2- All the partial types meant to be the part of same type must be defined within a same assembly and module.
3- Method signatures (retrn type, name of the method, and parameters) must be unique for the agregated typed (which was defined partially). i.e. you can write default constructor in two separate definitions for a particular partial classe.

Now here is the code:

File 1:

public partial class myPartialClass

{

/*

/// <summary>

///

/// </summary>

public myPartialClass()

{

Console.WriteLine(" I am in partial class in other Partial.cs");

}*/

public myPartialClass(string pString)

{

Console.WriteLine("I am in a partial class in Partial.cs. The parmeter passed is: " + pString);

}

public void doSomethingElse()

{

Console.WriteLine(" I am in Partial.cs ");

}

}

File 2:

public partial class myPartialClass

{

public myPartialClass()

{

Console.WriteLine(" I am in a partial class in Program.cs");t

}

public void doSomething()

{

Console.WriteLine(" I am in Progam.cs ");

}

}

class TestProgram

{

static void Main(string[] args)

{

/// see the classe are partial but the object is complete.

myPartialClass myCompleteObject = new myPartialClass();

myCompleteObject.doSomething();

myCompleteObject.doSomethingElse();

Console.ReadLine();

}

}


 


Feedback

# re: Partial Classes with C# 2.0

Gravatar Short and sweet article 1/20/2005 2:55 AM | Pankaj Chitriv

# re: Partial Classes with C# 2.0

Gravatar "you can write default constructor in two separate definitions for a particular partial classe."

This line s/d be "you can not write default constructor in two separate definitions for a particular partial classe."

??

1/27/2006 8:17 AM | Sanjay

# Partial Classes with C# 2.0

Gravatar I like it. 3/2/2006 5:04 PM | Dinesh

# re: Partial Classes with C# 2.0

Gravatar Hi
short and sweet...
you should visit following URL

http://msdn.microsoft.com/msdnmag/issues/04/05/C20/

5/4/2006 12:22 PM | An Indian

# re: Partial Classes with C# 2.0

Gravatar good Example ....
thank u
Raj 6/21/2006 9:00 AM | Raj

# re: Partial Classes with C# 2.0

Gravatar thanks

good example u have set
plz reward back with the implementation of abstract class or method along with partial class

Ashis & Sangeeth 8/16/2006 12:58 PM | Ashis & Sangeeth

# re: Partial Classes with C# 2.0

Gravatar NICE AND SHORT TO VIEW IT 8/25/2006 9:29 AM | Earnest Fantin

# re: Partial Classes with C# 2.0

Gravatar teri maa ki chut 8/28/2006 11:56 AM | vivekf

# re: Partial Classes with C# 2.0

Gravatar Do incorporate the user comments
"you can not write default constructor in two separate definitions for a particular partial classe" 1/12/2007 2:49 PM | NALINA K N

# re: Partial Classes with C# 2.0

Gravatar Good but need more in advantage and disadvantage about it. 7/29/2007 11:50 PM | Amit Das

# re: Partial Classes with C# 2.0

Gravatar is it works same in web based application. i m new to Visual Studio , need more infromation 9/9/2007 10:20 AM | alfa

# re: Partial Classes with C# 2.0

Gravatar Good article on Partial classes in a short. Thanks 12/26/2007 4:44 AM | Harish Chandra

# re: Partial Classes with C# 2.0

Gravatar Nice explanation on Partial Class. Thank you very much 2/11/2008 2:55 AM | Sethu Manickam

# re: Partial Classes with C# 2.0

Gravatar Thanx its good for Beginner to understand the concept of partial class 2/15/2008 1:13 PM | Rakesh Garg

# re: Partial Classes with C# 2.0

Gravatar good but when i will use myself and understand then i will say it is very good 4/1/2008 11:56 PM | ram

# re: Partial Classes with C# 2.0

Gravatar Very Nice Piece of Explanation...Thanks, Keep it up. 5/12/2008 6:14 AM | Manoj

# re: Partial Classes with C# 2.0

Gravatar It will suport with C# 1.0 alsoplz explain me if i m using in a C# console application 10/4/2008 6:11 AM | preeti

# re: Partial Classes with C# 2.0

Gravatar Gr8, Thanks 10/15/2008 3:03 AM | navnit

# re: Partial Classes with C# 2.0

Gravatar Good article , Thanx
11/7/2008 10:36 PM | manish

# re: Partial Classes with C# 2.0

Gravatar good one :) 12/9/2008 10:20 PM | ashu

# re: Partial Classes with C# 2.0

Gravatar simple and easy to understand. cute article 2/22/2009 11:40 PM | chinna

# re: Partial Classes with C# 2.0

Gravatar This is very useful for me , nice article 2/24/2009 12:25 PM | Senthil Athi

# re: Partial Classes with C# 2.0

Gravatar Very simple and fast learning!!! 3/16/2009 3:35 AM | Arun

# re: Partial Classes with C# 2.0

Gravatar Tu gadha hai 3/17/2009 5:23 AM | TriKumat

# re: Partial Classes with C# 2.0

Gravatar good explained.....
thanks 3/24/2009 7:39 AM | shraddha

# re: Partial Classes with C# 2.0

Gravatar good example....useful 3/30/2009 12:41 PM | aditya

# re: Partial Classes with C# 2.0

Gravatar It is very helpful to gain knowledge about the partial classes 4/6/2009 2:36 AM | Amila Nandana

# re: Partial Classes with C# 2.0

Gravatar Good :)

5/27/2009 4:16 AM | Devipriya

# re: Partial Classes with C# 2.0

Gravatar Nice one .. Good 6/24/2009 2:15 PM | Suresh Padaga

# re: Partial Classes with C# 2.0

Gravatar Thank you for a clear and concise explanation. Just one thing... please do change the line stating: "you can write default constructor in two separate definitions for a particular partial class". Others have balked at this typo, too. It can be a bit unnerving when one first reads it.

Still, thanks all the same. 6/25/2009 9:02 AM | achim

# re: Partial Classes with C# 2.0

Gravatar This article is really short and awesome.
Mainlly i impressed with your example because it is very simple and a layman also can understand this concept.
I am looking forward same type of articles for diffrent topices from you.

7/31/2009 3:01 AM | Ravindra Singh rathore

# re: Partial Classes with C# 2.0

Gravatar cool dude 9/9/2009 4:46 AM | hyper

# re: Partial Classes with C# 2.0

Gravatar What will be the output of this code will add more to the article 9/30/2009 12:19 AM | Deepti

# re: Partial Classes with C# 2.0

Gravatar This really good article to learn partial class 11/23/2009 9:09 PM | Arun

Post a comment





 

 

 

Copyright © INETA Pakistan