HI WELCOME TO SIRIS

C# Interview Questions and Answers

Leave a Comment
46) Can we use delegates for asynchronous method calls in C#?
Yes. We can use delegates for asynchronous method calls.
47) What are the uses of delegates in C#?
Below are the list of uses of delegates in C# -
  • Callback Mechanism
  • Asynchronous Processing
  • Abstract and Encapsulate method
  • Multicasting

48) What is Nullable Types in C#?
Variable types does not hold null values so to hold the null values we have to use nullable types. So nullable types can have values either null or other values as well.
Eg: Int? mynullablevar = null;

49) Why to use “Nullable Coalescing Operator” (??) in C#?
Nullable Coalescing Operator can be used with reference types and nullable value types. So if the first operand of the expression is null then the value of second operand is assigned to the variable. For example,
double? myFirstno = null;
double mySecno;
mySecno = myFirstno ?? 10.11;
50) What is the difference between “as” and “is” operators in C#?
  • “as” operator is used for casting object to type or class.
  • “is” operator is used for checking the object with type and this will return a Boolean value.
51) Define Multicast Delegate in C#?
A delegate with multiple handlers are called as multicast delegate. The example to demonstrate the same is given below
public delegate void CalculateMyNumbers(int x, int y);
int x = 6;
int y = 7;
CalculateMyNumbers addMyNumbers = new CalculateMyNumbers(FuncForAddingNumbers);
CalculateMyNumbers multiplyMyNumbers = new CalculateMyNumbers(FuncForMultiplyingNumbers);
CalculateMyNumbers multiCast = (CalculateMyNumbers)Delegate.Combine (addMyNumbers, multiplyMyNumbers);
multiCast.Invoke(a,b);
52) What is the difference between CType and Directcast in C#?
  • CType is used for conversion between type and the expression.
  • Directcast is used for converting the object type which requires run time type to be the same as specified type.
53) Is C# code is unmanaged or managed code?
C# code is managed code because the compiler – CLR will compile the code to Intermediate Language.
54) Why to use lock statement in C#?
Lock will make sure one thread will not intercept the other thread which is running the part of code. So lock statement will make the thread wait, block till the object is being released.
55) Explain Hashtable in C#?
It is used to store the key/value pairs based on hash code of the key. Key will be used to access the element in the collection. For example,
Hashtable myHashtbl = new Hashtable();
myHashtbl.Add("1", "TestValue1");
myHashtbl.Add("2", "TestValue2");
56) How to check whether hash table contains specific key in C#?
Method – “ContainsKey” can be used to check the key in hash table. Below is the sample code for the same –
Eg: myHashtbl.ContainsKey("1");
57) What is enum in C#?
enum keyword is used for declaring an enumeration, which consists of named constants and it is called as enumerator lists. Enums are value types in C# and these can’t be inherited. Below is the sample code of using Enums
Eg: enum Fruits { Apple, Orange, Banana, WaterMelon};
58) Which are the loop types available in C#?
Below are the loop types in C# -
For
While
Do.. While
59) What is the difference between “continue” and “break” statements in C#?
  • “continue” statement is used to pass the control to next iteration. This statement can be used with – “while”, “for”, “foreach” loops.
  • “break” statement is used to exit the loop.
60) Write a sample code to write the contents to text file in C#?
Below is the sample code to write the contents to text file –
Using System.IO;
File.WriteAllText(”mytextfilePath”, “MyTestContent”);


61) What you mean by boxing and unboxing in C#?


Boxing – This is the process of converting from value type to reference type. For example,


int myvar = 10;
object myObj = myvar;
UnBoxing – It’s completely opposite to boxing. It’s the process of converting reference type to value type. For example,
int myvar2 = (int)myObj;
62) Explain Partial Class in C#?
Partial classes concept added in .Net Framework 2.0 and it allows us to split the business logic in multiple files with the same class name along with “partial” keyword.
63) Explain Anonymous type in C#?
This is being added in C# 3.0 version. This feature enables us to create an object at compile time. Below is the sample code for the same –
Var myTestCategory = new { CategoryId = 1, CategoryName = “Category1”};
64) Name the compiler of C#?
C# Compiler is – CSC.
65) Explain the types of unit test cases?
Below are the list of unit test case types –
  • Positive Test cases
  • Negative Test cases
  • Exception Test cases
