C#.NET Interview Questions and Answers – Part 3

C#.NET Interview Questions and Answers – Part 3

21. What is the use of Nan values for double and float in C# ?

Nan in C# is generally used to represent special values in .NET . For example , in Silverlight and WPF , the values double.Nan is generally used to represent the automatic values.

22. What is the difference between Double and decimal in C# ?

Double uses Base 2 for its internal representation whereas decimal used Base 10 for its internal representation.

The Double can accept 15-16 decimal precision where as decimal can accept 28-29 . Double can have special values like +0 , –0 , Infinity and Nan whereas decimal does not accept these values.

Double is native to processor whereas decimal is non-native to processor and this decimal will some times be slower than double.

23. How does Equality (==) for the reference type work ?

The == (equality) operator checks for equality based on the reference instead of the actual value of the object .

In the below example , emp1 != emp2 but emp1 == emp3
public class Employee
{
public string Name;
public Employee(string name) { Name = name; }
}
Employee emp1 = new Employee(“Rajnikanth”);
Employee emp2 = new Employee(“Rajnikanth”);
Console.WriteLine (emp1 == emp2); // This will retuen False
Employee emp3 = emp1;
Console.WriteLine (emp1 == emp3); // This will return true

24. How does the equality operator work on string which is a reference type ?

Although string is a reference type , the equality operator checks the equality based on the value.

string str1 = “welcome”;
string str2 = “welcome”;
Console.Write (str1 == str2); // This returns true

25. What is a Verbatim string in C# ?

C# provides the verbatim string literals which is generally prefixed with @. The verbatim string avoids the usage of escape characters within the string and can also span multiple lines.

26. Is Array a value type or reference type ?

Array is a reference type object irrespective of its data type. For example , we declare an array using the new operator as shown below.

int[] array1 =  new int[100];

26. How to initialize an array with the shorthand array initialization expression without the use of new operator ?

Below is an example of how we can do it.

char[] vowel = {‘a’,’e’,’i’,’o’,’u’};

27. What is the default value for all the types in C# ?

Every type has a default values .

All reference types = null
All numeric types = 0
Char – ‘\0’
Boolean – false

28. How to obtain the default value of a type in C# ?

You can use the keyword default in C?# to get the default value for any type.
int intValue = default (int);

29.What is the default parameter passing mechanism used by C# ?

By default , the arguments in C# are passed by value.

30. How do we achieve pass by reference in C# ?

The pass by reference can be achieved in C# using the ref , out and in keyword.