Top 20 C# Interview Questions and Answers
Top 20 C# Interview Questions and Answers
Top 20 C# Interview Questions and Answers
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;
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?
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()
{
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.