You are on page 1of 13

Visit Csspoint101.

com more such posts

C# Interview Questions :

C# interview questions and answers for beginners

Q1: What is a Datatype?

Ans: Datatype refers to the type of data that can be stored in a variable. It also specifies how memory would be
allocated to a variable and the operations that can be performed on that variable.

Q2: What are Nullable types?

Ans: Value types that can accept a normal value or a null value are referred to as Nullable types

Q3: What is (??) operator in C#?

Ans: The ?? operator is called the null coalescing operator. It is used to define a default value for a nullable
value type.

Q4: How to check that a nullable variable is having value?

Ans: The HasValue property is used to check whether a variable having value or not.

Q5: What are the differences between Object and Var?

Object Var
The object was introduced with C# 1.0 Var was introduced with C# 3.0
Useful when you want to store multiple types of values Useful when you don't know the type of
to a single variable. assigned value.
It cannot be passed as a method
It can be passed as a method argument.
argument.
Q6: What is the ref keyword in C#?

Ans: The ref keyword causes an argument to be passed by reference, not by value.

Q7: What is the params keyword in C#?

Ans: The params keyword enables a method parameter to receive n number of arguments. These n number of
arguments are changed by the compiler into elements in a temporary array.

Q8: What do you mean by operators in C#?

Ans: An operator is a symbol that tells the compiler what operations can be performed o an operand.

Q9: What are the different types of operators in C#?

Ans: There are seven operators in C#.

Operator Name Define


Arithmetic Operators These operators are used to perform arithmetic operations.
These operators are used to compare values and always result in either true
Relational Operators
or false (>,<,!=, etc).
Logical/Boolean These operators are used compare values and always result in either true or
Operators false(! , &&).
Bitwise Operators These operators are used to perform a bit by bit operation.
Assignment
These operators are used to assign a new value to the variable.
Operators
Type Information
These operators are used to provide information about a particular type.
Operators
Miscellaneous
?: , => , & , && , *
Operators

Q10: What is ternary operator in C#?

Ans: A ternary operator works on a conditional expression that returns a Boolean value. It is a shorthand form of
if-else.

Q11: What is the static keyword mean in C#?


Ans: The static keyword is used to specify a static number, which means that static members are common to all
the objects and they do not tie to a specific object.

Q12: What do you mean by Typecasting in C#?

Ans: Typecasting is a mechanism to covert one type of value to another value. It is possible only when both the
data types are compatible with each other.

Q13: What are the different types of casting in c#?

Ans: 1. Explicit conversion: Conversion of larger data type to smaller data type. It might result in loss of data so
it is also an unsafe type of casting.

2. Implicit conversion: Conversion of a smaller data type to a larger data type and conversion of derived classes
to base class.

Q14: What is an Out keyword in C#?

Ans: The out is a keyword in C# which is used for passing the arguments to methods as a reference type. It is
generally used when a method returns multiple values.

Q15: Can you use out and ref for overloading as the different signature of method?

Ans: No, we cannot do that. Even if both ref and out are treated differently at runtime they treated the same at
compile time. Hence it cannot be overloaded with the same type of arguments.

Q16: What are the named arguments?

Ans: Any argument can be passed by parameter name instead of the parameter position.

Q17: What you man by value type and reference type?

Ans: Value type: A value type variable stores actual values.


Reference type: A reference type variable stores reference of actual values.

Q18: What is a safe and unsafe code in C#?

Ans: Safe code: The code which runs under the management of CLR is called safe code

Unsafe code: The code which does not run under the management of CLR is called unsafe code.

Q19: What is boxing and unboxing in C#?

Ans: Boxing: Implicit conversion of a value type(int, char) to a reference type is known as boxing. Eg:

int variable = 15 object boxingvar = variable

Unboxing: Explicit conversion of the same reference type(which is created back in boxing process) back to value
type is known as unboxing.

int variable = 15 object boxingvar = variable int unboxed = (int)boxingvar

Q20: What is upcasting and Downcasting?

Ans: Implicit conversion of derived classes to a base class is called Upcasting and explicit conversion of the base
class to a derived class is called Downcasting.

c# interview questions and answers (intermediate level)

Q21: What are the different types of decision-making statements in C#?

Ans: Decision-making statements help you to make a decision based on certain conditions. Different types of
decision-making statements are: if statements, if-else statements, if-else-if statements, and switch statements.
Q22: Which one is better/fast, switch or if-else-if statements and why?

