Sunday, July 4, 2021

Partial Class and Nested Types Interview Questions and Answers in C#

 

  1. What is a partial class? Explain with an example.
  2. What are the advantages of using Partial Classes in C#?
  3. Is it possible to create partial structs, interfaces, and methods in C#?
  4. Can you create partial delegates and enumerations?
  5. Can different parts of a partial class inherit from different interfaces?
  6. Can you specify nested classes as Partial Classes?
  7. How do you create partial methods in C#?
  8. What is the use of partial methods?
  9. What is a Nested Type in C#? Give an example?
  10. Can the nested class access the Containing class. Give an example?
What is a partial class? Explain with 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. The 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 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 in C#?

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 in C#?

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.

Partial Class Interview Questions and Answers in C#
How do you create partial methods in C#?

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 that is split 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”

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 to hold 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.

What is a Nested Type in C#? Give an example?

A type (class or a struct) defined inside another class or struct is called a nested type. An example is shown below. InnerClass is inside ContainerClass, Hence InnerClass is called a nested class.

Nested Types Interview Questions and Answers in C#

Will the following code compile in C#?
namespace Nested
{
class ContainerClass
{
class InnerClass
{
public string str = "A string variable in nested class";
}
}
class Demo
{
public static void Main()
{
InnerClass nestedClassObj = new InnerClass();
Console.WriteLine(nestedClassObj.str);
}
}
}

No, the above code will generate a compile-time error stating – The type or namespace name ‘InnerClass’ could not be found (are you missing a using directive or an assembly reference?)This is because InnerClass is inside ContainerClass and does not have any access modifier. Hence inner class is like a private member inside ContainerClass. For the above code to compile and run, we should make InnerClass public and use the fully qualified name when creating the instance of the nested class as shown below.

namespace Nested
{
class ContainerClass
{
public class InnerClass
{
public string str = "A string variable in nested class";
}
}
class Demo
{
public static void Main()
{
ContainerClass.InnerClass nestedClassObj = new ContainerClass.InnerClass();
Console.WriteLine(nestedClassObj.str);
}
}
}
Can the nested class access, the Containing class. Give an example?

Yes, the nested class or inner class can access the containing or outer class as shown in the example below. Nested types can access private and protected members of the containing type, including any inherited private or protected members.

namespace Nested
{
class ContainerClass
{
string OuterClassVariable = "I am an outer class variable";
public class InnerClass
{
ContainerClass ContainerClassObject = new ContainerClass();
string InnerClassVariable = "I am an Inner class variable";
public InnerClass()
{
Console.WriteLine(ContainerClassObject.OuterClassVariable);
Console.WriteLine(this.InnerClassVariable);
}
}
}
class Demo
{
public static void Main()
{
ContainerClass.InnerClass nestedClassObj = new ContainerClass.InnerClass();
}
}
}
What is the output of the following program?
namespace Nested
{
class ContainerClass
{
public ContainerClass()
{
Console.WriteLine("I am a container class");
}
public class InnerClass : ContainerClass
{
public InnerClass()
{
Console.WriteLine("I am an inner class");
}
}
}
class DemoClass : ContainerClass.InnerClass
{
public DemoClass()
{
Console.WriteLine("I am a Demo class");
}
public static void Main()
{
DemoClass DC = new DemoClass();
}
}
}

Output:

Nested Types Interview Questions and Answers C#.NET

Lab 09: Publish and subscribe to Event Grid events

  Microsoft Azure user interface Given the dynamic nature of Microsoft cloud tools, you might experience Azure UI changes that occur after t...