Explain Anonymous type in C#?
Answer:
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”};View
Explain Partial Class in C#?
Answer:
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.
ViewWhat you mean by boxing and unboxing in C#?
Answer:
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;View
Write a sample code to write the contents to text file in C#?
Answer:
Below is the sample code to write the contents to text file –
Using System.IO; File.WriteAllText(”mytextfilePath”, “MyTestContent”);View
What is the difference between “continue” and “break” statements in C#?
Answer:
Which are the loop types available in C#?
Answer:
Below are the loop types in C# -
For
While
Do.. While
What is enum in C#?
Answer:
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};View
How to check whether hash table contains specific key in C#?
Answer:
Method – “ContainsKey” can be used to check the key in hash table. Below is the sample code for the same –
Eg: myHashtbl.ContainsKey("1");View
Explain Hashtable in C#?
Answer:
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");
View
Why to use lock statement in C#?
Answer:
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.
ViewIs C# code is unmanaged or managed code?
Answer:
C# code is managed code because the compiler – CLR will compile the code to Intermediate Language.
ViewWhat is the difference between CType and Directcast in C#?
Answer:
Define Multicast Delegate in C#?
Answer:
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);View
What is the difference between “as” and “is” operators in C#?
Answer:
Why to use “Nullable Coalescing Operator” (??) in C#?
Answer:
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;View
What is Nullable Types in C#?
Answer:
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;View
What are the uses of delegates in C#?
Answer:
Below are the list of uses of delegates in C# -
View
Can we use delegates for asynchronous method calls in C#?
Answer:
Yes. We can use delegates for asynchronous method calls.
ViewWhat are the differences between events and delegates in C#?
Answer:
Main difference between event and delegate is event will provide one more of encapsulation over delegates. So when you are using events destination will listen to it but delegates are naked, which works in subscriber/destination model.
ViewWhat are the three types of Generic delegates in C#?
Answer:
Below are the three types of generic delegates in C# -
© 2017 QuizBucket.org