Ans: Switch statements are father than if-else-if statements because in if-else if statements each and every
condition has to check but in case of the switch statement compiler does not need to check earlier cases.

Q23: What is the goto statement?

Ans: The goto statement transfers program control to a labeled statement. The statement must exist in the scope
of the goto statement.

Q24: What is the return statement in C#?

Ans: The return statement terminates the execution of the method in which it appears and returns control to the
calling method.

Q25: What is the jump statement in C#?

Ans: The jump statements transfer the program control from one point in the program to another point of the
program.

Q26: What is the throw statement mean?

Ans: This statement throws an exception which indicates that an error has occurred during the program
execution.

Q27: What do you mean by an array and what are the different types of an array in C#?

Ans: An array is a collection of the same type of elements that are accessible by a numerical index. Different
types of array are- one dimensional, two dimensional, and jagged arrays.

Q28: What do you mean by multi-dimensional array?

Ans: A multi-dimensional array is an array with more than one level or dimension. For example, a 2D array,3D
array, etc. Eg:
int[,] arr2D = new int[6,8];// declaration of 2D array arr2D[0,0] = 1;

Q29: What is a jagged array?

Ans: A jagged array is an array whose elements are array itself of different dimensions and sizes.

Q30: What do you mean by an object in C#?

Ans: An object is a representative of the class and is responsible for memory allocation of its member functions.
An object is a real-world entity having attributes and behaviors.

Q31: What is a constructor?

Ans: A constructor is a special type of function which has the same name as its class. The constructor is invoked
whenever an object of a class is created.

Q32: Why static constructor has no parameter?

Ans: Because it is going to call by CLR and not by an object.

Q33: Why you can have only one static constructor?

Ans: To define multiple constructors for a class, you need to overload the constructors. It means you need to
define parameterized constructors that accept parameters from outside but a static constructor is called by the
CLR and CLR cannot pass parameters to the parameterized constructor.

Q34: What do you mean by Destructor?

Ans: The destructor is a special type of member function which has the same name as its class name preceded
by tilde[~] sign. It is used to release unmanaged resources allocated by the object. It cannot be called explicitly.

class { public csspoint101() { console.WriteLIne("Yo men!") } ~csspoint101() {


console.WriteLIne("Yo women!") } }
Q35: What is the purpose of "using" statement in C#?

Ans: The using statement obtains the specified resources, uses it and then automatically calls the dispose
method to clean up the specified resources when the execution of the statement is completed.

Q36: What is an access modifier?

Ans: An access modifier is used to specify the accessibility of the class member.

Q38: Can destructors have access modifiers?

Ans: No, destructors cannot have access modifiers as they are directly called by compiler.

Q39: What do you mean by an enum and when to use it?

Ans: An enum is a value type that stores a list of named constants which are known as enumerators.

Uses of enum are -

1. To define static constants.


2. To define constant flags.

Q40: What is the difference between class and structure?

Class Structure
The class can have default and parameterized constructor. The structure can have only default.
The class can have a destructor. The structure cannot have destructor.
The class is a reference type. The structure is a value type.

Q41: What do you mean by static members?

Ans: Static class members are declared using the static keyword. These can be only called with the class name.
Q42: What is the base class in .NET framework from which all the classes are derived?

Ans: System.Object

Q43: What is a static class?

Ans: A static class is a special class that is loaded into memory automatically by the CLR at the time of code
execution.

Q44: When to use a static class?

Ans: A static class is useful when you want to provide common utilities like: configuration settings, driver
functions, etc

Q45: What are sealed classes in C#?

Ans: Sealed classes are special types of class that is being restricted to be inherited. It is used to prevent
inheritance

Q46. Can multiple catch blocks be executed?

No, Multiple catch blocks can't be executed. Once the proper catch code executed, the control is transferred to
the final block, and then the code that follows the final block gets executed.

Q47: What are value types and reference types?

Ans: A value type holds a data value within its own memory space. Example

int a = 30;

Reference type stores the address of the object where the value is being stored. It is a pointer to another
memory location.

string b = "Hello";
Q48. What is an object pool in .NET?

Ans: An object pool is a container having objects ready to be used. It tracks the object that is currently in use, the
total number of objects in the pool.

Q49: What do you mean by a partial method?

Ans: A partial method is a special method within a partial class or struct. One part of a struct has the only partial
method declaration means signature and another part of the same partial class may have an implementation.

Q50: What are the different ways a method can be overloaded?

