Beruflich Dokumente
Kultur Dokumente
5M
(23K on)
Sign in
CPOL
Rate this:
Introduction
In this article, I explain partial classes in C# language with an example. A partial class
splits the definition of a class over two or more source files. You can create a class
definition in multiple files but it will be compiled as one class.
Suppose you have a "Person" class. That definition is divided into the two source files
"Person1.cs" and "Person2.cs". Then these two files have a class that is a partial class.
You compile the source code, then create a single class. Let's see that in Figure 1.1.
//UserRegister.cs file
public partial classUser : IRegister, ILogin
{
//implements IRegister interface
}
//UserLogin.cs file
public partial classUser
{
//implements ILogin interface
}
3. Right-click on the project in the Solution Explorer, then go to "Add" and click on
"Class".
4. Choose "LINQ to SQL Classes" from the list and provide the name "Person" for the
DBML name. Then click on "Add".
5. Drag the User table from the database in the Server Explorer and drop onto the O/R
Designer surface of the "Person.dbml" file.
using System;
namespace PartialClassExample
{
public partial class Person
{
public int Age
{
get { return Convert.ToInt32(System.DateTime.UtcNow.Date.Year _DateOfBirth.Value.Year); }
}
}
}
<%@Page Language="C#"AutoEventWireup="true"
CodeBehind="PersonUI.aspx.cs"Inherits="PartialClassExample.PersonUI"%>
<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"runat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="gridPerson"runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
8. Write code for the "Page_Load" event to bind a grid view by person list in the code
behind file.
Hide Shrink
Copy Code
using System;
using System.Linq;
namespace PartialClassExample
{
public partial class PersonUI : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (PersonDataContext context =new PersonDataContext())
{
var query = from person in context.GetTable<person>()
select new
{
person.Id,
person.Name,
person.DateOfBirth,
person.Age
};
var content = query.ToList();
gridPerson.DataSource = content;
gridPerson.DataBind();
}
}
}
}</person>
9. Run the application, you will see the Age column in the grid view that shows the age
of each person. Let's see that in Figure 1.3.
License
This article, along with any associated source code and files, is licensed under The Code
Share
EMAIL
India
Sandeep Singh Shekhawat is a Software Developer. He is awarded for C# Corner
MVP(2013,2014 and 2015) and CodeProject MVP (2015). His three articles at
CodeProject are prize wining in article competition. His more than 10 articles has been
selected Article of the Day at Microsoft ASP.NET (http://www.asp.net/) community.
Go
14-Mar-16 18:24
Consistency in an Example
Member 12133631
good
anilh.chavan
13-Nov-15 10:38
3-Feb-15 0:49
Re: good
3-Feb-15 17:29
31-Dec-14 7:58
thank
Member 10149906
30-Dec-14 13:10
Re: thank
30-Dec-14 17:24
My vote of 1
Nam1222
30-Dec-14 12:42
Re: My vote of 1
icemanind
31-Dec-14 7:21
Re: My vote of 1
Nam1222
16-Jan-15 21:05
Re: My vote of 1
icemanind
17-Jan-15 5:52
Re: My vote of 1
Nam1222
17-Jan-15 13:10
Re: My vote of 1
icemanind
18-Jan-15 11:06
Re: My vote of 1
Mario Z
Good
chargoy
24-May-16 23:37
30-Dec-14 5:39
My vote of 5
My vote of 2
Member 11290173
29-Dec-14 22:19
26-Dec-14 0:30
Re: My vote of 2
1 Question
Member 10334156
26-Dec-14 1:25
22-Nov-14 1:24
Re: 1 Question
22-Nov-14 15:47
Re: 1 Question
PIEBALDconsult
30-Dec-14 4:44
Re: 1 Question
30-Dec-14 4:49
Re: 1 Question
PIEBALDconsult
30-Dec-14 5:44
Re: 1 Question
Antonio Ripa
31-Dec-14 1:54
29-Oct-14 19:38
My vote of 3
Klaus Luedenscheidt
22-Nov-14 15:48
13-Sep-14 18:06
Re: My vote of 3
14-Sep-14 4:08
Re: My vote of 3
reeselmiller2
29-Dec-14 20:31
Re: My vote of 3
PIEBALDconsult
30-Dec-14 17:16
Re: My vote of 3
Klaus Luedenscheidt
Thoughts
PIEBALDconsult
30-Dec-14 18:27
13-Sep-14 4:55
Re: Thoughts
CS2011
16-Sep-14 4:17
Good Job
Member 10879623
28-Jul-14 18:40
30-Aug-14 16:44
My vote of 5
Mihai MOGA
14-Feb-14 17:05
Re: My vote of 5
30-Aug-14 16:45
My vote of 5
ThatsAlok
10-Feb-14 1:49
Re: My vote of 5
10-Feb-14 2:13
good work
winnervijay
7-Feb-14 6:42
30-Aug-14 16:43
23-Jan-14 16:37
30-Aug-14 16:46
30-Aug-14 16:46
My vote of 1
Antonio Ripa
14-Jan-14 4:37
Re: My vote of 1
PIEBALDconsult
30-Dec-14 4:46
Re: My vote of 1
Antonio Ripa
31-Dec-14 2:01
Re: My vote of 1
PIEBALDconsult
31-Dec-14 4:41
Re: My vote of 1
Antonio Ripa
31-Dec-14 6:40
Re: My vote of 1
PIEBALDconsult
31-Dec-14 6:46
Refresh
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160904.1 | Last Updated 29 Dec 2014
1 2 Next
Article Copyright 2014 by Sandeep Singh Shekhawat
Everything else Copyright CodeProject, 1999-2016