66) Explain Copy constructor in C#?
If the constructor contains the same class in the constructor parameter then it is called as copy constructor.
class MyClass
{
public string prop1, prop2;
public MyClass(string a, string b)
{
prop1 = a;
prop2 = b;
}

public MyClass(MyClass myobj) // Copy Constructor
{
prop1 = myobj.prop1;
prop2 = myobj.prop2;
}
}
67) Explain Static constructor in C#?
If the constructor is declared as static then it will be invoked only once for all number of instances of a class. Static constructor will initialize the static fields of a class.
class MyClass
{
public string prop1, prop2;
public MyClass(string a, string b)
{
prop1 = a;
prop2 = b;
}

Static MyClass()
{
Console.WriteLine(“Static Constr Test”);
}
public MyClass(MyClass myobj) // Copy Constructor
{
prop1 = myobj.prop1;
prop2 = myobj.prop2;
}
}
68) Which string method is used for concatenation of two strings in c#?
“Concat” method of String class is used to concatenate two strings. For example,
string.Concat(firstStr, secStr)
69) Explain Indexers in C#?
Indexers are used for allowing the classes to be indexed like arrays. Indexers will resemble the property structure but only difference is indexer’s accessors will take parameters. For example,
class MyCollection<T>
{
private T[] myArr = new T[100];
public T this[int t]
{
get
{
return myArr[t];
}
set
{
myArr[t] = value;
}
}
}
70) What are the collection types can be used in C#?
Below are the collection types in C# -
  • ArrayList
  • Stack
  • Queue
  • SortedList
  • HashTable
  • Bit Array
71) Explain Attributes in C#?
  • Attributes are used to convey the info for runtime about the behavior of elements like – “methods”, “classes”, “enums” etc.
  • Attributes can be used to add metadata like – comments, classes, compiler instruction etc.
72) List out the pre defined attributes in C#?
Below are the predefined attributes in C# -
  • Conditional
  • Obsolete
  • Attribute Usage
73) What is Thread in C#?
Thread is an execution path of a program. Thread is used to define the different or unique flow of control. If our application involves some time consuming processes then it’s better to use Multithreading., which involves multiple threads.
74) List out the states of a thread in C#?
Below are the states of thread –
  • Unstarted State
  • Ready State
  • Not Runnable State
  • Dead State
75) Explain the methods and properties of Thread class in C#?
Below are the methods and properties of thread class –
  • CurrentCulture
  • CurrentThread
  • CurrentContext
  • IsAlive
  • IsThreadPoolThread
  • IsBackground
  • Priority
76) What is a class ? A class is the generic definition of what an object is. A Class describes all the attributes of the object, as well as the methods that implement the behavior of the member object. In other words, class is a template of an object. For ease of understanding a class, we will look at an example. In the class Employee given below, Name and Salary are the attributes of the class Person. The Setter and Getter methods are used to store and fetch data from the variable.
public class Employee
{
private String name;
private String Salary;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getSalary ()
{
return Salary;
}
public void setSalary (String Salary)
{
this. Salary = Salary;
}
}

77) What is an Object?
An object is an instance of a class. It contains real values instead of variables. For example, let us create an instance of the class Employee called “John”.
Employee John= new Employee();
Now we can access all the methods in the class “Employee” via object “John” as shown below.
John.setName(“XYZ”);
78) What are the Access Modifiers in C# ?
Different Access Modifier are - Public, Private, Protected, Internal, Protected Internal
  • Public – When a method or attribute is defined as Public, it can be accessed from any code in the project. For example, in the above Class “Employee” getName() and setName() are public.
  • Private - When a method or attribute is defined as Private, It can be accessed by any code within the containing class only. For example, in the above Class “Employee” attributes name and salary can be accessed within the Class Employee Only. If an attribute or class is defined without access modifiers, it's default access modifier will be private.
  • Protected - When attribute and methods are defined as protected, it can be accessed by any method in the inherited classes and any method within the same class. The protected access modifier cannot be applied to classes and interfaces. Methods and fields in a interface can't be declared protected.
  • Internal – If an attribute or method is defined as Internal, access is restricted to classes within the current project assembly.
  • Protected Internal – If an attribute or method is defined as Protected Internal, access is restricted to classes within the current project assembly and types derived from the containing class. 

79) Explain Static Members in C# ?
If an attribute's value had to be same across all the instances of the same class, the static keyword is used. For example, if the Minimum salary should be set for all employees in the employee class, use the following code.
private static double MinSalary = 30000;
To access a private or public attribute or method in a class, at first an object of the class should be created. Then by using the object instance of that class, attributes or methods can be accessed. To access a static variable, we don't want to create an instance of the class containing the static variable. We can directly refer that static variable as shown below.
double var = Employee.MinSalary ;
80) What is Reference Type in C# ?
Let us explain this with the help of an example. In the code given below,
Employee emp1;
Employee emp2 = new Employee();
emp1 = emp2;
Here emp2 has an object instance of Employee Class. But emp1 object is set as emp2. What this means is that the object emp2 is referred in emp1, rather than copying emp2 instance into emp1. When a change is made in emp2 object, corresponding changes can be seen in emp1 object. 

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.