Thursday 9 March 2017

C# basic interview questions Part-7

1. Ques: Explain All the version (history) of C# ?
Answer:
C# is powerful object-oriented programming language developed by Microsoft.its first release in 2000. C# was introduced with .NET Framework 1.0 and the current version of C# is 6.0.

C# 1.0:
  • C# 1.0 was released by Microsoft with Visual Studio 2002. C# 1.0 targets .NET Framework 1.0. It was the first language adopted by developers to build .NET applications.
C# 1.2
  • C# 1.2 was released by Microsoft with Visual Studio 2003. C# 1.2 targets .NET Framework 1.1.
C# 2.0
  • C# 2.0 was released by Microsoft with Visual Studio 2005. C# 2.0 targets .NET Framework 2.0.
  • The new features added in C# 2.0 are as given below:
  • Generics
  • Partial types
  • Anonymous methods
  • Iterators
  • Nullable types
  • Private setters (properties)
  • Method group conversions (delegates)
  • Covariance and Contravariance

C# 3.0
  • C# 3.0 was released by Microsoft with Visual Studio 2008. It is also compatible with Visual Studio 2010. C# 3.0 targets the .NET Framework 2.0 (Except LINQ/Query Extensions), .NET Framework 3.0 (Except LINQ/Query Extensions) and .NET Framework 3.5.
  • The new features added in C# 3.0 are as given below:
  • Implicitly typed local variables (var)
  • Object and collection initializers
  • Auto-Implemented properties
  • Anonymous types
  • Extension methods
  • Query expressions
  • Lambda expressions
  • Expression trees
  • Partial Methods
  • LINQ

C# 4.0
  • C# 4.0 was released by Microsoft with Visual Studio 2010. C# 4.0 targets .NET Framework 4.0.
  • The new features added in C# 4.0 are as given below:
  • Dynamic binding (Late binding)
  • Named and optional arguments
  • Generic Covariance and Contravariance
  • Embedded interop types ("NoPIA")

C# 5.0
  • C# 5.0 was released by Microsoft with Visual Studio 2012. C# 5.0 targets .NET Framework 4.5.
  • The new features added in C# 4.0 are as given below:
  • Asynchronous methods
  • Caller info attributes

C# 6.0
  • Expression Bodied Methods
  • Auto-property initializer
  • nameof Expression
  • Primary constructor
  • Await in catch block
  • Exception Filter
  • String Interpolation


2. Ques: Difference between value type and reference types ?
Answer:
Value Type
  • Value Type stored on stack.
  • Value Type Contains actual value.
  • Cannot contain null values. However this can be achieved by nullable types.
  • Value type is popped on its own from stack when they go out of scope.
  • Memory is allocated at compile time.
  • In value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other.
  • Value types are derived from System.ValueType.
Reference Type
  • Reference Type stored on heap
  • Reference Type Contains reference to a value.
  • Can contain null values.
  • Required garbage collector to free memory.
  • Memory is allocated at run time.
  • In reference types, it is possible for two variables to reference the same object and thus possible for operations on one variable to affect the object referenced by the other variable.
  • Reference types are derived from System.Object.


3. Ques:  What are params in c# ? What is the use of params in C#?
Answer:
Params enables methods to receive variable numbers of parameters. With params, the arguments passed to a method are changed by the compiler to elements in a temporary array. This array is then used in the receiving method.
Ex:

class Program
{
  static void Main()
    {
        // Call params method with one to four int arguments.
        int sum1 = SumParameters(1);
        int sum2 = SumParameters(1, 2);
        int sum3 = SumParameters(3, 3, 3);
        int sum4 = SumParameters(2, 2, 2, 2);
    }

    static int SumParameters(params int[] values)
    {
        // Loop through and sum the integers in the array.
        int total = 0;
        foreach (int value in values)
        {
            total += value;
        }
        return total;
    }
}

By using the params keyword, you can specify a method parameter that takes a variable number of arguments. You can send a comma-separated list of arguments of the type specified in the parameter declaration or an array of arguments of the specified type.


4. Ques:  What is the difference between Array and Collection?
Answer:
Array 
  • You need to specify the size of an array at the time of its declaration.It can not be resized.
  • The member of an array should be of the same data type.
  • Array is strong type. 
Collection
  • The size of a collection can be adjusted dynamically as per users requirement.It does not have fixed size
  • Collection can have different data types.
  • Collection is not strong type.

5. Ques:  What is the difference between managed and unmanaged code?
Answer:
Managed Code
  • Code which is executed by CLR (Common Language Runtime) is called Managed Code, any application which is developed in .Net framework is going to work under CLR, the CLR internally uses the Garbage Collector to clear the unused memory and also used the other functionalities like CTS, CAS etc.
  • managed code executed on fixed location in memory.
  • it  get advantages of CLR like memory management, thread managament,security, garbage mechanism etc.
Unmanaged Code
  • The unmanaged code is basically developed using other languages (other than .Net Framework), so it uses its own language runtime to execute the applications. The application runtime will take care of its memory management, security etc.
  • unmanaged code executes on random location in memory.
  • it doesn't get advantages of CLR like memory management, thread managament,security, garbage mechanism etc.

6. Ques: What is the use of using statement in C# ?
Answer:
The using statement is used to obtain a resource, execute a statement, and then dispose of that resource.

There is 2  main use using statement. 
1:- Importing namespace example:using System; 
2:- Dispose the object  
 using(SqlConnection oConnection=new SqlConnection()) { oConnection.Open(); } 
if you won't close the connection it will close the connection automatically once SqlConnection has been executed completing.



7. Ques: What is Delegates in C# ? What is Use of Delegates ?
Answer:
Delegates in c# are type safe objects which are used to hold reference of one or more methods in c#.net. Delegates concept will match with pointer concept of c++ .
The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked

Whenever we want to create delegate methods we need to declare with delegate keyword and delegate methods signature should match exactly with the methods which we are going to hold like same return types and same parameters otherwise delegate functionality won’t work if signature not match with methods.

Delegates are two types
Single Cast Delegates
Multi Cast Delegates

Single Cast Delegates
Single cast delegate means which hold address of single method like as explained in above example.

Multicast Delegates
Multi cast delegate is used to hold address of multiple methods in single delegate. To hold multiple addresses with delegate we will use overloaded += operator and if you want remove addresses from delegate we need to use overloaded operator -=

Multicast delegates will work only for the methods which have return type only void. If we want to create a multicast delegate with return type we will get the return type of last method in the invocation list


8. Ques: What is the use of “Yield” keyword in C#?  What is use the of “Yield” keyword ?
Answer:
Yield helps you to provide custom stateful iteration over .NET collections.”

Use of “yield” keyword is
  • Customized iteration through a collection without creating a temporary collection.
  • Stateful iteration.
There are a few things to note when defining a function that uses yield. The yield statement can only appear inside an iterator block, which can be implemented as the body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following restrictions:
  • Unsafe blocks are not allowed.
  • Parameters to the method, operator, or accessor cannot be ref or out.
  • A yield return statement cannot be located anywhere inside a try-catch block. It can be located in a try block if the try block is followed by a finally block.
  • A yield break statement may be located in a try block or a catch block but not a finally block.

9. Ques:  What is the Task Parallel Library (TPL) in C#? 
Answer:
Task Parallel Library is new API's to simplify the process of adding parallelism and concurrency to applications. This set of API's is called the "Task Parallel Library (TPL)" and is located in the System.Threading and System.Threading.Tasks namespaces.
The Parallel class found in the System.Threading.Tasks namespace “provides library-based data parallel replacements for common operations such as for loops, for each loops, and execution of a set of statements”. In this article, we will use the  Invoke method of the Parallel class to call multiple methods, possibly in parallel.
 
   TPL handles the partitioning of the work, the scheduling of threads on the ThreadPool, cancellation support, state management, and other low-level details. By using TPL, you can maximize the performance of your code while focusing on the work that your program is designed to accomplish.

No comments:

Post a Comment

Thank you for comment