c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

C sharp partial class

| | CSharp

When working with large projects, partial class splits the class for multiple programmers to work on it at the same time.  All files combined into a single class, when the application is compiled. The Partial keyword is used to split an interface or a struct into two or more files.

In this following example, I created two partial class named student, the fields and property are declared in one partial class definition and member GetFullName declared in another partial class definition.

Example:

Step 1: Create a class file and name it as Student.cs. Copy and paste the following code.It contains fields and properties.

namespace MVC_tutorials.Models
{
    public partial class Student
    {
        private string mFirstName;
        private string mLastName;

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

        public string LastName
        {
            get { return mLastName; }
            set { mLastName = value; }
        }
    }
}

Step 2: Right click on the project and create another class file and name it as PartialStudent. Copy and paste the following code. It contains only public function GetFullName(). We are able to access the private fields of student.cs file.

namespace MVC_tutorials.Models
{
    public partial class Student
    {
        public string GetFullName()
        {
            return mFirstName + " " + mLastName;
        }
    }
} 

Step 2: Copy and paste the following code for the Page_Load event.

protected void Page_Load(object sender, EventArgs e)
        {
            //fields declared in partial class1
            Student stud = new Student();
           stud.FirstName = "Mohamed";
           stud.LastName = "rasik";
            //function declared in partialclass 2
            string FullName = stud.GetFullName();
           Response.Write("FullName of student = " +FullName + "<br/>");
        }

Output: