PARTIAL CLASS
RULES FOR CREATING PARTIAL CLASS

What Is Partial Class ?

  • A partial class is a special feature of C#.
  • It provides a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled.
  • A partial class is created by using a partial keyword.
  • The partial keyword can also be used to split a struct or an interface over two or more files.
  • We use partial class when code of a class contains so many lines to manage.

Advantages of Partial Class

  • Multiple developers can work simultaneously with a single class in separate files.
  • When working on large projects, spreading a class over separate files allows programmers to work on it simultaneously.
  • Visual Studio uses partial classes to separate, automatically generated system code from the developer’s code. For Example when you add a webform, two .CS files are generated.
  • 1. WebForm1.aspx.cs – Contains the developer code.
  • 2. WebForm1.aspx.designer.cs – Contains the system generated code. For example, declarations for the controls that you drag and drop on the webform

RULES FOR PARTIAL CLASS IN C# PROGRAMMING

  • All the parts spread across different files, must use the partial keyword.
  • All the partial class definitions must be in the same assembly and namespace.
  • All the parts must have the same accessibility like public or private, etc.
  • If any part is declared abstract then the whole class is declared of the abstract type.
  • If any part is declared sealed then the whole class is declared of the sealed type.
  • If any of the parts inherit a class, then the entire type inherits the class.
  • C# does not support multiple class inheritance. Different parts of the partial class, must not specify different base classes.
  • Different parts of the partial class can specify different base interfaces.
  • Any member that are declared in a partial definition are available to all of the other parts of the partial class.

Source Code

StudentPartial1.cs – Source Code

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

namespace Partial_Class_Demo
{
    public partial class StudentPartial
    {
        private string _firstName;
        private string _lastName;

        public string FirstName
        {
            set
            {
                _firstName = value;
            }
            get
            {
                return _firstName;
            }
        }

        public string LastName
        {
            get
            {
                return _lastName;
            }

            set
            {
                _lastName = value;
            }
        }
    }
}

StudentPartial2.cs – Source Code

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

namespace Partial_Class_Demo
{
    public partial class StudentPartial
    {
        public string GetCompleteName()
        {
            return _firstName + " " + _lastName;
        }
    }
}

Main Method – Source Code

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

namespace Partial_Class_Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            StudentPartial obj = new StudentPartial();
            obj.FirstName = "Mohammad";
            obj.LastName = "Adil";
            Console.WriteLine("Your complete name is: " + obj.GetCompleteName());
            Console.ReadLine();
        }
    }
}

https://www.mediafire.com/file/3pfv11h9l73vcau/partial+class+IN+C#.pptx/file

https://www.mediafire.com/file/orfbyu6hzua7mup/Partial_Class_Demo.rar/file

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *