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:
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article