What is partial class?

Posted by Pkanwar under ASP.NET on 10/8/2013 | Points: 10 | Views : 1008 | Status : [Member] | Replies : 6
What is partial class is asp.net ? Why we use partial class and advantage of partial class?




Responses

Posted by: Naraayanan on: 10/8/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
Check this link: http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.90).aspx

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pradeepmanne on: 10/8/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
partial class is used to declare a class in separate files in the same project.
A partial class allows a single class to be divided in to two separate physical files. During compile time these files get compiled in to single class.

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/8/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Partial class itself defines the same class file name into divided multiple parts with same singature. When complier will complie the class file ,it makes into single file.

Example:

Public partial class HRModule
{
public void DoWork() {}
}
Public partial class HRModule
{
public void GoToLunch() {}
}


Advantage
------------

1- Multiple Developer can work on same file
2-Type Safty
3-More Productvity


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 10/9/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,



When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.

When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.

To split a class definition, use the partial keyword modifier,

public partial class Employee
{
}

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.

If any part is declared abstract, then the whole type is considered abstract. If any part is declared sealed, then the whole type is considered sealed. If any part declares a base type, then the whole type inherits that class.

Mark as answer if satisfied....


Regards,
Shree M.

Kavya Shree Mandapalli

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Click on "Mark as Answer " link

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirbhogayta on: 10/29/2013 [Member] Starter | Points: 25

Up
0
Down
hi.. this is your answer

Partial classes span multiple files. How can you use the partial modifier on a C# class declaration? With partial, you can physically separate a class into multiple files. This is often done by code generators.
Keywords
Example

With normal C# classes, you cannot declare a class in two separate files in the same project. But with the partial modifier, you can. This is useful if one file is commonly edited and the other is machine-generated or rarely edited.

Program that uses partial class: C#

class Program
{
static void Main()
{
A.A1();
A.A2();
}
}

Contents of file A1.cs: C#

using System;

partial class A
{
public static void A1()
{
Console.WriteLine("A1");
}
}

Contents of file A2.cs: C#

using System;

partial class A
{
public static void A2()
{
Console.WriteLine("A2");
}
}

Output

A1
A2

SAMIR
Sr. Software Engineer

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response