I would like to dedicate this C# Interview Questions part 1 for people who want to do better in their interviews.
- What output do we get when we execute the below code?
- object o = null;
- try {
- int ? i = (int ? ) o;
- int i2 = i ? ? 0;
- Console.WriteLine(i2);
- Console.ReadLine();
- } catch (Exception e) {
- Console.WriteLine(e);
- Console.ReadLine();
- throw;
- }
Ans: 0
- What is nullable type?
Value types like int, long etc., can be assigned a Null value using this feature in C#. Just append "?" to a datatype, like int? and long?.
- When is "volatile" keyword used?
The volatile keyword is used to ensure that multiple threads can access the current value of a variable at all times.
- When is "checked" keyword used?
The checked keyword is used to enable the overflow checking for integer type arithmetic operations, like addition, subtraction, etc.
- When is "static" constructor called?
A static constructor is called before you create the first instance of a class or reference any static member.
- What is a static constructor?
A static constructor is used to initialize any static data or to perform any action that has to be executed only once.
- What is a dictionary?
Dictionary is used to represent the key-value pairs of the data.
- Dictionary<string, Int16> bestSites = new Dictionary<string, Int16>();
- What is the output of the below code?
- static void Main(string[] args) {
- try {
- Dictionary < Int16, string > bestSites = new Dictionary < short, string > ();
- bestSites.Add(2, "www.google.com");
- bestSites.Add(1, "www.c-sharpcorner.com");
- var ordered = bestSites.OrderBy(a => a.Value);
- foreach(var site in ordered) {
- Console.WriteLine("Key ={0},Value={1}", site.Key, site.Value);
- }
- bestSites[4] = "www.msn.com";
- Console.WriteLine(bestSites[4]);
- Console.ReadLine();
- } catch (Exception e) {
- Console.WriteLine(e.GetType());
- Console.ReadLine();
- throw;
- }
- }
Key =1,Value=www.c-sharpcorner.com
Key =2,Value=www.google.com
www.msn.com
- What is named parameter?
Named parameters provide provision for developers to pass parameters in any order.
- Here is the sample: static void Main(string[] args) {
- try {
- namedParameters(secondParameters: "this is second", thirdParameters: "this is third", firstParameters: "this is first");
- } catch (Exception e) {
- Console.WriteLine(e.GetType());
- throw;
- }
- }
- public static void namedParameters(string firstParameters, string secondParameters, string thirdParameters) {
- Console.WriteLine(firstParameters + "," + secondParameters + "," + thirdParameters);
- Console.ReadLine();
- }
When you execute the above code, you will see the below output.
“this is first, this is second,this is third”
- What are optional parameters?
Optional parameters provide provision to mark specific arguments as optional.
Here is the sample,
- static void Main(string[] args) {
- try {
- optionalParameters(secondParameters: "this is second", firstParameters: "this is first");
- } catch (Exception e) {
- Console.WriteLine(e.GetType());
- throw;
- }
- }
- public static void optionalParameters(string firstParameters, string secondParameters, string thirdParameters = "this is optional") {
- Console.WriteLine(firstParameters + "," + secondParameters + "," + thirdParameters);
- Console.ReadLine();
- }
Output
“this is first, this is second,this is optional”
- What is the output of the below code?
- static void Main(string[] args) {
- try {
- Console.WriteLine("Enum value for Orange is: " + (int) Colors.Orange);
- Console.ReadLine();
- } catch (Exception e) {
- Console.WriteLine(e.GetType());
- throw;
- }
- }
- public enum Colors {
- Blue = 1,
- Green = Blue | 2,
- Red = Blue | Green,
- Black = 4, White = 5,
- Orange = Red | White
- }
Enum value for Orange is: 7
- What is enum?
Enum is used to define a set of constants.
- What is the purpose of Action<T> delegate?
It encapsulates a method that has a single parameter and doesn’t return any value.
- What set of statements do you use in a continue statement?
while, do, for, or foreach.
- What is the purpose of XSD?
XSD is used to validate an XML document.