Professional Documents
Culture Documents
🏠 Python ▾ Selenium QA Agile Angular SQL MySQL Java Linux C C# Web PHP Android
Today, we’ve outlined 15 C# interview questions that every beginner should read once. We’ve mostly
covered the fundamentals of C# programming and touched upon a few advanced concepts.
As per the latest TIOBE index, C# stands at the seventh position among the best programming
language to learn first. So you probably took the right decision to start learning the language that can
fetch you one of the highest paying jobs in the IT industry.
Looking at the popularity and the volume of job openings, we’ve picked some of the essential C#
interview questions that could increase your chances of getting hired. You must remember that a
successful candidate is the one who can think a step ahead than the interviewer.
Before moving on to the questions and answers section, let’s read a few interesting facts about C#
programming.
C# Fun Facts
It puts away all global variables and functions. You must declare them strictly inside a class.
Local variables can’t push back the variables of the enclosing block.
C# presents a native boolean type i.e. bool. Unlike C/C++, you can’t translate it to/from an integer.
You can use pointers only from the blocks marked as unsafe. You won’t be able to dereference any
pointer defined outside the unsafe block.
C# brought in GC (Garbage Collector) to manage memory and avoid memory leaks.
It fixed the well-known diamond problem by casting out the ability that could lead to multiple
inheritance issues.
C# allows using Enumeration within the boundary of their namespaces.
It fully supports type reflection and discovery.
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 1/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
C# Interview Questions
Q-1. What Are The Essential Features Of C# Language That Made It The Fourth Most
Used Language?
Ans. Following is the list of features behind the popularity of the C# language:
1. Boolean Conditions
2. Automatic Garbage Collection
3. Standard Library
4. Assembly Versioning
5. Properties and Events
6. Delegates and Events Management
7. Easy-to-use Generics
8. Indexers
9. Conditional Compilation
10. Simple Multithreading
11. LINQ and Lambda Expressions
12. Integration with Windows
Ans. Access modifiers define the scope in which an object and its members are accessible. All types and
type members in C# support them.
A. Public :
It is the most common access specifier in C#. There is no restriction on accessing public members. The
scope of its accessibility is inside as well as outside the class.
B. Private :
A private member is accessible from within the body of the class or the struct which defines it. Thus it
cannot be accessed from outside the class. It is the default access modifier type if there is no explicit
declaration.
C. Protected :
The scope of accessibility limits to within the class or struct and the class derived ( or Inherited) from this
class.
D. Internal :
The internal access modifiers can access within the program that contains its declarations and also
access within the same assembly level but not from another assembly.
E. Protected Internal :
It is the same as protected and internal access levels. Its accessibility scope is anywhere in the same
assembly, in the same class and also the classes inherited from that class.
Ans. As usual, a constructor is a special method of a class that gets invoked automatically on
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 2/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
, p g y
instantiating that class.
The primary purpose of the constructors is to initialize the private members while creating the objects of
the class. If a class doesn’t have an explicit constructor, then the compiler automatically creates the
default one. It initializes all numeric fields in the class to zero and all string and object fields to null.
Advertise on LinkedIn
Reach a community of
55M+ professionals in India
Since there is no way a constructor can return values, so it doesn’t have a return type.
A constructor can call another constructor by using “this” keyword. The “this” keyword represents the
current instance of a class.
Syntax:
You can define a constructor with the same class name and without mentioning any return type as:
Ans. We can classify the constructors into the following five types:
1. Default Constructor:
A constructor without any parameters is the default Constructor. A major drawback of default Constructor
is that all instances of the class get initialized with the same values. It is not possible to initialize them
with different values.
2. Parameterized Constructor:
A Constructor with at least one parameter is Parameterized Constructor. Its advantage is that it enables
to initialize each instance of the class to different values.
3. Copy Constructor:
A Copy Constructor is a parameterized constructor that contains, parameters of same class type. Its
objective is to initialize a new instance to the values of an existing one.
4. Static Constructor:
We create a Static Constructor by declaring a Constructor as static. It gets invoked only once for any
number of instances of the class.
5. Private Constructor:
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 3/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
It is not possible to instantiate a class that contains a Private Constructor. Thus we can create a Private
Constructor in a class which has only static members.
Ans. A jagged array is a sequential collection of elements which themselves are arrays. It may constitute
elements of different dimensions and sizes.
Programmers often call it as an “array of arrays” where the length of each array index can differ.
Example:
In the above declaration, the rows are fixed in size. However, columns can vary as they are not
specified.
Ans. Class and struct both are the user-defined data types. However, there are some differences between
them, which are as follows:
Struct:
Class:
Ans.
The abstract keyword allows creating classes and class members that are incomplete. Implementation of
such classes transpires in the derived class.
However, the sealed keyword stops a class (marked as virtual) or a particular class member from being
inherited.
Q-8. What Are The Possible Ways Of Passing Parameters To A Function In C#?
1. Value Parameters:
It makes a copy of the original value of the arguments, into the functional parameters. In this case, any
change made to the parameter inside the function has no effect on the actual argument.
2. Reference Parameters:
This method copies the address of the memory location of the argument into the formal parameter. Thus,
any change in the value of the parameter affects the actual argument.
3. Output Parameters:
Ans. Following are the key differences between ref and out keywords:
Ref:
1. The parameter or the argument must be initialized first before passing it to Ref.
2. It is not mandatory for the called method to initialize or assign values to parameters (passed by
reference) before control returns to the calling function.
3. Passing a parameter value by Ref is useful when its value has to be modified by the called function
also.
4. It is not compulsory to assign a value to a parameter before using it in the calling method.
5. Ref supports the bi-directional passing of data.
Out:
Ans. It is not possible to use “this” in the static method. Because ‘this’ keyword returns a reference to
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 5/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
Static methods (or any static member) do not belong to a particular instance. They exist even if you do
not create an instance of the class and we call it using the name of the class.
So we can’t use “this” keyword in the body of Static Methods. However, there is a situation known as the
Extension Method which does allow to use “this” keyword in static methods.
It is relatively a new concept introduced in C# 3.0. It enables a class to extend without modifying its
code.
Ans. The namespace keyword is used to declare a scope that contains a set of related objects. You can
use a namespace to organize code elements and to create globally unique types. In other words, it
provides a way to keep one set of names separate from another.
For example, let’s create a class with the name Console and place it in its own namespace. The
explanation for doing this is that it ensures, there is no confusion about when the System.Console class
gets used and when the user created class gets used.
Though, it is bad coding practice to create a class with the name as Console. However, there are
situations where the name of our class could conflict with the names in the Dot Net library or a third
party library. In such cases, namespace helps you avoid the problems that identical class names would
cause. In such scenarios, namespace helps you avoid the problems that identical class names would
cause.
This implies that the class names declared in one namespace does not conflict with the same class
names declared in another.
C# Example
using System;
namespace one
{
class myClass
{
public void func()
{
Console.WriteLine("Inside namespace one");
}
}
}
namespace two
{
class myClass
{
public void func()
{
Console.WriteLine("Inside namespace two");
}
}
}
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 6/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
}
class TestClass
{
static void Main(string[] args)
{
one.myClass obj = new one.myClass();
two.myClass itm = new two.myClass();
obj.func();
itm.func();
Console.ReadKey();
}
}
Ans. The “using” keyword states that the program is using the names in the given namespace.
C# Example.
// Namespace Declaration
using System;
using techbeamers;
// techbeamers Namespace
namespace techbeamers
{
class myExample
{
public static void myPrint()
{
Console.WriteLine("Example of using directive.");
}
}
}
Explanation.
If we want to call methods without typing their fully-qualified name, we implement the using directive. In
the above example, we have declared two using directives. The first, using System directive allows us to
type the method names of members of the System namespace without typing the word System every
time. In myPrint() method above, Console is a class member of the System namespace with the method
WriteLine(). Its fully qualified name is <System.Console.WriteLine(…)>.
Similarly, the <using techbeamers> directive enables us to call the members of the techbeamers
namespace without typing the fully-qualified name. This is why we can type <myExample.myPrint()>
instead of <techbeamers.myExample.myPrint()> every time we want to call that method.
Ans. Boxing and Unboxing both are used for type conversion but have some differences:
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 7/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
g g yp
1. Boxing:
It is the process of converting a value type to an object or to an interface data type. When boxing of
CLR happens, it means that CLR is converting a value type to an Object Type. It wraps the value inside
a <System.Object> and stores it in the heap area in an application domain.
C# Example:
2. Unboxing:
It is a process that gets utilized when extracting the value type from the object or any implemented
interface type. Boxing is done implicitly, while unboxing has to be explicit by code.
C# Example:
Q-14. Which Class Acts As A Base Class For All The Data Types In C#?
Ans. The Object type is the base class of all data types in C# Common Type System (CTS). It stays at
the root of the hierarchy and works as an alias for the classes.
We can assign Object type to any of the value or reference types, predefined or user-defined
types. However, before assigning values, type conversion is mandatory.
Ans. An enum is a value type with a set of related named constants often referred to as an enumerator
list. You can define it with the enum keyword. It’s a user-defined primitive data type.
Usually, an enum is of an integer type. Though, you can use other types e.g. float, byte, double.
However, you need to cast while using it.
An enum helps to define numeric constants in C#. All of its members are of the enum type. There must
be a numeric value for each enum type.
The default underlying type of the enumeration element is int. By default, the first enumerator has the
value 0, and the value of each successive enumerator is increased by 1.
Given below is the example of the enum declaration:
enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
1. They are strongly-typed constants. Thus an enum of one type cannot be implicitly assigned to another
enum type, even though their members have the same values.
2 The enum values are same as constants You can print them like a string and process like an integer
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 8/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
2. The enum values are same as constants. You can print them like a string and process like an integer.
3. Its default type is int, and the approved types are as follows.
byte, sbyte, short, ushort, uint, long, and ulong .
4. All enum type automatically derives from System.Enum and its methods work on enums.
5. Enums are value types constructed on the stack and not on the heap.
In our next posts on C# programming, we’ll add more questions for experienced and bring a few quizzes
for you to practice your skills.
If you have any feedback or like to share some idea, then speak it out in the comment box.
TechBeamers
RELATED
C# Exception Handling – 35 20 C# Programming Questions
Questions Programmers Should On Object Oriented Concepts
Not Miss
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 9/10
9/15/2019 15 C# Interview Questions Beginners Should not Miss
Python Tutorial Python Interview Questions AngularJS Questions ☛ About Us
☛ Privacy Policy
Selenium Python Tutorial SQL Interview Questions JavaScript Questions
☛ Disclaimer
Selenium Webdriver Tutorial Selenium Interview Questions Web Developer Questions ☛ Contact Us
Python Tips & Tricks Linux Interview Questions PHP Interview Questions-2
© 2019 TechBeamers.com
https://www.techbeamers.com/c-sharp-interview-questions-beginners/ 10/10