Professional Documents
Culture Documents
applications, desktop applications, and mobile apps. This article contains the top 20
C# interview questions and answers, in order to prepare you for the interview.
1. What is C#?
C# pronounced as "See Sharp". It is an object-oriented programing
language developed by Microsoft, which runs under the .NET platform. Its syntaxes are
similar to C++ or Java. The most recent version of C# is 7.3 which is introduced with
Visual Studio 2017 update 15.7. C# is widely used for developing web applications,
desktop applications, mobile apps, and games, etc. Now, C# can be run on Mac,
Linux/Unix and Windows using .NET Core.
C# 1.0
Managed Code
IDE - Visual Studio .NET 2002, 2003
.NET Framework - 1.0, 1.1
C# 2.0
Generics
Static Classes
Partial types
Anonymous methods
Iterators
Nullable types
Asymmetric Property and Indexer Accessors
Delegate Inference
Covariance and Contra-variance
.NET Framework - 2.0
C# 3.0
Implicit types (var)
Partial Methods
Object and collection initializers
Auto-Implemented properties
Anonymous types
Extension methods
Query expressions
Lambda expressions
Expression trees
IDE - Visual Studio 2008
C# 4.0
Dynamic binding
Named arguments
Generic Covariance and Contra-variance
COM Interop
IDE -Visual Studio 2010
.NET Framework - 4.0
C# 5.0
Asynchronous methods
Caller info attributes
IDE - Visual Studio 2012, 2013
.NET Framework - 4.5, 4.5.1
C# 6.0
Auto Property Initializer
Primary Constructors
Dictionary Initializer
Declaration Expressions
Static Using
await inside catch block
Exception Filters
Null-Conditional Operator
IDE - Visual Studio 2015
.NET Framework - 4.6
C# 7.0
Local Functions
Literal improvements
Digit Separators
Pattern matching
ref returns and ref Locals
Throw Expressions
Expression Bodied Members
Deconstruction
7. What is Datatype?
Data Type refers to the type of data that can be stored in a variable. It also specifies
how much memory would be allocated to a variable and the operations that can be
performed on that variable. C# is rich in data type which is broadly divided into two
categories.
Value Type
Reference Type
8. What is the difference between value type and reference
type?
Value Type: A value type variable stores actual values. Values types are of two types -
built-in and user-defined. Value types are stored in a stack and derived from
System.ValueType class.
0.
1. int x = 10;
2. char ch = 'a';
3.
Reference Type: A reference type variable stores a reference to the actual value.
Typically, a reference type contains a pointer to another memory location that stores
the actual data. Reference types are of two types - built-in and user-defined. Reference
types are stored in a heap and derived from System. Object class.
Note
You should not declare a string as nullable since it is implicitly nullable. Hence,
declaring a string type like string? firstNamecauses a compile-time error.
Key Points
The Passing argument must be initialized before passing to the method using the
ref keyword.
A Property cannot be passed as ref parameter since internally property is a
function.
Out – The out keyword also makes an argument (data) to be passed by reference to
the corresponding parameter, but that argument can be passed without assigning any
value to it.
Key Points
The passing argument must be initialized in the passed method before it returns
back to the calling method.
Also, a Property cannot be passed as out parameter since internally property is a
function.
For example, A program uses ref and out keyword as method argument.
4. public class MyClass
5. {
6. public static void Main()
7. {
8. int arg1 = 0; //must be initialized
9. int arg2;
10.
11. MyMethod1(ref arg1);
12. Console.WriteLine(arg1); // Now 2!
13.
14. MyMethod2(out arg2);
15. Console.WriteLine(arg2); // Now 3!
16. }
17.
18. static void MyMethod1(ref int param1)
19. {
20. param1 = 1; //initialization is optional
21. }
22. static void MyMethod2(out int param2)
23. {
24. param2 = 2; //must be initialized
25. }
26. }
27.
28. /* Output
29. 1
30. 2
31. */
32.
Key points
It is a special function/method because its name is the same as the class.
It can be overloaded the same as normal methods.
It is automatically invoked as soon as the object of the class is created.
It does not return a value, not even a void type.
0. Default Constructor
1. Parameterized Constructor
The parameterized constructor has one or more arguments and used to assign
values to instance variables of the class.
1. class Example
2. {
3. double num1, num2;
4.
5. #region Constructor Overloading
6.
7. //Default Constructor
8. public Example()
9. {
10. Console.WriteLine("This is a default constructor!");
11. }
12.
13. //Parameterized Constructor
14. public Example(int a, int b)
15. {
16. num1 = a;
17. num2 = b;
18. Console.WriteLine("This is a parameterized constructor!");
19. }
20.
21. //Parameterized Constructor
22. public Example(double a, double b)
23. {
24. num1 = a;
25. num2 = b;
26. Console.WriteLine("This is a parameterized constructor!");
27. }
28. #endregion
29.
30. public void ShowResult()
31. {
32. double result = num1 + num2;
33. Console.WriteLine("Result: {0}", result);
34. }
35. }
36.
37. class Program
38. {
39. static void Main()
40. {
41. Example obj1 = new Example(); //default constructor called
42. Example obj2 = new Example(4, 5); //parameterized constructo
r called
43. Example obj3 = new Example(4.2, 5.2); //parameterized constr
uctor called
44.
45. obj2.ShowResult();
46. obj3.ShowResult();
47. Console.Read();
48. }
49. }
50.
51. /* OutPut
52. This is a default constructor!
53. This is a parameterized constructor!
54. This is a parameterized constructor!
55. Result: 9
56. Result: 9.4
57. */
58.
2. Copy Constructor
This constructor is used to copy the entire values of an object to another object.
But C# does not support copy constructor.
//destructor
~Example()
{
Console.WriteLine("Destructor called to destroy the instance");
}
}
class Program
{
static void Main()
{
Example T = new Example();
GC.Collect();
}
}
/*Output
Constructor called.
Destructor called to destroy the instance
*/
For example, you can use one thread to monitor input from the user, second thread to
perform background tasks, third thread to process user input and fourth thread to
show the output of the third thread processing.