i am relatively a new programmer to c# and vb.net i found some difficulty in understanding partial classes what are the uses of partial classes.
thanks.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Shivanand ArurPosted Sep 14, 2012, 2:59 PM
Suppose there are 2 class files, (eg: Man.cs and Woman.cs) and these 2 files define a class which is partial and have some methods in it.
Male.cs file :
public partial class Human
{
string MGender = String.Empty;
public string Man()
{
MGender = "Male";
return MGender;
}
}
Female.cs file :
public partial class Human
{
string FGender = String.Empty;
public string Woman()
{
FGender = "Female";
return FGender;
}
}
Now when you create an instance of the Human Class in the Main Method in any file, then you will get access to both the Methods defined in these 2 class files. This happens because, during compilation/building the application, these class files get compiled into a single class file.
eg : Main Method of the God.cs file (This is another Physical class file and contains only main method)
public static void Main()
{
Human human = new Human();
string gender = human.Man();// returns Male.
string gender2 = human.Woman();// returns Female.
}
Partial Classes are useful when you have many methods in a class file. Suppose, you are creating an application and you have created a class file which already contains 18-20 methods with different logic which perform different tasks. And if you feel that more methods will be added in future to this class, then this is where Partial Classes can help a developer. You create a Partial class and define some methods in Class File 1 and some in Class File 2. This will help you to maintain the class files and won't mess up the class.
NOTE - DO NOT FORGET TO ADD A "Partial" KEYWORD AFTER DEFINING THE ACCESS SPECIFIER.
Hope this will help you understand the Partial Class concept.
Check out this video by Shivprasad Sir.... A great resource to understand the concept.
http://www.youtube.com/watch?v=ZEsS9-KfC70&feature=youtu.be&hd=1
Please feel free to ask questions if you have any doubt and if you feel that this reply has helped you, then please don't forget to mark this reply as an "Answer".
Satyapriya NayakPosted Sep 14, 2012, 12:04 PM
Please refer the below links
http://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.80%29.aspx
http://www.dotnet-guide.com/partialclasses.html
http://www.dotnetperls.com/partial
http://www.c-sharpcorner.com/uploadfile/puranindia/partial-classes-in-C-Sharp/
Thanks
If this post helps you mark it as answer