C# Interview Question and Answers with Example - Part 6


Below is top C# interview question and answers with examples for freshers and intermediate developers.



Views: 62

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.

//Partial class with same name method:
public partial class Calculator
{
    public string Add()
    {
        return "";
    }
    public string Subtract()
    {
        return "";
    }
}
public partial class Calculator
{
    public string Multiply()
    {
        return "";
    }
    public string Add()
    {
        return "";
    }
}

There will be a compile time error when you try to compile this code! What happens at build time is the compiler combines all the members defined in all the partial definitions of the class into one. It will then try to compile it the usual way. In your case it will raise an error mentioning you already have defined a method with the same name.



Views: 62