C# interview questions and answers

C# interview questions and answers
HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

The c# programming language provides the rapid application development and It is the best language for writing Microsoft .NET applications. Its syntax is similar to C++ syntax. Here are top Basic and Advanced C# Interview Questions and Answers for Freshers and Experienced Dot Net developers.

C# Basic Questions

1) What is a C# programming language?

C# is a modern, high level, general purpose object oriented programming language. This language is principal language of .Net framework. The design goal of the language was software robustness, durability & programmer productivity. It can be used to create a console application, GUI application, web application both on PC or embedded systems.

2) What are the types of comment in C# with examples?
  1. Single line
    //This is a Single line comment

  2. Multiple line
    /*This is a multiple line comment
    Here you can use more than 1 line*/

  3. XML Comments
    /// This is XML code Comment.
3) What is a class in c#?

A class describes all attributes of objects as well as the method that implements the behavior of member object. Basically, it represents a blue print of object. It is a template of an object. A class contains data & behavior of an entity.
Example: Aircraft.
Data: Model number of aircraft, Color of aircraft, category of aircraft etc.
Behavior/Method: Duration of flight, number of passengers etc.

4) What is an object?

An object is an instance of the class. It is a basic unit of the system. An object is an entity that has attributes, behavior & identity. Attributes & behavior of an object is defined by the class definition.

5) What is the relation between class and objects?

They both look very much same but they are not same. The class is definition while the object is the instance of the class created. The class is blue print while objects are actual objects existing in the world.
Example: Car has attributes & method like speed, brake, type of car etc.

6) List some of the advantages of C#?

Following are the advantages of C#

  1. Easy to learn
  2. Object oriented
  3. Component oriented
  4. Part of .NET framework
7) What are IDE’s provided by Microsoft for C# development?

Following are the IDE’s used for C# development

  1. Visual Studio Express (VCE)
  2. Visual Studio (VS)
  3. Visual Web Developer
8) What is the difference between a struct and a class in C#?

Struct & Class both are the user defined data type but have some major differences:

# Struct Class
1. Structure are value type in C# & it inherits from System.ValueType. Classes are reference type & it inherits from the System.Object Type.
2. Structure use stack to store value. classes use heap to store value.
3. Structure can not be declared as protected . Classes can not declare as protected.
4. We can not be able to use inheritance in Structure. We can use inheritance in classes.
5. The struct can have the only constructor. The class can have the constructor and destructor.
6. Structure objects can not destroy using GC. Objects created from classes are terminated using GC.
7. The struct can instantiate without using the new keyword.. The new keyword should be used to create the object for the class.
8. The struct can only inherit the interfaces. The class can inherit the interfaces, abstract classes.
9) What are the similarities between a struct and a class in C#?
  1. Both can have a constructor, method/functions, properties, fields, constants, enumeration, events & event handler.
  2. Structure & classes can implement the interface.
  3. Both of them can have a constructor with & without parameters.
  4. Both can have delegates & events.
10) What is oops and what are the different properties provided by oops?

Oops stands for object oriented programming system. It is a problem-solving technique to think real world problems in terms of objects.

Following are different properties provided by oops

  1. Inheritance: It is a process of forming new class from the existing class.
    Example: A scientific calculator is an extended form of a calculator. Here, the calculator is the parent & scientific calculator is child object.

  2. Polymorphism: The ability to use an operator or functions in different ways giving different meaning to function to the operator is called polymorphism.
    Example: A person is behaving like a Child, professor, student and then as a clerk. This is the power of polymorphism, a single object behaves in different forms.

  3. Encapsulation: Encapsulate means to hide also called as data hiding. Encapsulation is used to hide the code & data in a single unit. It is a process of hiding all the internal details of an object from the outside world.
    Example: Capsule: Capsule which hides medicine inside it.

  4. Abstraction: Abstraction means showing only necessary information & hiding unnecessary information from the user. Abstraction let you focus on what object does instead of how it does it.
    Example:Suppose u have a car, and u are riding car, now when you apply break you know that car is going to stop but you don't know what is the process happened when you applied break. Abstraction means putting all the variables and methods in a class that is necessary.

11) The Differences between Abstraction and Encapsulation in C#?
# Abstraction Encapsulation
1. Abstraction solves the problem at design level. Encapsulation solves the problem at the implementation level.
2. Abstraction is set to focus on the object instead of how it does it. Encapsulation means hiding the internal details or mechanics of how an object does something.
3. Abstraction is used for hiding the unwanted data and giving only relevant data. Encapsulation is hiding the code and data into a single unit to protect the data from the outer world.
4. Abstraction is implemented using interface & abstract class. Encapsulation is implemented using private & protected access modifiers.
5. Abstraction is outer layout in terms of design. Encapsulation is inner layout in terms of implementation.
e.g. Outer Look of an iPhone, like it has a display screen. Inner Implementation detail of an iPhone, how Display Screen are connected with each other using circuits.
12) Explain sealed class in C#?

The sealed class is used to stop the class from being inherited from other classes. If you define a class in C# as "Sealed" & "nonheritable" in VB.NET, then you can not inherit the class further.

13) What are value types in C#?

Value types directly contain data and derived from System.ValueType.
Value types use the stack to store value.
Example: int, float, char, Bool, short

14) What are reference types in C#?

reference types stored the address of memory where data is stored
reference types use the heap to store value.
Example: Class, Interface, String, Object, Delegate, Array

15) What is the difference between value types and reference types?
# Struct Class
1. They are stored on stack. They are stored on heap.
2. Memory is allocated at compile time. Memory is allocated at runtime.
3. The value type is popped on its own from the stack when they go out of scope. Required garbage collector to free memory.
4. Value type contains actual value. Reference type contains reference to a value.
5. Cannot contain null values. However, this can be achieved by nullable types. Can contain null values.
e.g. int, float, char, Bool, short. Class, Interface, String, Object, Delegate, Array.

« 1 2 3 4 5 »

Popular Posts