Photo Gallery

Wednesday, June 21, 2017

C # Interview Questions

Here, you can see all list of interview questions which are frequently asked during an interview.


If you have any question that is asked in a interview, please post it here.

C# ?
C Sharp is the most commonly used language for leveraging the .net framework. C# is an object oriented, simple and type safe managed language. Its similar to JAVA and C++. It is compiled by .NET framework using IDE.

What are the new features in C# 6.0?
Its a new and improved version and has following features,

1) Auto Property initializer
Example :
public string Name {get; set;} ="Chintan" ;

2) using static type to reduce duplication.
Example :
using static System.Console;
using static NewCsharp.TestClass;

namespace NewCsharp
{
class Program
{
static void Main(string[] args)
{
Test();
ReadKey();
}
}

static class TestClass
{
public static void Test()
{
WriteLine("Test Method");
}
}
}

3) Null Conditional Operator to prevent null reference exception
?. is used to check if instance is null or not, if it's not null then execute the code after ?. but if it's null then execute the code after ?? .
Example:
empSalary=emp ?. Details ?. salary ?? 0;

4) Index Initializer with Dictionary
Remove curly braces and use square brackets [] .
Example:
Dictionary employee= new Dictionary()
{
["Name"] = "Chintan",
["Department"] = "IT"
};

5) String interpolation
we can directly use string variable instead placeholder.
Example:
string name = "Chintan patel";
string department = "IT";
WriteLine("{name } is working in {department} at Virtusa");
ReadLine();

What is Framework ?
Framework is a collection of classes.

What is Namespace ?
Namespace is used to organized the class, code or program. In other words, logical grouping of class, interface and other members of class.

What is class ?
Its a blueprint of an object or template. It describe attribute, properties and method.

What is an object ?
Its an instance of class. In other words, its an entity that has attribute, behavior and an identity.

What is Method (re: Function ) ?
It contain series of code or statement. It allows you to define logic once and use it at many places and performed by class object.

What is constructor ?
Its a method in a class, same name as class name and always public. Its used to initialize the fields of class. It gets executed when object is created.

Code snippet for constructor - ctor and click tab.

Example -
public class TestClass
{
public string name {get; set;}

//constructor
public TestClass()
{

}
}

OOP's concept ?
Object Oriented Program techniques to develop classes. Program considered as a collection of classes.

What are the OOP's Features ?
1) Abstraction : Exposed only relevant data to user.
Example - Access modifier (public, private, protected etc.)

2) Polymorphism : Allows to use entity in the multiple format.
Example - Overriding, overloading , inheritance

3) Encapsulation : Prevent data from unwanted access, in other words properties, methods and other class members treated as single unit called object.
Example - Access modifier

4) Inheritance : Reusability of code to minimize redundancy of code. It reduce the time and error. In other words, derive class from existing class.

What is an Abstract Class ?
It contains one or more abstract methods. An abstract method is declared but contains no implementation.
Abstract Class may not be instantiated, it require its sub classes to provide implementations for those abstract methods.

When to use Abstract Classes?
Its useful when we need a class for the purpose of polymorphism or inheritance. These classes are used when we want to define standard template for a sub classes that share some common logic or code. And one more thing that if we want to guarantee that the object of the super class can not be created.

For example:  if we have to create report at two different level and those two report has common method or functionality, so we can create those common functionality method in the super class as BaseReport and you can define other method associate to particular level only in subclass.

Abstarct class have one more benefit in polymorphism which allows to use abstract type as a method argument or return type.
For Example : subclass(Superclass object)

What is Boxing and Unboxing ?
Boxing - Converting a value type into reference type
Example -
int i = 7;
object obj = (object)i;

Unboxing - Converting a reference type into value type
Example -
obj = 7;
i = (int)obj;

Generics ?
It introduced in C# 2.0 and allows us to design classes & methods decoupled from data types.
Generics present in system.collection.generics namespace.

Advantage of using Generics - we can have type safety & we can avoid boxing/unboxing.
Example - List , hashtable etc

- Code reusability
- type safety
- better performance : no type casting
- clean code

What is Dictionary in C#?
A Dictionary is a collection of key and value pair of data. A Dictionary class is a generic class and present in System.Collections.Generic namespace. Before using dictionary, you must import the above mentioned namespace as below,

using  System.Collections.Generic

How to Create the Dictionary in C# ?
When creating a dictionary, we must need to specify data type for key and value.

Dictionary class has Add, Remove, Clear and other methods.
Example-

// Create Dictionary
Dictionary<int, string> dictionaryEmployee = new Dictionary<int, string>();

// Add data into Dictionary
dictionaryEmployee.Add(1,"Chintan");
dictionaryEmployee.Add(2,"Hitesh");
dictionaryEmployee.Add(3,"Madhav");

// Read data from Dictionary
foreach( KeyValuePair<int, string>employee in dictionaryEmployee )
{
Console.WriteLine("Employee ID= {0}, EmplyeeName= {1}",
employee .Key,
employee .Value);
}

// Results
Employee ID=1,EmplyeeName=Chintan
Employee ID=2,EmplyeeName=Hitesh
Employee ID=3,EmplyeeName=Madhav

Difference between string(small) and String(capital) ?
There is no difference between string and String, both are same and one. small string is alias for System.String. It is recommended to use small string as this is the alias for that class. MSDN reference for aliases of predefined types in the System namespace Types in C#.

What is an Interface? 
Interface is a way to define a contract, in other words if a class implements such kind of interface, then class has all the methods and properties defined in the interface contract.

More added soon...

If you have any questions, post here. Also, provide your valuable suggestions and thoughts in form of comments in the section below.

Thank you !!!

3 comments: