Below is top 100 C# interview question and answers with examples for freshers and intermediate developers.
C# is a general-purpose, object-oriented programming language developed by Microsoft around 2000. It is strongly type and it encompasses imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.
Current version of C# is C# 7.3 (as of this writing(Aug 30, 2018)). Below are the version history of C#.
|C# 1.0||January 2002|
|C# 1.1||April 2003|
|C# 1.2||April 2003|
|C# 2.0||November 2005|
|C# 3.0||November 2007|
|C# 4.0||April 2010|
|C# 5.0||August 2012|
|C# 6.0||July 2015|
|C# 7.0||March 2017|
|C# 7.1||August 2017|
|C# 7.2||November 2017|
|C# 7.3||May 2018|
Below are the new features added with C# versions.C# 7.3
Below is the main oops concepts of C#.
Inheritance is when an object or class is based on another object or class, using the same implementation or specifying implementation to maintain the same behaviour.
Encapsulation is a packing of data and functions into a single component. It prevents access to implementation details. Class is an example of encapsulation
Abstraction refers to represent essential data without including the background details. Abstractions can be achieved by means of access specifiers
Polymorphism means one object behaving as multiple forms. There is compile time and runtime polyporphism.
In compile time polymorphism, we can declare methods with same name but different parameters and it is called as method overloading.
Runtime polymorphism can be achieved by method overriding.
A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type.
An object is basically a block of memory that has been allocated and configured according to the class or struct (blueprint).
Objects are also called instances, and they can be stored in either a named variable or
in an array or collection.
Client code is the code that uses these variables to call the methods and access the public properties of the object. In an object-oriented language such as C#, a typical program consists of multiple objects interacting dynamically.
Value Type A value type variable holds data value within own memory space that means this variable contains their value directly. Data of value type stored in Stack memory. Below data types are value type.
1. Value types are stored on the stack where as reference types are stored on the managed heap.
2. Value type variables directly contain their values where as reference variables holds only a reference to the location of the object that is created on the managed heap.
3. There is no heap allocation or garbage collection overhead for value-type variables. As reference types are stored on the managed heap, they have the over head of object allocation and garbage collection.
4. Value Types cannot inherit from another class or struct. Value types can only inherit from interfaces. Reference types can inherit from another class or interface.
Passing by Value When you pass a value type variable from one method to another method, the system creates a separate copy of a variable in another method, so that if value got changed in the one method won't affect on the variable in another method.
Passing by Reference When you pass a reference type variable from one method to another, it doesn't create a new copy; instead, it passes the address of the variable. If we change the value of the variable in a method, it will also be reflected in the calling method.
A namespace is used to organize your code logically. A namespace in collection of classes, interface, structs, enums, delegates and other namespaces.
The ref and out keywords are used to pass the parameter to the function. Both Ref and out keywords are used to pass parameter by reference. ref: The ref keyword is used to pass parameter as reference. If value of parameter is changed in called method then parameters changed value is also gets reflected in calling method. The parameter must be initialized in calling method before it passed to called method. out: The out keyword is also used to pass parameter as reference. Here, initialization of parameter is not necessary in calling method before passing parameter to called method. But, initialization is compulsory in called method before returning back to called method. Ex.
1) A data type of value type or reference type can be pass to method by ref keyword. But, if a value type is passed using ref keyword then boxing does not occurs.
2) Properties cannot be passed using ref or out keyword as they are implemented as function internally.
The process of converting value type to reference type is boxing and reference type to value type in unboxing. Boxing and unboxing is not a data type conversion. Boxing is basically storing a value type as an object in heap and unboxing is reading the value from the object. Ex.
Yes, boxing happens implicitly.
No, unboxing is an explicit conversion.
Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object. Due to this boxing and unboxing can have performance impact.
Converting a variable of one data type to another data type is called casting. This is also called as data type conversion.
Implicit conversions: No special syntax is required because the conversion is type safe and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes. Explicit conversions: Explicit conversions require a cast operator. The source and destination variables are compatible, but there is a risk of data loss because the type of the destination variable is a smaller size than (or is a base class of) the source variable.
Explicit conversions require a cast operator where as an implicit converstion is done automatically.
Explicit conversion can lead to data loss where as with implicit conversions there is no data loss.
double d = 9999.11;
int i = d;
No, the above code will not compile. Double is a larger data type than integer. An implicit conversion is not done automatically bcos there is a data loss. Hence we have to use explicit conversion as shown below.
double d = 9999.11;
int i = (int)d; //Cast double to int.
Explicit conversion as shown below. Consider Vehicle as base class and Car as derived class.
//Create a new derived type.
Car C1 = new Car();
// Implicit conversion to base type is safe.
Vehicle V = C1;
// Explicit conversion is required to cast back to derived type. The code below will compile but throw an exception at run time if the right-side object is not a Car object.
Car C2 = (Car) V;
The is and as operators can be used to cast from one reference type to another without the risk of throwing an exception.
Yes, Value types are sealed.
1. Single Line Comments. You define single line comments with // as shown below.
// This is an example for single line comment
2. Multi line comments. You define multi line comments with /* */ as shown below.
This is an example for
Multi Line comments
3. XML Comments. You define XML comments with /// as shown below.
/// This is an example for defining XML comments.
You use the struct, class, interface, and enum constructs to create your own custom types. The .NET Framework class library itself is a collection of custom types provided by Microsoft that you can use in your own applications.
A class is a reference type means when an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.
A struct is a value type mean when a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.
A class is used to model more complex behavior, or data that is intended to be modified after a class object is created. A struct is best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.