AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Monday, 7 May 2018

How to generate partial class in c#?

In this article I am going to discuss about partial classes in c#.  Partial class provides a facility to create single class file code to multiple class files. "Partial" keyword is used to implement the partial class.
1- ExamplePartialClass1.cs
public partial class ExamplePartialClass
{
    public ExamplePartialClass()
    {
    }

    public void Method1(int arg)
    {
        Console.WriteLine(arg);
    }
}

2- ExamplePartialClass2.cs
public partial class ExamplePartialClass
{
    public void Method2(int arg)
    {
        Console.WriteLine(arg);
    }
}

On run time compiler will combine both the classes and make as one class.
public class ExamplePartialClass
{
    public ExamplePartialClass()
    {
    }

    public void Method1(int arg)
    {
        Console.WriteLine(arg);
    }

    public void Method2(int arg)
    {
        Console.WriteLine(arg);
    }

}

Prerequisite for Partial class

  1. All the members of class must have the same access specifiers like public or private, etc
  2. If any member of class is declared abstract, sealed or base type then the whole class is declared of the same type.
  3. Each and every partial class must be defined in the same assembly and namespace.

0 comments :

Post a comment

  • Popular Posts
  • Comments