51 C# Interview Questions and Answers

C# Interview Questions and Answers {C# .Net Interview Questions}

1. What is C#?

C# is one of the best programming language for writing and rapid development of .Net Applications. Syntax of C# is very similar to that of C++. C# is a general purpose object oriented programming language.

You can find C# Programming guide here.

2. What is the latest version of C#?

The latest version of C# is 7.2 which is with the Visual Studio 2017 version 15.5.

3. What are new features in version 7.2?

  • Reference semantics with value types
  • Non-trailing named arguments
  • Leading underscores in numeric literals
  • private protected access modifier

4. Describe about Syntax of C#

The syntax of C# is similar to C/C++ and java.

  1. Variables are assigned value using equal sign(‘=’).
  2. Semicolons ; are used at the end of each statement.
  3. Square brackets [ ] are used with arrays.
  4. Curly brackets { } are used for grouping statements in functions, classes or other conditions.

5. What is the difference between C# and .Net?

  • C# is an object-oriented programming language built on .Net framework for the development of applications while .Net is an application development framework which contains libraries having a lots of functionalities,
  • .Net support many languages like VB.Net, C#.Net, C++.
  • C# and C#.Net are the same things.
  • The .NET libraries can run on the Common Language Runtime(CLR) and thus any language which can run on the CLR can also use the .NET libraries.

You can view detail here: What is the difference between C# and .NET?

6. What is object?

Object is an instance of class which is created dynamically. When we instantiate an object, a block of memory is allocated. The objects can be user defined, reference or value type and inherit directly or indirectly from system.

Complete description about objects: View

Managed code: This type of code executes under the Common Language Runtime virtual machine. The languages which are written in .Net Framework are managed codes.

Unmanaged code: Such codes are developed outside the .Net Framework and are called unmanaged codes. The application that do not run under Common Language Runtime are called unamanged codes.

11. What are the properties in C#?

Properties in C#.net provide a proper mechanism of read, write or compute the values of private fields. Property is a return type function which has one or no parameter. Accessors are used to access and assign the values to private fields.

There are three types of properties i.e. Read/Write, ReadOnly, WriteOnly.

12. What are the accessors?

These are used to restrict the accessibility of property. With the help of get accessor we access the values of private fields while with the set accessor, we can assign value to private fields.

13. What is virtual function in C#?

Virtual functions are used when we implement a defined function in inherited class. These functions are implemented differently in different inherited classes.

14. Operator overloading in C#

In operator overloading special keyword operator is used following by symbol for operator being defined. Like other functions overloaded operator has return type and parameters.

Built-in operators are overloaded in C#. The programmer has also option of overloading user defined types.

15. What is enum in C#?

enum keyword is used to declare enumeration. It is primitive user defined data type that consists of named constants called enumerator list.

enum week_days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};

The default type of enum is int, however, enum types can be byte, sbyte, short, ushort, uint, long or ulong. The underlying type specify how much storage is allocated to each enumerator. An enumerator cannot contain white space in its name.

16. What is early binding?

Early binding is way of linking a function with the object during compile time. This process is also called stating binding.

17. How to pass parameters to a function in C#?

You can pass parameters to a function or method by using following three ways.

  • By Value: You can pass parameters by value. In this way new storage location is created in memory for value parameter.
  • By Reference: To pass parameter by reference ref keyword is used. In this type function accesses the memory location of argument and passes it to function.
  • Output Parameter: In this type out keyword is used. It allows the function to return two values. It is similar to passing parameter by reference except that data is transferred out of the function.

18. What are nullable types in C#?

Nullable data types contain defined data type for null, in addition to their normal values. These are used to integrate C# to work with value types, and databases that often use null values. Following syntax is used to declare a nullable type in C#:

?  = null;

19. What is the difference between ref and out keywords?

ref keyword is used to indicate the parameter is passed by reference while out keyword is similar to ref keyword except that ref requires variable be initialized before it is passed.

20. What is difference between struct and a class in C#?

struct : struct is used to create structure value type to represent a record. These play similar role as classes. struct are value type while classes are reference type. Since struct are value type, therefore, these are allocated and deallocated on the stack or inline.

class : Class is reference type and is allocated and deallocated on the heap and garbage collected and tends to consume more resources. In case of boxing and unboxing, a value type can get boxed when it is cast to a reference type as boxes are objects allocated to the heap and deallocated via garbage collection. Too much boxing and unboxing of a value can negatively impact the performance. Reference types such as classes are preferred in above situation.

21. What is polymorphism and how polymorphism is implemented in C#?

The word polymorphism means “many-shaped”. It is the ability to represent the same programming interface for different underlying forms. However, the concept of polymorphism is same in different programming languages but the implementation differs.

In C# polymorphism can be

  • Static polymorphism
  • Dynamic polymorphism

22. What is dependency injection?

Dependency injection is a mechanism used to decouple tightly linked classes. In this way direct dependency of classes upon each is reduced. To achieve dependency injection, following ways are used:

  1. Constructor dependency
  2. Property dependency
  3. Method dependency

23. What is the difference between public, static and void?

Public: The public declared variables can be accessed in the while application.

Static: Static declared variables are globally accessible without creating an instance of the class.

Void: Void is a type modifier that shows that method does not return any value.

24. What are constructors in C#?

A constructor is a member function in the class having same name as its class. The constructor is called when object is created.

25. What is Jagged Arrays?

Jagged array is also called array of array. It has elements of type array, therefore, called jagged array. The elements can be of different dimensions and sizes.

26. What is the lock statement in C#?

Lock statement prevents one thread to enter in critical section while another thread is already in critical section. If another thread attempts to enter a locked-code, it will have to wait, block until object is released.

27. What is serialization?

Serialization is the process of converting object into a stream of bytes. After conversion, we can transport an object through network.

28. What is the difference between dispose and finalize variables in C#?

  • Dispose: It uses “IDisposable” interface and will free up both managed and unmanaged codes.
  • Finalize: It is called by garbage collector and cannot be called from code. Unlike Dispose method which is called explicitly, it is called internally.

29. What is the difference between “throw ex” and “throw” methods in C#?

  • “throw ex” In this case stack trace of exception will be replaced with a stack trace starting at re-throw point.
  • “throw” It preserves original error stack information.

30. What is the difference between interface and abstract?

Interface: It contains only signatures of methods, properties, indexers or events. A class which implements interface must also implement members of interface that are specified in interface definition.

Abstract: Abstract modifier indicate the thing being modified has incomplete implementation. This modifier can be sued with classes, methods, properties, indexers and events. Its use shows in a class shows that class is intended only to be case class of other classes.

31. What is upcasting?

In upcasting operation, base class reference is created from a subclass reference.

(subclass -> superclass) (i.e. Teacher -> Student)

32. What is downcasting?

It is opposite to upcasting. In downcasting operation subclass reference is created from base class reference

(superclass -> subclass) (i.e. Student -> Teacher)

33. What is % operator?

It is called modulus operator. It computes remainder after division of first operand by the second operand.

34. What is the difference between “break” statement and “continue” statement in C#?

Break Statement: Break statements exit the program from particular loop, condition or switch case on meeting a certain condition.

Continue Statement: When a program finds continue statement, it executes all the statements uptil continue statement again, without execution of statements after continue statement.

35. What are the name of exceptions in C#?

  • ArgumentNullException
  • IndexOutOfRangeException
  • NullReferenceException
  • InvalidOperationException
  • DivideByZeroException

36. What is async modifier in C#?

If async modifier is used in a method or expression, it specifies that the method or expression is asynchronous.

37. What is extern modifier in C#?

The extern modifier is used to declare a method which is implemented externally.

38. What is sealed modifier?

The sealed modifier is used to prevent other classes from inheriting from it.

39. What is unsafe modifier in C#?

The unsafe modifer indicates the unsafe context, required for any operation including pointers.

40. Describe “base” keyword.

The base keyword is used for accessing members of base class from within the derived class.

41. Describe “this” keyword.

this keyword refers to current instance of a class. It is also used to pass object as a parameter  to other method.

42. Can we inherit multiple interfaces?

Yes you can inherit multiple interfaces in C#.

43. What is object pool in C#?

It is used for tracking objects being used in code. It reduces object creation overhead.

44. What is collection?

A collection is said to be a container of instances of other classes. For this, the classes implement ICollection interface.

45. What is reflection?

It is used to get metadata and assemblies of an object at run time.

46. What are number of classes in .Net DLL

One .Net DLL can contain unlimited number of classes.

47. Describe about manifest.

Manifest is the metadata which describes assemblies.

48. How many types of constructors are there in C#?

Following are the five types of constructors

  1. Default contructor
  2. Private constructor
  3. Static constructor
  4. Instance constructor
  5. Parametrized constructor

49. What are generics in C#?

Generics are used to make the code reusable, resultantly decreases code redundancy and performance is increased.

50. What is delegate in C#?

Delegates are type safe pointers used to represent the reference of methods having return type and parameters.

51. What are types of delegates?

Following are types of delegates.

  • Single Delegate
  • Multicast Delegate
  • Generic Delegate

You may also like:
1)  Ajax Interview Questions
2)  Information Technology Interview Questions
3)  Networking Interview Questions