0% found this document useful (0 votes)
7 views7 pages

Top 50 C# Interview Questions & Answers

The document provides a comprehensive overview of C#, a popular programming language primarily used for .NET development, including its features, object-oriented principles, and key concepts such as managed and unmanaged code, boxing and unboxing, and the differences between structs and classes. It also outlines various C# interview questions and answers that cover fundamental topics like enums, control statements, and the use of keywords like ref and out. The latest version of C# mentioned is C# 11.

Uploaded by

Padmanaban R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Top 50 C# Interview Questions & Answers

The document provides a comprehensive overview of C#, a popular programming language primarily used for .NET development, including its features, object-oriented principles, and key concepts such as managed and unmanaged code, boxing and unboxing, and the differences between structs and classes. It also outlines various C# interview questions and answers that cover fundamental topics like enums, control statements, and the use of keywords like ref and out. The latest version of C# mentioned is C# 11.

Uploaded by

Padmanaban R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C# is among the most popular programming languages and the goto language

for .NET development. So, if you are a .NET developer going for a .NET interview,
you will be asked questions on C# programming. Here are the top 50 C# interview
questions and answers for beginners and professional C# developers.

1. What is C#? And What is the latest version of C#?


C# is a computer programming language. Microsoft developed C# in 2000 to provide
a modern general-purpose programming language that can be used to develop all
kinds of software targeting various platforms, including Windows, Web, and Mobile,
using just one programming language. Today, C# is one of the most popular
programming languages in the world. Millions of software developers use C# to build
all kinds of software.

C# is the primary language for building Microsoft .NET software applications.


Developers can build almost every kind of software using C#, including Windows UI
apps, console apps, backend services, cloud APIs, Web services, controls and
libraries, serverless applications, Web applications, native iOS and Android apps, AI
and machine learning software, and blockchain applications.

C# provides rapid application development with the help of Visual Studio IDE. C# is
a modern, object-oriented, simple, versatile, and performance-oriented programming
language. C# is developed based on the best features and use cases of several
programming languages, including C++, Java, Pascal, and SmallTalk.

C# syntaxes are like C++. .NET, and the C# library is similar to Java. C# supports
modern object-oriented programming language features, including Abstraction,
Encapsulation, Polymorphism, and Inheritance. C# is a strongly typed language.
Most of the types in .NET are inherited from the Object class.

C# supports concepts of classes and objects. Classes have members such as fields,
properties, events, and methods. Here is a detailed article on C# and OOP.

C# is versatile and modern and supports modern programming needs. Since its
inception, C# language has gone through various upgrades. The latest version of C#
is C# 11.

2. What is an object in C#?


C# language is an object-oriented programming language. Classes are the
foundation of C#. A class is a template that defines a data structure and how data
will be stored, managed, and transferred. A class has fields, properties, methods,
and other members.

While classes are concepts, objects are real. Objects are created using class
instances. A class defines the type of an object. Objects store real values in
computer memory.
Any real-world entity with certain characteristics or that can perform some work is
called an Object. This object is also called an instance, i.e., a copy of an entity in a
programming language. Objects are instances of classes.

For example, we need to create a program that deals with cars. We need to create
entities for cars. Let’s call it a class, Car. A car has four properties, i.e., model, type,
color, and size.

To represent a car in programming, we can create a class Car with four properties,
Model, Type, Color, and Size. These are called members of a class. A class has
several types of members, constructors, fields, properties, methods, delegates, and
events. A class member can be private, protected, or public. In addition, since these
properties may be accessed outside the class, these can be public.

An object is an instance of a class. A class can have as many instances as needed.


For example, Honda Civic is an instance of a Car. In real programming, Honda Civic
is an object. Therefore, Honda Civic is an instance of the class Car. The Model,
Type, Color, and Size properties of the Honda Civic are Civic, Honda, Red, and 4,
respectively. BMW 330, Toyota Carolla, Ford 350, Honda CR4, Honda Accord, and
Honda Pilot are some more examples of objects of Car.

To learn more about real-world examples of objects and instances, please


read Object Oriented Programming with Real World Scenario.
3. What is Managed or Unmanaged Code?
Managed Code

“Managed code is the code that is developed using the .NET framework and its
supported programming languages such as C# or [Link]. Managed code is directly
executed by the Common Language Runtime (CLR or Runtime), and the Runtime
manages its lifecycle, including object creation, memory allocation, and object
disposal. Any language that is written in .NET Framework is managed code".

Unmanaged Code

The code that is developed outside of the .NET framework is known as unmanaged
code.

“Applications that do not run under the control of the CLR are said to be unmanaged.
For example, languages such as C or C++, or Visual Basic are unmanaged.

The programmers directly manage the object creation, execution, and disposal of
unmanaged code. Therefore, if programmers write bad code, it may lead to memory
leaks and unwanted resource allocations.”

The .NET Framework provides a mechanism for unmanaged code to be used in


managed code and vice versa. The process is done with the help of wrapper
classes.

4. What is Boxing and Unboxing in C#?


Boxing and Unboxing are both used for type conversions.

Converting from a value type to a reference type is called boxing. Boxing is an


implicit conversion. Here is an example of boxing in C#.
// Boxing
int anum = 123;
Object obj = anum;
[Link](anum);
[Link](obj);

Converting from a reference type to a value type is called unboxing. Here is an


example of unboxing in C#.
// Unboxing
Object obj2 = 123;
int anum2 = (int)obj;
[Link](anum2);
[Link](obj);
5. What is the difference between a struct and a
class in C#?
Class and struct are both user-defined data types but have some major differences:

Struct
 The struct is a value type in C# and inherits from [Link] Type.
 Struct is usually used for smaller amounts of data.
 Struct can’t be inherited from other types.
 A structure can't be abstract.
 No need to create an object with a new keyword.
 Do not have permission to create any default constructor.
Class
 The class is a reference type in C#, and it inherits from the [Link] Type.
 Classes are usually used for large amounts of data.
 Classes can be inherited from other classes.
 A class can be an abstract type.
 We can create a default constructor.
Read the following articles to learn more about structs vs. classes, Struct, and Class
Differences in C#.
6. What is the difference between Interface and
Abstract Class in C#?
Here are some common differences between an interface and an abstract class in
C#.

 A class can implement any number of interfaces, but a subclass can, at most,
use only one abstract class.
 An abstract class can have non-abstract methods (concrete methods), while in
the case of interface, all the methods have to be abstract.
 An abstract class can declare or use any variables, while an interface cannot do
so.
 In an abstract class, all data members or functions are private by default, while
in an interface, all are public; we can’t change them manually.
 In an abstract class, we need to use abstract keywords to declare abstract
methods; in an interface, we don’t need that.
 An abstract class can’t be used for multiple inheritance, while the interface can
be used for multiple inheritance.
 An abstract class uses a constructor, while we don’t have any constructor in an
interface.
To learn more about the difference between an abstract class and an interface,
visit Abstract Class vs. Interface.
7. What is an enum in C#?
An enum is a value type with a set of related named constants, often called an
enumerator list. The enum keyword is used to declare an enumeration. It is a
primitive data type that is user-defined.

An enum type can be an integer (float, int, byte, double, etc.). But if you use it beside
int, it has to be cast.

An enum is used to create numeric constants in the .NET framework. All the
members of the enum are enum type. Therefore, there must be a numeric value for
each enum type.

The underlying default 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.
enum Dow {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

Some points about enum,

 Enums are enumerated data types in c#.


 Enums are not for the end-user. They are meant for developers.
 Enums are strongly typed constant. They are strongly typed, i.e., an enum of
one type may not be implicitly assigned to an enum of another type even though
the underlying value of their members is the same.
 Enumerations (enums) make your code much more readable and
understandable.
 Enum values are fixed. Enum can be displayed as a string and processed as an
integer.
 The default type is int, and the approved types are byte, sbyte, short, ushort,
uint, long, and ulong.
 Every enum type automatically derives from [Link], and thus, we can
use [Link] methods on enums.
 Enums are value types created on the stack, not the heap.
For more details, follow the link, Enums in C#.
8. What is the difference between “continue” and
“break” statements in C#?
Using a break statement, you can 'jump out of a loop,' whereas using a continue
statement, you can 'jump over one iteration' and resume your loop execution.

E.g., Break Statement


using System;
using [Link];
using [Link];
using [Link];
namespace break_example {
Class brk_stmt {
public static void main(String[] args) {
for (int i = 0; i <= 5; i++) {
if (i == 4) {
break;
}
[Link]("The number is " + i);
[Link]();
}
}
}
}
Output
The number is 0;
The number is 1;
The number is 2;
The number is 3;
E.g., Continue Statement
using System;
using [Link];
using [Link];
using [Link];
namespace continue_example {
Class cntnu_stmt {
public static void main(String[] {
for (int i = 0; i <= 5; i++) {
if (i == 4) {
continue;
}
[Link]("The number is "+ i);
[Link]();
}
}
}
}
Output
The number is 1;
The number is 2;
The number is 3;
The number is 5;
For more details, check out the following link: Break and Continue Statements in C#
9. What is the difference between constant and
readonly in C#?
Const is nothing but "constant," a variable whose value is constant but at compile
time. Therefore, it's mandatory to assign a value to it. By default, a const is static,
and we cannot change the value of a const variable throughout the entire program.

Readonly is the keyword whose value we can change during runtime or assign it at
run time but only through the non-static constructor.

Example

We have a Test Class in which we have two variables, one is readonly, and the other
is a constant.
class Test {
readonly int read = 10;
const int cons = 10;
public Test() {
read = 100;
cons = 100;
}
public void Check() {
[Link]("Read only : {0}", read);
[Link]("const : {0}", cons);
}
}

Here, I was trying to change the value of both the variables in the constructor, but
when I try to change the constant, it gives an error to change their value in the block
that I have to call at run time.

Finally, remove that line of code from the class and call this Check() function like in
the following code snippet:
class Program {
static void Main(string[] args) {
Test obj = new Test();
[Link]();
[Link]();
}
}
class Test {
readonly int read = 10;
const int cons = 10;
public Test() {
read = 100;
}
public void Check() {
[Link]("Read only : {0}", read);
[Link]("const : {0}", cons);
}
}
Output
Learn more about const and readonly here: Difference Between Const, ReadOnly,
and Static ReadOnly in C#.
10. What is the difference between ref and out
keywords?
The ref keyword passes arguments by reference. Therefore, any changes made to
this argument in the method will be reflected in that variable when the control returns
to the calling method.

The out keyword passes arguments by reference. This is very similar to the ref
keyword.

To learn more about the ref and out keywords, read the following article: Ref Vs. Out
Keywords in C#

You might also like