- Usually a class placed in a single file. But to use that class in multiple files we can use partial classes.
- Partial allows classes, structs, interfaces to span across multiple files.
- Ex:
//file1.cs
partial class Class1
{
public void m1()
{
}
}
//file2.cs
partial class Class1
{
public void m2()
{
}
}
- After compile the source like
partial class Class1
{
public void m1()
{
}
public void m2()
{
}
}
Good one
ReplyDeleteNow here is the code:
ReplyDeleteFile 1:
public partial class myPartialClass
{
/*
///
///
///
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();
}
}