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 get domain name information from a Domain using Python
- ModulenotFoundError: no module named 'debug_toolbar' -SOLUTION
- How to create superuser in django project hosted in cPanel without terminal
- CSS & images not loading in django admin | cpanel without terminal
- Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
- How to sell domain name on Godaddy (2023)
- TemplateSyntaxError at / Could not parse the remainder: ' + 1' from 'forloop.counter0 + 1'
- urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0
Related Article