Geeks With Blogs
INETA Pakistan Blogs Home of INETA Geeks

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();

}

}


 

Posted on Sunday, November 28, 2004 1:45 PM | Back to top


Comments on this post: Partial Classes with C# 2.0

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Short and sweet article
Left by Pankaj Chitriv on Jan 20, 2005 2:55 AM

# re: Partial Classes with C# 2.0
Requesting 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."

??

Left by Sanjay on Jan 27, 2006 8:17 AM

# Partial Classes with C# 2.0
Requesting Gravatar...
I like it.
Left by Dinesh on Mar 02, 2006 5:04 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Hi
short and sweet...
you should visit following URL

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

Left by An Indian on May 04, 2006 12:22 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
good Example ....
thank u
Raj
Left by Raj on Jun 21, 2006 9:00 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
thanks

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

Ashis & Sangeeth
Left by Ashis & Sangeeth on Aug 16, 2006 12:58 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
NICE AND SHORT TO VIEW IT
Left by Earnest Fantin on Aug 25, 2006 9:29 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
teri maa ki chut
Left by vivekf on Aug 28, 2006 11:56 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Do incorporate the user comments
"you can not write default constructor in two separate definitions for a particular partial classe"
Left by NALINA K N on Jan 12, 2007 2:49 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Good but need more in advantage and disadvantage about it.
Left by Amit Das on Jul 29, 2007 11:50 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
is it works same in web based application. i m new to Visual Studio , need more infromation
Left by alfa on Sep 09, 2007 10:20 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Good article on Partial classes in a short. Thanks
Left by Harish Chandra on Dec 26, 2007 4:44 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Nice explanation on Partial Class. Thank you very much
Left by Sethu Manickam on Feb 11, 2008 2:55 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Thanx its good for Beginner to understand the concept of partial class
Left by Rakesh Garg on Feb 15, 2008 1:13 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
good but when i will use myself and understand then i will say it is very good
Left by ram on Apr 01, 2008 11:56 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Very Nice Piece of Explanation...Thanks, Keep it up.
Left by Manoj on May 12, 2008 6:14 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
It will suport with C# 1.0 alsoplz explain me if i m using in a C# console application
Left by preeti on Oct 04, 2008 6:11 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Gr8, Thanks
Left by navnit on Oct 15, 2008 3:03 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Good article , Thanx
Left by manish on Nov 07, 2008 10:36 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
good one :)
Left by ashu on Dec 09, 2008 10:20 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
simple and easy to understand. cute article
Left by chinna on Feb 22, 2009 11:40 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
This is very useful for me , nice article
Left by Senthil Athi on Feb 24, 2009 12:25 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Very simple and fast learning!!!
Left by Arun on Mar 16, 2009 3:35 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Tu gadha hai
Left by TriKumat on Mar 17, 2009 5:23 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
good explained.....
thanks
Left by shraddha on Mar 24, 2009 7:39 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
good example....useful
Left by aditya on Mar 30, 2009 12:41 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
It is very helpful to gain knowledge about the partial classes
Left by Amila Nandana on Apr 06, 2009 2:36 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Good :)

Left by Devipriya on May 27, 2009 4:16 AM

# re: Partial Classes with C# 2.0
Requesting 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.
Left by achim on Jun 25, 2009 9:02 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
cool dude
Left by hyper on Sep 09, 2009 4:46 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
What will be the output of this code will add more to the article
Left by Deepti on Sep 30, 2009 12:19 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
This really good article to learn partial class
Left by Arun on Nov 23, 2009 9:09 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
really gud artical
Left by sreelu on Dec 08, 2009 2:58 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
good explanation...
Left by rashmi pardeshi on Jan 18, 2010 11:42 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Good Example
Left by Shantilal B. on Mar 25, 2010 3:30 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
same drawbacks i was searching.. thanx
Left by jitendra on Apr 10, 2010 2:39 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Nic article for the fresher...........gud job
Left by Rohit K Dhiman on May 27, 2010 12:41 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
nice example but u can explain more if you want..
Left by abhinav on Nov 17, 2010 11:51 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
very very excellent example,but i want some more theory,because interviewer ask some more theory not concept
Left by pramod kumar singh on Dec 08, 2010 5:32 PM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Ya,It's good.
It cleared out my doubts.
Left by Shital Rananavare on May 23, 2011 7:19 AM

# re: Partial Classes with C# 2.0
Requesting Gravatar...
Nice example
Left by Pradeepa on May 07, 2012 8:20 AM

Your comment:
 (will show your gravatar)


Copyright © INETA Pakistan | Powered by: GeeksWithBlogs.net | Join free