Please enable Javascript to correctly display the contents on Dot Net Tricks!
C# Questions & Answers

General

  1. What is C#?

    C# is pronounced as “See Sharp”. It is object oriented programming language developed by Microsoft which runs under .NET platform. Its syntax is similar to C++ or Java. It's most recent version C# 5.0 was released on August 15, 2012. It is widely used for developing web application, windows application, smart phone apps and games etc.

  2. What is the advantage of C# over Java?

    The advantageous features of C# over java are as follows.

    1. C# offers cross language interoperability or mixed language programming (Java lacking).

    2. C# directly supports windows operating system (Java lacking).

    3. C# is component-oriented language integrated support for writing of software support.

    4. C# support pointer as unsafe (Java lacking it).

  3. Explain evolution history of C#?

    The evolution history of C# is as follows:

    C# 1.0

    1. Managed Code

    2. IDE - Visual Studio 2002, 2003

    3. .NET Framework - 1.0, 1.1

    C# 2.0

    1. Generics

    2. Static Classes

    3. Partial types

    4. Anonymous methods

    5. Iterators

    6. Nullable types

    7. Asymmetric Property and Indexer Accessors

    8. Delegate Inference

    9. Covariance and Contra-variance

    10. IDE - Visual Studio 2005

    11. .NET Framework - 2.0

    C# 3.0

    1. Implicit types (var)

    2. Partial Methods

    3. Object and collection initializers

    4. Auto-Implemented properties

    5. Anonymous types

    6. Extension methods

    7. LINQ

    8. Query expressions

    9. Lambda expressions

    10. Expression trees

    11. IDE - Visual Studio 2008

    12. .NET Framework - 3.5

    C# 4.0

    1. Dynamic binding

    2. Named arguments

    3. Optional Parameters

    4. Generic Covariance and Contra-variance

    5. COM Interop

    6. IDE - Visual Studio 2010

    7. .NET Framework - 4.0

    C# 5.0

    1. Asynchronous methods

    2. Caller info attributes

    3. IDE - Visual Studio 2012, 2013

    4. .NET Framework - 4.5, 4.5.1

  4. Explain the new features of C# 5.0?

    The new features in C# 5.0 are given below:

    Async Feature (Asynchronous Methods)

    C# 5.0 Async feature introduces two keywords async and await which allows you to write asynchronous code more easily and intuitively like as synchronous code. Before C# 5.0, for writing an asynchronous code, you need to define callbacks (also known as continuations) to capture what happens after an asynchronous process finishes. This makes your code and other routine task such exception handling complicated.

    Both the keywords are used in a combination of each other. Hence, an await operator is applied to a one or more than one expressions of an async method. An async method returns a Task or Task<TResult> that represents the ongoing work of the method. The task contains information that the caller of the asynchronous method can use, such as the status of the task, its unique ID, and the method's result.

    public async Task<IEnumerable<Product>> GetProductList()
    {
     HttpClient client = new HttpClient();
     Uri address = new Uri("http://dotnet-tricks.com/");
     client.BaseAddress = address;
     HttpResponseMessage response = await client.GetAsync("myservice/product/ProductList");
     if (response.IsSuccessStatusCode)
     {
     var list = await response.Content.ReadAsAsync<IEnumerable<Product>>();
     return list;
     }
     else
     {
     return null;
     }
    }
    

    Caller Information (Caller info attributes)

    Caller Information can help you in tracing, debugging and creating diagnose tools. It will help you to avoid duplicate codes which are generally invoked in many methods for same purpose, such as logging and tracing.

    You could get the following information of caller method:

    1. CallerFilePathAttribute

      Full path of the source file that contains the caller. This is the file path at compile time.

    2. CallerLineNumberAttribute

      Line number in the source file at which the method is called.

    3. CallerMemberNameAttribute

      Method or property name of the caller.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    class Example
    {
     static void Main(string[] args)
     {
     Console.WriteLine("Main method Start");
     InsertLog("Main");
     MyMethodB();
     MyMethodA();
     Console.WriteLine("Main method End!");
     Console.ReadLine(); // hold on result
     }
     static void MyMethodA()
     {
     InsertLog("MyMethodA");
     MyMethodB();
     }
     static void MyMethodB()
     {
     // some code here. 
     }
     static void InsertLog(string method)
     {
     Console.WriteLine("{0} called MyMethodB at {1}", method,
     DateTime.Now);
     }
    }
    /* Output:
     Main method Start
     Main called MyMethodB at 11/17/2013 11:12:24 PM
     MyMethodA called MyMethodB at 11/17/2013 11:12:24 PM
     Main method End!
    */
    

    In both Main and MyMethodA, method InsertLog is invoked for logging. Now we can change the above code as follows.

    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;
    class Example
    {
     static void Main(string[] args)
     {
     Console.WriteLine("Main method Start");
     MyMethodB();
     MyMethodA();
     Console.WriteLine("Main method End!");
     Console.ReadLine();
     }
     static void MyMethodA()
     {
     MyMethodB();
     }
     static void MyMethodB([CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
     {
     InsertLog(memberName);
     }
     static void InsertLog(string method)
     {
     Console.WriteLine("{0} called MyMethodB at {1}", method, DateTime.Now);
     }
    }
    /*Output:
     Main method Start
     Main called MyMethodB at 11/17/2013 10:30:11 PM
     MyMethodA called MyMethodB at 11/17/2013 10:30:11 PM
     Main method End!
     */
    
  5. Explain the new features of C# 4.0?

    The new features of C# 4.0 are given below:

    Dynamic binding

    This feature is derived from dynamic languages such as Python, Ruby and JavaScript which resolve the types and members at runtime instead of compile time.

    dynamic d = "hello c#";
    Console.WriteLine(d.ToUpper()); // HELLO C#
    Console.WriteLine(d.Show()); // Compiles OK, but gives error at runtime
    

    Optional parameters

    In C# 4.0, methods parameters can be specified as optional by providing a default value. When the method having optional parameter is invoked, optional parameters can be omitted.

    void Sum(int a, int b = 10)
    {
     Console.WriteLine(a+b);
    }
    

    Above method can be called as:

    Sum(5); // 15
    

    Named arguments

    In C# 4.0, any argument can be passed by parameter name instead of parameter position. The Sum() can be called as :

    Sum(b:5,a:10); // 15
    

    Type variance

    C# 4.0, allows generic interfaces and generic delegates to mark their type parameters as covariant or contravariant. This enables the following code to work:

    IEnumerable<string> str;
    //TO DO: assign values to str
    IEnumerable<object> obj = str;
    

    COM specific interoperability features

    Dynamic binding as well as named and optional arguments help making programming against COM less painful than today. On top of that, however, we are adding a number of other features that further improve the interoperability experience specifically with COM.

  6. How C# code is executed?

    C# code execution life cycle diagram is shown as follows:

 
 
 
Free Interview Books