June 13, 2019

Srikaanth

Pegasystems Latest C# Interview Questions Answers

Pegasystems Most Frequently Asked Latest C# Interview Questions Answers

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.

What Is A Nested Type. 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 as nested class.

using System;
namespace Nested
{
class ContainerClass
{
class InnerClass
{
public string str = "A string variable in nested class";
}
public static void Main()
{
InnerClass nestedClassObj = new InnerClass();
Console.WriteLine(nestedClassObj.str);
}
}
}
Pegasystems Most Frequently Asked Latest C# Interview Questions Answers
Pegasystems Most Frequently Asked Latest C# Interview Questions Answers

Can A Class Or A Struct Have Multiple Constructors?

Yes, a class or a struct can have multiple constructors. Constructors in csharp can be overloaded.

Can A Child Class Call The Constructor Of A Base Class?

Yes, a child class can call the constructor of a base class by using the base keyword as shown in the example below.

using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass(string str)
{
Console.WriteLine(str);
}
}
class ChildClass : BaseClass
{
public ChildClass(string str): base(str)
{
}
public static void Main()
{
ChildClass CC = new ChildClass("Calling base class constructor from child class");
}
}
}

If A Child Class Instance Is Created, Which Class Constructor Is Called First - Base Class Or Child Class?

When an instance of a child class is created, the base class constructor is called before the child class constructor. An example is shown below.

using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass()
{
Console.WriteLine("I am a base class constructor");
}
}
class ChildClass : BaseClass
{
public ChildClass()
{
Console.WriteLine("I am a child class constructor");
}
public static void Main()
{
ChildClass CC = new ChildClass();
}
}
}

Can A Class Have Static Constructor?

Yes, a class can have static constructor. Static constructors are called automatically, immediately before any static fields are accessed, and are generally used to initialize static class members. It is called automatically before the first instance is created or any static members are referenced. Static constructors are called before instance constructors. An example is shown below.

using System;
namespace TestConsole
{
class Program
{
static int I;
static Program()
{
I = 100;
Console.WriteLine("Static Constructor called");
}
public Program()
{
Console.WriteLine("Instance Constructor called");
}
public static void Main()
{
Program P = new Program();
}
}
}

Can You Mark Static Constructor With Access Modifiers?

No, we cannot use access modifiers on static constructor.

Can You Have Parameters For Static Constructors?

No, static constructors cannot have parameters.

What Happens If A Static Constructor Throws An Exception?

If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

Give 2 Scenarios Where Static Constructors Can Be Used?

1. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
2. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

Does C# Provide Copy Constructor?

No, C# does not provide copy constructor.

Is The Following Code Legal?

using System;
namespace Demo
{
class Program
{
public static void Main()
{
}
public void Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
public int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
}
}

No, The above code does not compile. You cannot overload a method based on the return type. To overload a method in C# either the number or type of parameters should be different. In general the return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to.

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.

using System;
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 Ouput Of The Following Program?

using System;
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
I am a container class
I am an inner class
I am a Demo class

The above program has used the concepts of inheritance and nested classes. The ContainerClass is at the top in the inheritance chain. The nested InnerClass derives from outer ContainerClass. Finally the DemoClass derives from nested InnerClass. As all the 3 classes are related by inheritance we have the above output.

What Is A Destructor?

A Destructor has the same name as the class with a tilde character and is used to destroy an instance of a class.

Can A Class Have More Than 1 Destructor?

No, a class can have only 1 destructor.

Can Structs In C# Have Destructors?

No, structs can have constructors but not destructors, only classes can have destructors.

Can You Pass Parameters To Destructors?

No, you cannot pass parameters to destructors. Hence, you cannot overload destructors.

Can You Explicitly Call A Destructor?

No, you cannot explicitly call a destructor. Destructors are invoked automatically by the garbage collector.

Why Is It Not A Good Idea To Use Empty Destructors?

When a class contains a destructor, an entry is created in the Finalize queue. When the destructor is called, the garbage collector is invoked to process the queue. If the destructor is empty, this just causes a needless loss of performance.

Is It Possible To Force Garbage Collector To Run?

Yes, it possible to force garbage collector to run by calling the Collect() method, but this is not considered a good practice because this might create a performance over head. Usually the programmer has no control over when the garbage collector runs. The garbage collector checks for objects that are no longer being used by the application. If it considers an object eligible for destruction, it calls the destructor(if there is one) and reclaims the memory used to store the object.

Usually In .net, The Clr Takes Care Of Memory Management. Is There Any Need For A Programmer To Explicitly Release Memory And Resources? If Yes, Why And How?

If the application is using expensive external resource, it is recommend to explicitly release the resource before the garbage collector runs and frees the object. We can do this by implementing the Dispose method from the IDisposable interface that performs the necessary cleanup for the object. This can considerably improve the performance of the application.

When Do We Generally Use Destructors To Release Resources?

If the application uses unmanaged resources such as windows, files, and network connections, we use
destructors to release resources.

What Is A Constructor In C#?

Constructor is a class method that is executed when an object of a class is created. Constructor has the same name as the class, and usually used to initialize the data members of the new object.

In C#, What Will Happen If You Do Not Explicitly Provide A Constructor For A Class?

If you do not provide a constructor explicitly for your class, C# will create one by default that instantiates the object and sets all the member variables to their default values.

Structs Are Not Reference Types. Can Structs Have Constructors?

Yes, even though Structs are not reference types, structs can have constructors.

https://mytecbooks.blogspot.com/2019/06/pegasystems-latest-c-interview.html
Subscribe to get more Posts :