Top 100+ popular C# Interview Questions and Answers.

 


Ø  What is Var Type?

ü  Var is type which is declared without .Net data Type.

ü  Implicitly type variable.

ü  IT will automatically convert variable at compile time according the value is assign to this variable.

ü  Introduce in C# version 3.0

ü  Value must assign at the time of declaration.

Ø  What is Dynamic Type?

ü  Dynamic is type which is declared without .Net Data type.

ü  Implicitly type variable.

ü  It will automatically convert variable at run time according to value assign to this variable.

ü  Introduce in C# version 4.0

ü  Doesn’t require assigning a value at the time of declaration.

Ø  What is Object?

ü  Object is type which declared without .Net Data type.

ü  The compiler has little information about type.

ü  We need to convert explicitly when assign in .Net Data type.

ü  Introduce in C# version 1.0

ü  Doesn’t require assigning a value at the time of declaration.

Ø  Difference between Var And Dynamic

Var

Dynamic

Object

Introduce in C# version 3.0

 

introduced in C# 4.0

introduced in C# 1.0

Variable type decided at compile time.

Variable type decided at run time.

The little information about the data type.

Does require initializing variable at the time of declaration.

Doesn’t require initializing variable at the time of declaration.

Doesn’t require initializing variable at the time of declaration.

The var type can’t be passed as a method argument and method return type.

The dynamic type can be passed as a method argument and method return type.

The object type can be passed as a method argument and method return type.

We can’t change variable data type at run time.

Ex. var a=10;

A=”ten”;

This isn’t work because type is decided at compile time so it will return compile time error.

We can change data type of variable during run time.

Ex. dynamic a=10;

A=”ten”;

This is work

We can change data type of variable during run time.

Ex. object a=10;

A=”ten”;

This is work

Explicit conversion is not required when we convert into actual .net data type.

Explicit conversion is not required when we convert into actual .net data type.

Explicit conversion is  required when we convert into actual .net data type.

               

 

Ø  What is readonly keyword in c#?

ü  Readonly is the keyword whose value we can assign during runtime.

ü  We can assign it runtime only through not static constructor.

ü  We can’t assign redonly variable value by class method.

ü  We can’t able to assign value within static constructor.

Ø  What is constant keyword in c#?

ü  Constant is the keyword whose value assign during compile time.

ü  Once value is assigned can’t change at runtime or throughout the program.

Ø  What is IEnumerable interface and why?

ü  IEnumerable is interface allow to iteration (foreach) over generic or non-generic collection.

ü  IEnumerable return readonly collection,  we can’t add or insert new value in the collection.

ü  IEnumerable is the base interface of all non-generic collection.

ü  IEnumerable has one method GetEnumerator() which return IEnumerator interface, this allow to iterate readonly collection.What is interface IEnumerable ?

ü  IEnumerable doesn’t allow random access from collection like fetch value using integer index.

Ø  What is IEnumerator interface and why?

ü  IEnumerator is interface allow to iteration (foreach) over generic and non-generic collection.

ü  It will use the methods like Reset, MoveNext, Current for iterate the collection.

ü  IEnumerator return readonly collection,  we can’t add or insert new value in the collection.

ü  If we want to retain the cursor position and pass this collection one function to another then use IEnumerator.

ü  Ex.

        IEnumerator ienumeraterList = new[] { "a", "b", "c", "d" }.ToList().GetEnumerator();

       while (ienumeraterList.MoveNext())

            {

                Console.WriteLine(ienumeraterList.Current);

            }

Ø  What is IQueryable interface and why?

ü  IQueryable exists in System.Linq  namespace.

ü  IQueryable is used for fetching data from database collection.

ü  IQueryable executes a query on database server with all filters.

Ø  Which is better IEnumerable or  IQueryable?

ü  IEnumerable executes select query on database server side, load all data in client machine and after that apply filter like where condition.

ü  IQueryable executes whole query on database server with all filters after that load data in client machine.

Ø  What is StringBuilder?

ü  StringBuilder is Mutable (Can change) type, means a new instance is not created every time in memory when we change the instance value.

ü  Namespace System.Text.

ü  Performance-wise faster due to not create unnecessary a new instance in memory while changing the instance value.

ü  We can modify the StringBuilder type value using a method like Append, Insert, Remove, AppendFormat, Replace

Ø  Difference between Mutable and Immutable string in C#?

Mutable (String)

Immutable (StringBuilder)

Mutable means ‘Can Change’.

Immutable means ‘Can’t change’.

StringBuilder is a mutable type.

String is an immutable type.

Can’t create a new instance in memory when we change the instance value.

Create a new instance in memory when we change the instance value.

Ex.

string myInstance = string.Empty;

 

for (int i = 0; i < 3; i++)

{

myInstance += "Total instance created:" + i;

}

 

In above example variable will modify 3 times within loop, So it will create 3 instances in memory. It will create garbage value.

Ex.

StringBuilder myInstance = new StringBuilder();

for (int i = 0; i < 3; i++)

{

myInstance.Append("Total instance created only once");

}

 

In above example variable will modify 3 times within loop, But instance created only once in memory, So it will not create garbage value.

Performance-wise slower due to create unnecessary instances during value change or modification.

Performance-wise faster because doesn’t create unnecessary instance during value change or modification.


Ø  What is Abstract class in C#?

ü  An abstraction is the process of hide the internal details and showing only the functionality.

ü  An abstract class declares with abstract keyword.

ü  An abstract class may or may not abstract method and properties.

ü  Can’t create object of abstract class.

ü  An abstract method has no body (only declaration of method).

ü  An abstract classes are used for define base class in the class hierarchy.

ü  An abstract method must be implemented within derived class.

ü  An abstract method never declares as private modifier.

ü  User must use the override keyword in the child class to implement abstract method.

ü  An abstract class can’t have constructers or destructors.

ü  It can support implementation within non abstract method.

ü  An abstract class can’t declare as static.

ü  Class can’t implement multiple abstract class

Ø  What is Interface in C#?

ü  We can achieve abstraction using interface.

ü  An interface contains only declaration of method (fully abstract class) not implementation.

ü  Method must be implemented within derive class.

ü  Method has no any access modifier by default public.

ü  Class can implement multiple interfaces.

ü  We do not have to use override keyword when implement in derived class.

ü  Interface cannot contain fields, and auto-implemented properties.

ü  Use public modifier when implementing an interface implicitly, whereas don’t use it in case of explicit implementation.

ü  Example of implicitly interface:

                                interface IFile

              {

                     string ReadFile();

              }

              class FileInfo : IFile

              {

                     public string ReadFile()

                     {

                            return "Reading Text File";

                     }

              }

ü  Example of explicit interface:

                                interface IFile

              {

                     string ReadFile();

              }

              interface IBinaryFile

              {

                     string OpenBinaryFile();

                     string ReadFile();

              }

             

              class FileInfo : IFile, IBinaryFile

              {

                     string IFile.ReadFile()

                     {

                            return "Reading Text File";

                     }

 

                      string IBinaryFile.OpenBinaryFile()

                      {

                           return "Opening Binary File";

                      }

 

                     string IBinaryFile.ReadFile()

                     {

                           return "Reading Binary File";

                     }

              }

Ø  When to use abstract class and when to use interface?

ü  Interface: If child classes must have to implement some group of the method as per the requirement in that case we use interface (we can say contract between interface and child class).

Ex. In Car color, gear, the passenger is different for each model, so in that case, we can use interface so child class is free to decide their price, color and the passenger capacity.

ü  Abstract Class: If base class should provide some predefine implementation of the method to their child classes and another method should be open to implementing as per their requirements in that case use an abstract class.

Ex. Let’s say Mobile base price is fixed including all government tax, but store may be giving some special discount to their Silver customer or gold customer, in that case, we use abstract class. In abstract class, one method is implemented for returning mobile base price, when one method declare as abstract to decide selling price including discount as per customer type.

[When to use abstract class and interface in C#? With Example]

Ø  What is Async and Await keyword in C#?

ü  Async and Await are the code markers, which indicate where the controller should resume after the task completion.

ü  Introduce in .Net Framework 4.5.

ü  We can run all the methods parallels using Async and Await keyword.

ü  In the traditional method, we are using Thread mechanisms to achieve parallel processing but it will block the whole UI until the all process not done. We can resolve this problem using simple Async and Await keyword.

ü  If method one is dependent on method two in that case Await keyword is waiting for completion of method one after that method two will start.

ü  We can’t use Async without the Await keyword or vise-versa in the code.

[Async and Await in C# with Example]

Ø  Can a private virtual method be overridden in the derived class?

ü  First, we can’t declare the virtual method as private.

ü  With a virtual keyword, we can use protected or public access modifiers.

ü  So the answer is not possible to private virtual method be overridden in the derived class.

Ø  If we want to use "using" keyword, which interface should be implemented on a class?

ü  Dispose() of IDisposable interface implemented in your class.

Ø  Can we use "this" keyword within a static method?

ü  Answer is no because the static method does not require any object to call, and this keyword always points to a current object of a class.

ü  So we can’t use this keyword with a static method.

ü  But in C# 3.0 we can use this keyword with static method in case of EXTENSION METHOD.


Next Recommended Article:

  1. When to use abstract class and interface in C#?
  2. Async and Await in C#.
  3. Optimized top 2 ways to find 2nd or Nth highest salary in SQL Server.
  4. Different ways to remove or delete duplicate rows from a SQL Table.
  5. Top 100+ popular ASP.Net MVC Interview Questions and Answers.
  6. Top 100+ popular SQL Interview Questions and Answers Blog.
  7. @Input, @Output decorator and EventEmitter class in Angular.
  8. Dependency Injection and types of dependency injection.


 

No comments:

Post a Comment