Você está na página 1de 3

c  


 
A class can be declared static, indicating that it contains only static members. It is not possible to
create instances of a static class using the new keyword. Static classes are loaded automatically by
the .NET Framework common language runtime (CLR) when the program or namespace containing
the class is loaded.

Use a static class to contain methods that are not associated with a particular object. For example, it
is a common requirement to create a set of methods that do not act on instance data and are not
associated to a specific object in your code. You could use a static class to hold those methods.

->The main features of a static class are:


1. They only contain static members.
2. They cannot be instantiated.
3. They are sealed.
4. They cannot contain Instance Constructors or simply constructors as we know that they are
associated with objects and operates on data when an object is created.
[C# e.g.]
static class CollegeRegistration
{
//All static member variables
static int nCollegeId; //College Id will be same for all the students studying
static string sCollegeName; //Name will be same
static string sColegeAddress; //Address of the college will also same

//Member functions
public static int GetCollegeId()
{
nCollegeId = 100;
return (nCollegeID);
}
//similarly implementation of others also.
} //class end

public class student


{
int nRollNo;
string sName;

public GetRollNo()
{
nRollNo += 1;
return (nRollNo);
}
//similarly ....
public static void Main()
{
//Not required.
//CollegeRegistration objCollReg= new CollegeRegistration();

//<ClassName>.<MethodName>
int cid= CollegeRegistration.GetCollegeId();
string sname= CollegeRegistration.GetCollegeName();

} //Main end
}

 

In this article I will explain what is a partial class? What are the benefits of using partial classes
and how to implement partial classes in your C# applications.

Partial class is a new feature added to C# 2.0 and Visual Studio 2005. It is supported in .NET
Framework 2.0. If you are working with .NET 1.0 or 1.1, partial classes may not work.

It is possible to split the definition of a class or a struct, or an interface over two or more source files.
Each source file contains a section of the class definition, and all parts are combined when the
application is compiled.

When working on large projects, spreading a class over separate files allows multiple programmers
to work on it simultaneously.

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 creating Windows Forms, Web
Service wrapper code, and so on. You can create code that uses these classes without having to edit
the file created by Visual Studio.
„      

1) More than one developer can simultaneously write the code for the class.

2) You can easily write your code (for extended functionality) for a VS.NET generated class. This will
allow you to write the code of your own need without messing with the system generated code.

‘  
      
    

@| All the partial definitions must proceeded with the key word "Partial".

@| All the partial types meant to be the part of same type must be defined within a same
assembly and module.

@| Method signatures (return type, name of the method, and parameters) must be unique for
the aggregated typed (which was defined partially).

@| The partial types must have the same accessibility.

@| If any part is sealed, the entire class is sealed.

@| If any part is abstract, the entire class is abstract.

@| Inheritance at any partial type applies to the entire class.

I have attached code of the partial classes along with this article. You can open the project and
understand the functionality.

Hope the article would have helped you in understanding what partial classes are. Waiting for your
feedback.

Você também pode gostar