Ans: Methods can be overloaded using different data types for a parameter, different order of parameters, and the
different number of parameters.

C# interview questions and answers (expert level)

Q51: What are serialization and its main purpose in C#?

Ans: Serialization is the process of converting an object into a stream of bytes. That helps in storing that object
into memory or a fill int he form of XML, JSON, etc. Its main purpose is to save the state of an object so that it can
be recreated when it requires.

Q52: What is a reflection in c#?

Ans: Reflection is used to examine objects and their types. The system. Reflection namespace contains classes
that allow you to obtain information and about assemblies, modules, and types.

Q53: When you should use reflection?

Ans: It can be used in the following cases:


1. To create an instance of a type or bind the type to an existing object dynamically.
2. To get the type from an existing object and invoke its methods or access its fields and properties.

Q54: What do you mean by exceptions in C#?

Ans: Exceptions are unexpected or unseen errors that occur during the execution of a program. It can be caused
due to the improper use of user inputs, system errors, etc

Q55: What is the role of the System.Exception class?

Ans: System.Exception class is provided by the .NET framework to handle any type of exception that occurs. The
exception class is the base class for all other exception classes.

Q56: What is an exception Handling?

Ans: Exception handling is a method to capture run-time errors and handle them correctly. It is done by using Try-
catch blocks and throw keyword.

Q57: What are the different types of serialization?

Ans: 1. XML serialization

2. Binary serialization

3. SOAP serialization

Q58: What are Delegates?

Ans: A delegate is a reference type that holds the reference to a method class method. Any method which has
the same signature as a delegate can be assigned to delegate.

Q59: What is Polymorphism in C#?


Ans: The ability of a programming language to process objects in different ways depending on their data type or
class is known as Polymorphism. There are two types of polymorphism

Compile-time polymorphism. The best example is Overloading


Runtime polymorphism. The best example is Overriding

Q60: What is Method Hiding in C#?

Ans: If the derived class doesn't want to use methods in the base class, the derived class can implement its own
version of the same method with the same signature. For example, in the classes given below, DriveType() is
implemented in the derived class with the same signature. This is called Method Hiding.

Q61: What are the uses of delegates in C#?

Ans: Below is the list of uses of delegates in C# -

Callback Mechanism
Asynchronous Processing
Abstract and Encapsulate method
Multicasting

Q62: What is the property in C#?

Ans: A property acts as a wrapper around a field. It is used to assign and read the value from that field by using
set and get accessors. The property can be created for a public, private, protected and internal field.

Q63: When you should use the property?

Ans: Its uses are:

1. When to validate data before assigning it to a field.


2. When you need to log all access for a child.

Q64: What is the difference between “as” and “is” operators in C#?

“as” operator is used for casting the object to type or class.


“is” operator is used for checking the object with type and this will return a Boolean value.

Q65: What is an indexer in C#?

Ans: An indexer enables a class or struct instances to be indexed in the same way as an array. It is defined using
this keyword.

Q66: How encapsulation is implemented in C#?

Ans: Encapsulation is implemented by using access specifiers. An access specifier defines the scope and
visibility of a class member.

Q67: What are collections in C#?

Ans: A collection is a set of related objects. It is a class, so you must declare a new collection before you can add
elements to that collection.

Q68: What do you mean by Generics in C#?

Ans: Generic is a class that allows the user to define classes and methods with the placeholder. Generics were
added to version 2.0 of the C# language. The basic idea behind using Generic is to allow type (Integer, String, etc
and user-defined types) to be a parameter to methods, classes, and interfaces.

Q69: Explain Anonymous type in C#?

Ans: Anonymous types allow us to create a new type without defining them. This is a way of defining read-only
properties into a single object without having to define type explicitly.

Q70: Give a brief explanation on Thread Pooling in C#.

Ans: A collection of threads, termed as a Thread Pool in C#. Such threads are for performing tasks without
disturbing the execution of the primary thread. After a thread belonging to a thread pool completes execution, it
returns to the thread pool.

Q71: What is Multithreading in C#?


Ans: Multithreading allows you to perform more than one operation concurrently.The .NET framework
System.Threading namespace is used to perform threading in C#

Q72: Explain different states of a Thread in C#?

Ans: A thread in C# can have any of the following states:

Aborted – The thread is dead but not stopped


Running – The thread is executing
Stopped – The thread has stopped the execution
Suspended – The thread has been suspended

Visit : csspoint101.com

You might also like