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.

14 comments:

  1. Can we have same method name in partial classes..?

    ReplyDelete
    Replies
    1. No , it gives compile time error

      Delete
  2. Error will occur during compilation time -"class already defines a member with the same parameter types"

    ReplyDelete
  3. Hi,I have a question.I have a class which is splitted into two partial classes.these two partial classes have same methods.what happens when we compile.I faced this question in one of the MNC interview.please help me.Thanks in advance

    ReplyDelete
  4. Q:I have a class which is splitted into two partial classes.these two partial classes have same methods.what happens when we compile

    Answer: At compile time all partial classes will be combined together to form a single final class. In same class we cannot have multiple methods with same name.But of course method overloading possible.I hope I have solved your question

    ReplyDelete
  5. Do all the parts of partial class need to be in one namespace

    ReplyDelete
  6. Do all the parts of partial class need to be in one namespace

    All parts must be defined within the same namespace.

    All the parts must use the partial keyword.

    All of the parts must be available at compile time to form the final type.

    All the parts must have the same accessibility (i.e. public, private, etc.).

    If any of the parts are declared abstract, then the entire type is abstract.

    If any of the parts are declared sealed, then the entire type is sealed.

    If any of the parts declare a base type, then all parts use that same base type. All parts that specify a base class must specify the same base class. You can omit a base class in one or more of the parts in which case the part still uses the same base class.

    ReplyDelete
  7. In ASP.NET, Why aspx.cs class is always declared partial?

    ReplyDelete
    Replies
    1. in early .NET 1.0 framework the design(i,e aspx)and code behind(now called .cs) used to reside in same form which lead to confusion.so they made aspx.cs as partial so when compiled it will merge with aspx.and completes the class.which made developing applications simpler with no confusions

      Delete
  8. i was searching this concept from so many days..Thanks Venkat

    ReplyDelete
  9. what ispartial means

    ReplyDelete
  10. can we have same method name in partial classes... ( one of the interview question I have faced in an mnc interview)... plz help me

    ReplyDelete
  11. how does the partial classes are combined during compile time?

    ReplyDelete
  12. how does the partial classes(user defined class split) combine during compile time? do we use versioning here? Please reply a little briefly....it was asked in an interview

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us