C#
Interview Questions on partial classes, structs and methods.
What is a partial class. Give an example?
A partial class is a class whose definition
is present in 2 or more files. Each source file contains a section of the
class, and all parts are combined when the application is compiled. To split a
class definition, use the partial keyword as shown in the example below.
Student class is split into 2 parts. The first part defines the study() method
and the second part defines the Play() method. When we compile this program
both the parts will be combined and compiled. Note that both the parts uses
partial keyword and public access modifier.
using System;
namespace PartialClass
{
public partial class Student
{
public void Study()
{
Console.WriteLine("I am studying");
}
}
public partial class Student
{
public void Play()
{
Console.WriteLine("I am Playing");
}
}
public class Demo
{
public static void Main()
{
Student StudentObject = new Student();
StudentObject.Study();
StudentObject.Play();
}
}
}
It is very important to keep the following
points in mind when creating partial classes.
1. All the parts must use the partial
keyword.
2. All the parts must be available at
compile time to form the final class.
3. All the parts must have the same access
modifiers - public, private, protected etc.
4. Any class members declared in a partial
definition are available to all the other parts.
5. The final class is the combination of
all the parts at compile time.
What are the advantages of using partial
classes?
1. When working on large projects,
spreading a class over separate files enables multiple programmers to work on
it at the same time.
2. 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.
Is it possible to create partial structs,
interfaces and methods?
Yes, it is possible to create partial
structs, interfaces and methods. We can create partial structs, interfaces and
methods the same way as we create partial classes.
Will the following code compile?
using System;
namespace PartialClass
{
public partial class Student
{
public void Study()
{
Console.WriteLine("I am studying");
}
}
public abstract partial class Student
{
public void Play()
{
Console.WriteLine("I am Playing");
}
}
public class Demo
{
public static void Main()
{
Student StudentObject = new Student();
}
}
}
No, a compile time error will be generated
stating "Cannot create an instance of the abstract class or interface
"PartialClass.Student". This is because, if any part is declared
abstract, then the whole class becomes abstract. Similarly if any part is declared
sealed, then the whole class becomes sealed and if any part declares a base
class, then the whole class inherits that base class.
Can you create partial delegates and
enumerations?
No, you cannot create partial delegates and
enumerations.
Can different parts of a partial class
inherit from different interfaces?
Yes, different parts of a partial class can
inherit from different interfaces.
Can you specify nested classes as partial
classes?
Yes, nested classes can be specified as
partial classes even if the containing class is not partial. An example is
shown below.
class ContainerClass
{
public partial class Nested
{
void Test1() { }
}
public partial class Nested
{
void Test2() { }
}
}
How do you create partial methods?
To create a partial method we create the
declaration of the method in one part of the partial class and implementation
in the other part of the partial class. The implementation is optional. If the
implementation is not provided, then the method and all the calls to the method
are removed at compile time. Therefore, any code in the partial class can
freely use a partial method, even if the implementation is not supplied. No
compile-time or run-time errors will result if the method is called but not
implemented. In summary a partial method declaration consists of two parts. The
definition, and the implementation. These may be in separate parts of a partial
class, or in the same part. If there is no implementation declaration, then the
compiler optimizes away both the defining declaration and all calls to the
method.
The following are the points to keep in
mind when creating partial methods.
1. Partial method declarations must begin
partial keyword.
2. The return type of a partial method must
be void.
3. Partial methods can have ref but not out
parameters.
4. Partial methods are implicitly private,
and therefore they cannot be virtual.
5. Partial methods cannot be extern,
because the presence of the body determines whether they are defining or
implementing.
What is the use of partial methods?
Partial
methods can be used to customize generated code. They allow for a method name
and signature to be reserved, so that generated code can call the method but
the developer can decide whether to implement the method. Much like partial classes,
partial methods enable code created by a code generator and code created by a
human developer to work together without run-time costs.
No comments:
Post a Comment