C# Interview Questions and Answers (958) - Page 12

Is it possible to instantiate a struct without using a new operator and can a struct inherit from another struct or class in C#?

Yes, it is possible to instantiate a struct without using a new operator.
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 and what's the type of struct in C#?

Yes, the struct can be inherited from an interface and the 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.
Mention two cases where static constructors can be used?

• When the class is using a log file and the constructor is used to write data and enters into the file then we can use static constructor.

• It is also useful to call the LoadLibrary method by using constructors and when creating wrapper classes for unmanaged code.
Can a class have static constructor?

Yes, a class can have static constructor. Static constructors are called before when any static fields are executed. These are used to initialize static class members and called automatically before the first instance is created or any static members are referenced. This is also known as before instance constructor. Below is one example of above described theory.

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SampApp
{
class Sample
{
static int i;
static Sample()
{
i = 10;
Console.WriteLine("In Static Constructor");
}
public Sample()
{
Console.WriteLine("In Instance Constructor");
}
public static void Main()
{
Sample s = new Sample();
}
}
}

How you create partial methods?

To create a partial method we have to declare two parts that is definition or we can say declaration and the implementation. Here the implementation is optional i.e. when it is not declared then all calls to the method are removed at compile time. So that the code in partial class can freely use partial method even if implementation is not provided. If implementation is not declared then the compiler will define the declaration and will call to the methods.

Following points to keep when creating partial methods…

• The declaration of Partial method must begin with a partial keyword.
• The return type of a partial method must be void.
• Partial methods can have ref parameters but not the out parameters.
• The default type of Partial method is private, and it cannot be virtual.
• Partial methods cannot be extern, because it determines whether they are defining or implementing.
Can you inherit different parts of a partial class from different interfaces and is it possible to create partial delegates and enumerations?

Yes, the different parts of a partial class can be inherited from different interfaces and you cannot create any partial delegates and enumerations for a partial class.
Show how you specify nested classes as partial classes with example?

The nested classes can be specified as a partial class even if the containing class is not partial. Below is one example.

class ClsContainer

{
public partial class Nested
{
void App1() { }
}

public partial class Nested
{
void App2() { }
}
}

Can a nested class access the outer class? Give an example?

Yes, the nested class or inner class can access the outer class. Nested types can access private and protected members of the containing type, including inherited private or protected members.

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nested
{
class ClsContainer
{
string outerClsVariable = "This is outer class variable";

public class InnerClass
{
ClsContainer Obj = new ClsContainer();
string innerClsVariable = "This is inner class variable";

public InnerClass()
{
Console.WriteLine(Obj.outerClsVariable);
Console.WriteLine(this.innerClsVariable);
}
}
}

class Exec
{
public static void Main()
{
ClsContainer.InnerClass nestedClsObj = new ClsContainer.InnerClass();
}
}
}

What is the difference between method parameters and method arguments? Give an example?

The method parameters are passed as an argument for a method but the method arguments are itself having a value. The method definition specifies the name and type of any parameters that are required. The arguments must be compatible with the parameter type but the argument name used in the calling code does not have to be the same as the parameter named defined in the method.
In the below example FNum and SNum are method parameters and N1 and N2 are method arguments.

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sample
{
class Program
{
public static void Main()
{
int N1 = 10;
int N2 = 20;
//N1 and N2 are method arguments
int Total = Sum(N1, N2);
Console.WriteLine(Total);
}

//FNum and SNum are method parameters
public static int Sum(int FNum, int SNum)
{
int Sum = FNum + SNum;
return Sum;
}
}
}

Can you pass value types by reference to a method?

Yes, you can pass value types by reference to a method. Below is one sample code snippet to have clear idea.

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sample
{
class Program
{
public static void Main()
{
int a = 10;
Console.WriteLine("Value of a before passing to the method = " + a);
Function(ref a);
Console.WriteLine("Value of a after passing to the method by reference= " + a);
}
public static void Function(ref int Num)
{
Num = Num + 5;
}
}
}

What is the difference between a constant and a static readonly field?

A static readonly field is very much similar as constant, but the difference is the C# compiler does not have access to the value of a static readonly field at compile time. It will access only at run time.
Write the different components of LINQ (Language Integrated Query) and state the purpose of LINQ Providers in LINQ?

The three components of LINQ are
• Standard Query Operators
• Language Extensions
• LINQ Providers

The LINQ Providers are set of classes which take a LINQ query and it dynamically generates a method that executes its corresponding query against particular data source.
How can you implement Standard Query Operators in LINQ?

These are implemented in LINQ as extension methods in .NET Framework. This can be used to work with any collection of objects that implements the IEnumerable interface. The inherited classes from the IEnumerable interface provide an enumerator for repeating over a collection of a specific type. All most all arrays and generic collection classes implementing IEnumerable interface.
What is the use of Standard Query Operators in LINQ?

Basically these are used in LINQ to work with collections for the following...
• To get total count of elements in a collection.
• To order the results of a collection.
• For grouping.
• For computing average.
• To join two collections based on matching keys.
• To filter the results
What is Regular Expressions in C#.NET?

The Regular Expressions are the languages which identifies character patterns. Basically this is used for the following tasks such as:

• To validate the text input such as passwords, numbers etc.
• Parsing the textual data into more structured forms. For example, extracting data from an HTML page to store in database.
• Replacing the pattern of text into a document.

All Regular Expression types are defined in System.Text.RegularExpressions .
What is Zero-Width Assertions in C#?

With the help of Regular Expression languages, you can place conditions on what it should occur before and after a match, through lookbehind, lookahead, anchors, and word boundaries. They don’t increase the width or length of the match. So that’s why these are called Zero-Width Assertions.
What is Finalizer?

These are nothing but class-only methods which will execute before the garbage collector reclaims the memory for an unreferenced object. To implement finalizer, you have prefix the symbol(~) before the name of the class.

Syntax:
class Sample

{
~Sample()
{
…….
…….
}
}

How to install and uninstall Windows Services ?

In order to install and uninstall Window Services. First we have to open the command prompt of Visual Studio and we have to move to the project folder where the .exe file is located and then we have to type :

For Installing :

installutil -i ServiceName.exe


For UnInstalling :

installutil -u ServiceName.exe

What are circular references? Explain how garbage collection deals with circular references?

A circular reference is a series of references when a formula refers back to its own cell, either directly or indirectly and the last object references the first, resulting in a closed loop. Also it is a run-around where in two resources are interdependent on each other.

The methods to deal with circular references are:
• Weighted reference counting
• Indirect reference counting

There are some ways to handle problem of detecting and collecting circular references with the help of garbage collection.
• The system may explicitly forbid reference cycles.
• Systems ignore cycles if it have small amount of cyclic garbage.
• You can also periodically use a tracing garbage collector cycles.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories