Вы находитесь на странице: 1из 47

Top 20 C# Interview Questions and Answers

C# is the most popular and widely used


programming language for developing
web applications, desktop applications,
and mobile apps. This article contains
the top 20 C# interview questions and
Introduction answers, in order to prepare you for the
interview.
C# pronounced as "See Sharp". It is
an object-oriented programing
language developed by Microsoft, which
runs under the .NET platform. Its
Q- 1 What is syntaxes are similar to C++ or Java. The
most recent version of C# is 7.3 which is
C#? 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.
Q-2 Explain the evolution history of C#?
Q- 3 What
features are
The following features are added to
added to different versions of C#:

different versions C# 1.0


• Managed Code
of C#? • IDE - Visual Studio .NET 2002, 2003
• .NET Framework - 1.0, 1.1
C# 2.0
• Generics
• Static Classes
• Partial types
• Anonymous methods
• Iterators
Continuing Q-3 • Nullable types
• Asymmetric Property and Indexer
Accessors
• Delegate Inference
• Covariance and Contra-variance
C# 3.0
• Implicit types (var)
• Partial Methods
• Object and collection initializers
• Auto-Implemented properties
• Anonymous types
• Extension methods
Continuing Q-3 • Query expressions
• Lambda expressions
• Expression trees
• IDE - Visual Studio 2008
• .NET Framework - 3.5
C# 4.0
• Dynamic binding
Continuing Q-3 • Named arguments
• Generic Covariance and Contra-variance
• COM Interop
• IDE -Visual Studio 2010
• .NET Framework - 4.0
C# 5.0
Continuing Q-3 • 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
Continuing Q-3 • 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
Continuing Q-3 • Pattern matching
• ref returns and ref Locals
• Throw Expressions
• Expression Bodied Members
• Deconstruction
• IDE - Visual Studio 2017
In C# 5.0 two keywords async and await are
introduced which allow you to write
asynchronous code more easily and
intuitively like as synchronous code. Before
Q- 4 What is C# 5.0, for writing asynchronous code, you
need to define callbacks to capture what
Asynchronous happens after an asynchronous process
finish. These keywords are used in
Method? a combination of each other and an await
operator is applied to a one or more than
one expression of an async method. An async
method returns a Task or Task<TResult> that
represents the ongoing work of the method.
Continuing Q- 4
Q- 5 Explain the C# code
execution process?

C# code execution life cycle


diagram is shown as follows :
Continuing :- Q 5
Q- 6 What are
the advantages The advantage of C# over java is given below :
• C# offers cross-language
of C# over Java? interoperability or mixed language
programming (Java lacking).
• C#directly supports windows
operating system (Java lacking).
• C# is component-oriented language
integrated support for the writing of
software support.
• C# support pointer as unsafe (Java
lacking it).
Data Type refers to the type of
data that can be stored in a
variable. It also specifies how
Q-7 What is much memory would be
Datatype? 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
Q-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.
int x = 10;
char ch = 'a';
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.
Value types that can accept a normal value or a null value are referred to as
Q- 9 What are Nullable types. These are instances of the Nullable struct. For example, A
Nullable<bool> pronounced as "Nullable of bool" and can have the
values true, false or null. Nullable types can also be defined using ? type

Nullable types? modifier. This token is placed immediately after the value type being
defined as nullable.
//assigning normal value
Nullable<bool> flag = true;
//Or You can also define as
bool? flag = true;

//assigning normal value


Nullable<int> x = 20;
//Or You can also define as
int? x = 20;

Note:-
You should not declare a string as nullable since it is implicitly nullable.
Hence, declaring a string type like string? firstName causes a
compile-time error.
Q-10 What is The ?? Operator is called the null-coalescing operator. It
is used to define a default value for nullable value types
or reference types. It returns the left-hand operand if
?? Operator in the operand is not null; otherwise, it returns the right
operand.

C#?
int? x = null;
int y = -1;

// when x is null, z = y
// when x is not null, z = x
int z = x ?? y; // where x and y are left and right operand
The ?? Operator is called the null-coalescing
operator. It is used to define a default value for
nullable value types or reference types. It
Q- 10 What is returns the left-hand operand if the operand is
not null; otherwise, it returns the right
?? Operator in operand.
int? x = null;
C#? int y = -1;

// when x is null, z = y
// when x is not null, z = x
int z = x ?? y; // where x and y
are left and right operand
Q-11 What is the
dynamic type in
C#? The dynamic type was introduced in C# 4.0.
Dynamic type variables are declared using
the "dynamic" keyword. A dynamic type
variable bypasses the compile-time type
checking and its operations are resolved at
run time. In dynamic type, if the operations
are not valid then the exception would be
thrown at run time, not at compile time.
Continuing
Q- 11
Continuing Q- 11
Ref - The ref keyword makes an argument (data) to be passed by reference to

Q- 12 What are the corresponding parameter. Hence, when the value of the parameter is
changed in the method, it gets reflected in the calling method.
Key Points
the differences • The Passing argument must be initialized before passing to the method using
the ref keyword.
between ref • A Property cannot be passed as ref parameter since internally property is a
function.
and out Out – The out keyword also makes an argument (data) to be passed by
reference to the corresponding parameter, but that argument can be passed
parameters? 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.
CONTINUING
Q-12
A Class is a user-defined data
structure that contains data members
(fields, properties, events, and A user-defined data type.
indexer), member function and
nested class type. Basically, a class is :

Q-13 What do
you mean by
Class? A reference type. A tag or template for an object.

class Book
{
//Attributes
int BookId;
string Title;
string Author;
string Subject;
double Price;
Continuing Q- 13
Continuing Q-13
Continuing Q- 13
An object is a representative of the class
and is responsible for memory allocation
of its data members and member
Q- 14 What do functions. An object is a real-world entity
you mean by having attributes (data members) and
behaviors (member functions).
Object?

For example, a student object can have


attributes name, address, and contact
number. It performs activities attending
class, giving exam, etc.
A constructor is a special type of function/method
which has the same name as its class. The
constructor is invoked whenever an object of a
Q-15 What is class is created. It is called constructor since it
constructs the values of the class data members.
Constructor? 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.
There are three types of the constructor –
default constructor, parameterized
Q-16 What are constructor, and copy constructor. But C#
does not support copy constructor.
the different • Default Constructor
types of A default constructor has no parameter.
When a class has no constructor, a default
constructors? constructor is served by the compiler to
that class.
• Parameterized Constructor
The parameterized constructor has one or
more arguments and used to assign values
to instance variables of the class.
Continuing
Q-17
Continuing Q-
17
Continuing
Q- 17
Copy Constructor

Continuing Q-
18 This constructor is used to copy
the entire values of an object to
another object. But C# does not
support copy constructor.
The destructor is a special type member
Q-17 What is function/method which has the same
Destructor? name as its class name preceded by a
tilde (~) sign. The destructor is used to
release unmanaged resources allocated
by the object. It is called automatically
before an object is destroyed. It cannot
be called explicitly. A class can have only
one destructor.
class Example
{
public Example()
{
Console.WriteLine("Constructor called");
}

//destructor
~Example()
{

Continuing Q- 17 Console.WriteLine("Destructor called to destroy the instance");


}
}

class Program
{
static void Main()
{
Example T = new Example();
GC.Collect();
}
Finalize method is used to free unmanaged
resources like files, database connections,
Q- 18 What is COM, etc. held by an object before that object
is destroyed. Internally, it is called by Garbage
finalize () Collector and cannot be called by user code.
Finalize method belongs to Object class in C#.
method?
You should implement it when you have
unmanaged resources in your code and want
to make sure that these resources are freed
when the Garbage collection happens.
• // Implementing Finalize method
• class Example
• {
• public Example()
• {
• Console.WriteLine("Constructor called");
• }

Continuing Q- • //At runtime C# destructor is automatically Converted
to Finalize method.

18 •

~Example()
{
• //TO DO: clean up unmanaged objects
• }
• }


Generics were introduced in C# 2.0. Generics
Q-19 What allow you to define type-safe classes,
are Generics interfaces, methods, events, delegates, and
generic collections without compromising
in C#? type safety, performance, or productivity. In
generics, a generic type parameter is supplied
between the open (<) and close (>) brackets
and which makes it strongly typed collections
i.e. generics collections contain only similar
types of objects.
Q- 19 What is
Multithreading
in C#? Multithreading allows you to perform concurrent
processing so that you can do more than one
operation at a time. The .NET Framework
System. Threading namespace is used to perform
threading in C#.
Continuing
Q- 20
For example, you can use one thread to
Continuing monitor input from the user, second thread to
perform background tasks, third thread to
Q- 20 process user input and fourth thread to show
the output of the third thread processing.
I hope these questions and answers will
help you to crack your C# interview. These
interview Questions have been taken from
our new released eBook C# Interview
Questions & Answers. This book contains
more than 140+ C# interview questions.
Summary
This eBook has been written to make you
confident in C# with a solid foundation.
Also, this will help you to turn your
programming into your profession. It's
would be equally helpful in your real
projects or to crack your C# Interview.

Вам также может понравиться