GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  ASP.NET
Go To First  |  Previous Question  |  Next Question 
 ASP.NET  |  Question 146 of 164    Print  
what is .NET Partial Classes?
One of the language enhancements in .NET 2.0—available in both VB.NET 2005 and C# 2.0—is support for partial classes. In a nutshell, partial classes mean that your class definition can be split into multiple physical files. Logically, partial classes do not make any difference to the compiler. During compile time, it simply groups all the various partial classes and treats them as a single entity.

One of the greatest benefits of partial classes is that it allows a clean separation of business logic and the user interface (in particular the code that is generated by the visual designer). Using partial classes, the UI code can be hidden from the developer, who usually has no need to access it anyway. Partial classes will also make debugging easier, as the code is partitioned into separate files.

In this article, I will examine the use of partial classes in more detail and discuss how Visual Studio 2005 makes use of partial classes.

Using Partial Classes
Listing 1 contains two class definitions written in VB.NET, with the second class definition starting with the partial keyword. Both class definitions may reside in two different physical files. Functionally, Listing 1 is equivalent to Listing 2.

Listing 1

'---File1.vb---
Public Class Class1
Public Sub method1()

End Sub
End Class

File2.vb
Partial Public Class Class1
Public Sub method2()

End Sub
End Class

Listing 2

'---File1.vb---
Public Class Class1
Public Sub method1()

End Sub
Public Sub method2()

End Sub
End Class

So, what are the uses for partial classes?

Here are some good reasons to use partial classes:

1. They allow programmers on your team to work on different parts of a class without needing to share the same physical file. While this is useful for projects that involve big class files, be wary: If you find your class file getting too large, it may well signal a design fault and refactoring may be required.
2. The most compelling reason for using partial class is to separate your application business logic from the designer-generated code. For example, the code generated by Visual Studio 2005 for a Windows Form is kept separate from your business logic (we will discuss this in a later section). This will prevent developers from messing with the code that is used for the UI. At the same time, it will prevent you from losing your changes to the designer-generated code when you change the UI.



  
Total Answers and Comments: 2 Last Update: October 20, 2009     Asked by: girish 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
November 16, 2007 05:52:35   #1  
Gavaskar.s        

RE: what is .NET Partial Classes?

partial types are defined using the keyword “partial”.
Further the “partial” keyword should precede the type signature definition.
Note that the partial keyword applies to classes structs and interfaces but not enums.
It also does not apply to classes that extend the system classes or types.
“It is possible to split the definition of a class or a struct
or an interface over two or more source files




public partial class Employee

{

public void ReadData()

{

//Some code

}

}


Employee2.cs

public partial class Employee

{

public void SaveData()

{

//Some code

}

}


class Test

{

public static void Main(string[] args)

{

Employee employee new Employee();

//Some Code

employee.ReadData();

//Some code

employee.SaveData();


}

}


 
Is this answer useful? Yes | No
October 20, 2009 12:05:58   #2  
kirangiet Member Since: June 2009   Contribution: 24    

RE: what is .NET Partial Classes?
Using Partial Classes we can have same class span across multiple files.
However it is considered as a single class by CLR. Infact if we can take a look into IL code we will find a single class only. So two developers can work on their own copy of partial class.

Some More things that we can do with partial class say Employee.
We can keep all properties related to Employee in one Employee Partial Class.
We can keep all Methods related to Employee in one Employee Partial Class.
We can keep all constants and fields related to Employee in one Employee Partial Class and so on.

 
Is this answer useful? Yes | No

 Related Questions

ViewState in ASP.NET IntroductionMicrosoft ASP.NET Web Forms pages are capable of maintaining their own state across multiple client round trips. When a property is set for a control, the ASP.NET saves 
Latest Answer : Session variables are maintained for particular session like for particular user only, so any user related information has to be saved in session variable.  While application variables are maintained for whole application. Whole application related ...
Read Answers (2) | Asked by : Shiv

One of the language enhancements in .NET 2.0—available in both VB.NET 2005 and C# 2.0—is support for partial classes. In a nutshell, partial classes mean that your class definition can be split 
Latest Answer : Using Partial Classes we can have same class span across multiple files.However it is considered as  a single class by CLR. Infact,if we can take a look into IL code, we will find a single class only. So two developers can work on their own copy ...
Read Answers (2) | Asked by : girish


 Sponsored Links

 
Related Articles

ODP.NET - Fundamental ODP.NET Classes to Retrieve Data

ODP NET Fundamental ODP NET Classes to Retrieve Data To retrieve data from an Oracle database using ODP NET we need to work with a few of the ODP NET classes At this point we will discuss the most fundamental classes available in ODP NET for retrieving data mosgoogle The following is the list of fun
 

Microsoft AJAX Library - C# and JavaScript Classes

C and JavaScript Classes For the purpose of demonstrating a few more OOP related concepts we ll use another class Our new class is named Table and it has two public fields rows columns and one method getCellCount The getCellCount method should return the number of rows multiplied by the number of co
 

Microsoft AJAX Library - JavaScript Classes

JavaScript Classes Not only can JavaScript functions contain other functions but they can also be instantiated This makes JavaScript functions a good candidate for implementing the concept of a class from traditional object oriented programming This is very helpful feature indeed because JavaScript
 

C++ Storage Classes

C Storage Classes In this C tutorial you will learn about storage classes types of storage class variables Automatic External and Static explained with examples mosgoogle center Storage classes In the context of scope of variables in functions exists the important concept of storage class What is St
 

C++ Objects and Classes

C Objects and Classes An Overview about Objects and Classes In object oriented programming language C the data and functions procedures to manipulate the data are bundled together as a self contained unit called an object A class is an extended concept similar to that of structure in C programming l
 

The Interview Snafu

How to turn someone else’s mistake to your advantage Your dream job is about to become reality. A recruiter gave you the heads up about the perfect position at Humungous Conglomerate, Inc. You went through five interviews as well as a battery of psychological tests mandated by their HR de
 

Winning a Job Interview with a Winning Resume

Does your resume unlock your potential, take your skills to the highest level and win you the interview and the job you want now? The job market today is highly competitive and even if you think you have what it takes to get an interview you won’t get over the line without a polished, prof
 

Importance of Proper English during Job Interview

Importance of Proper English during Job Interview Your job interview is crucially important and it will determine whether or not you will get the job Depending on the type of job you re going for it is very important for you to use proper English In most cases jobs which offer higher salaries will h
 

HR Interview - HR Interview Mistakes You Will Want To Avoid

HR Interview Mistakes You Will Want To Avoid The job interview can be a stressful process This is especially true for those who are going after a competitive position Your nonverbal communication combined with the answers you give during the interview will determine if you are hired mosgoogle While
 

HR Interview - Behavioral HR Interviews

Behavioral HR Interviews As the name implies a behavioral interview is an interview that is held by a human resources department to determine if an applicant has the behaviors that are appropriate for a job The company must know how an applicant will behave in a certain situations mosgoogle The logi
 

About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape