google-site-verification=ECX1M6_Vb39ty4VtQDXQce6wOjmPTxSettd3hTIZb9Q

C# Tutorial - About Partial Class in C#

random

C# Tutorial - About Partial Class in C#



In this article I will explain about C# Partial Classes and How to use Partial Classes in C#. Find the c# examples below:-

What is Partial Class in C#


"To split the definition of a class or a struct, an interface or a method over two or more source files is known as Partial Class."

Each source file contains a section of the class, and all parts are combined when the application is compiled. Use the partial keyword to split a class definition. 


c sharp partial, partial class, partial classes in C#, Partial classes, what ispartial classes, with example, c# interview questions

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UsingPartialClass
{
    public partial class Student
    {
        public void study()
        {
            Console.WriteLine("Studing..");
        }
    }

    public partial class Student
    {
        public void play()
        {
            Console.WriteLine("Playing..");
        }
    } 
}

class Program
    {
        static void Main(string[] args)
        {
            Student obj = new Student();
            obj.study();
            obj.play();

            Console.ReadKey();
        }
    }

NoteWhile creating a partial class it is very important to keep the following points in mind:-
  • All the parts must use the partial keyword.
  • All the parts must be available at compile time to form the final class.
  • All the parts must have the same access modifiers - public, private, protected etc.
  • Any class members declared in a partial definition are available to all the other parts. 
  • The final class is the combination of all the parts at compile time.

Benefits Of Using 'Partial' Class In C#?

1. When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.

2. When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.

Is it possible to create partial structs, interfaces and methods?

Yes, it is possible to create partial structs, interfaces and methods. We can create partial structs, interfaces and methods the same way as we create partial classes.

Will the following code compile?

using System;
namespace PartialClass
{
  public partial class Student
  {
    public void Study()
    {
      Console.WriteLine("I am studying");
    }
  }
  public abstract partial class Student
  {
    public void Play()
    {
      Console.WriteLine("I am Playing");
    }
  }
  public class Demo
  {
    public static void Main()
    {
      Student StudentObject = new Student();
    }
  }
}

No, a compile time error will be generated stating "Cannot create an instance of the abstract class or interface "PartialClass.Student". This is because, if any part is declared abstract, then the whole class becomes abstract. Similarly if any part is declared sealed, then the whole class becomes sealed and if any part declares a base class, then the whole class inherits that base class.

Can you create partial delegates and enumerations?

No, you cannot create partial delegates and enumerations.

Can different parts of a partial class inherit from different interfaces?

Yes, different parts of a partial class can inherit from different interfaces. 

Can you specify nested classes as partial classes?

Yes, nested classes can be specified as partial classes even if the containing class is not partial. An example is shown below.

class ContainerClass
{
  public partial class Nested
  {
    void Test1() { }
  }
  public partial class Nested
  {
    void Test2() { }
  }
}

How do you create partial methods?

To create a partial method we create the declaration of the method in one part of the partial class and implementation in the other part of the partial class. The implementation is optional. If there is no implementation declaration, then the compiler optimizes away both the defining declaration and all calls to the method. 

While creating a partial methods it is very important to keep in mind following points in mind.
  • Partial method declarations must begin partial keyword.
  • The return type of a partial method must be void.
  • Partial methods can have ref but not out parameters.
  • Partial methods are implicitly private, and therefore they cannot be virtual.
  • Partial methods cannot be extern, because the presence of the body determines whether they are defining or implementing.

Use of Partial Methods?

Partial methods can be used to customize generated code. They allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method. Much like partial classes, partial methods enable code created by a code generator and code created by a human developer to work together without run-time costs.

To download the source code, click here
Please leave your comments, suggestions and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading! :)
C# Tutorial - About Partial Class in C# Reviewed by Ravi Kumar on 2:56 PM Rating: 5

3 comments:

  1. Informative. Should the name of both partial classes be the same 'Student' as specified in above source code? Can we give different names to them?

    ReplyDelete
    Replies
    1. No you can't give different name for them. There must be the same name for the class "Student".

      Delete

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.