Partial Class Interview Questions C#.NET

Partial Class Interview Questions and Answers C#.NET

In this article, I am going to discuss most frequently asked Partial Class Interview Questions and Answers on C#.NET. To understand the below questions and answers first have a look at the below article where we discussed the Partial class and Partial methods in details.

Partial Class and Partial Methods in C#

What is a partial class? Give an example?

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 use partial keyword and public access modifier.

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. The final class is the combination of all the parts at compile time.
  3. All the parts must be available at compile time to form the final class.
  4. Any class members declared in a partial definition are available to all the other parts. 
  5. All the parts must have the same access modifiers – public, private, protected etc.
What are the advantages of using partial classes?

When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.

When working with an automatically generated source, the 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 in the same way as we create partial classes.

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 i.e. 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 with the partial keyword.
  2. The return type of a partial method must be void.
  3. Partial methods can have ref but not out parameters.
  4. The 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.

I have a class which is spitted into two partial classes. These two partial classes have the same methods. What happens when we compile

At compile time all partial classes will be combined together to form a single final class. In the same class, we cannot have multiple methods with the same name. But of course method overloading possible.

Will the following code compile?
public class Example
{
    static void Main()
    {
        TestStruct T = new TestStruct();
        Console.WriteLine(T.i);
    }
}
public struct TestStruct
{
    public int i = 10;
    //Error: cannot have instance field initializers in structs
}

No, a compile-time error will be generated stating “within a struct declaration, fields cannot be initialized unless they are declared as const or static”

Can a struct have a default constructor (a constructor without parameters) or a destructor in C#? 

No

Can you instantiate a struct without using a new operator in C#?

Yes, you can instantiate a struct without using a new operator

Can a struct inherit from another struct or class in C#?

No, a struct cannot inherit from another struct or class, and it cannot be the base of a class.

Can a struct inherit from an interface in C#?

Yes

Are structs value types or reference types? 

Structs are value types

What is the base type from which all structs inherit directly?

All structs inherit directly from System.ValueType, which inherits from System.Object.

What do you mean by saying a “class is a reference type”? 

A class is a reference type means when an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.

What do you mean by saying a “struct is a value type”?

A struct is a value type mean when a struct has created the variable to which the struct is assigned holds the struct’s actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable, therefore, contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

When do you generally use a class over a struct?

A class is used to model more complex behavior or data that is intended to be modified after a class object is created. A struct is best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.

List the 5 different access modifiers in C#?
  1. Public
  2. Protected
  3. Internal
  4. protected internal
  5. private
If you do not specify an access modifier for a method, what is the default access modifier?

private

Classes and structs support inheritance. Is this statement true or false? 

False, only classes support inheritance. structs do not support inheritance.

If a class derives from another class, will the derived class automatically contain all the public, protected, and internal members of the base class? 

Yes, the derived class will automatically contain all the public, protected, and internal members of the base class except its constructors and destructors.

Can you create an instance for an abstract class? 

No, you cannot create an instance for an abstract class.

How do you prevent a class from being inherited by another class?

Use the sealed keyword to prevent a class from being inherited by another class.

Classes and structs can be declared as static, Is this statement true or false?

False, only classes can be declared as static and not structs.

Can you create an instance of a static class?

No, you cannot create an instance of a static class.

Can a static class contain non static members? 

No, a static class can contain only static members.

If you want to learn Partial Class and Partial Methods in details in C# then please read below article.

Partial Class and Partial Methods in C#

SUMMARY

In this article, I try to explain most frequently asked Partial Class Interview Questions and Answers on C#.NET. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *