Introduction To C# Interview Questions and Answers
C# is an object-oriented language compiled by .Net Framework and works as an Intermediate language. It is type-safe and a managed language. It is used by many operating systems and hence one must have a strong understanding of this language. It is highly in demand and due to its versatility, it can support many operating systems.
So you have finally found your dream job in C# but are wondering how to crack the 2023 C# Interview and what could be the probable C# Interview Questions. Every interview is different and the scope of a job is different too. Keeping this in mind we have designed the most common C# Interview Questions and Answers to help you get success in your interview.
Below are a few 2023 C# Interview Questions that are asked in interviews and will help you understand C# in detail. This question is divided into two parts are as follows:
Part 1 – C# Interview Questions (Basic)
This first part covers basic C# Interview Questions and answers
Q1. What is Managed and Unmanaged code?
Answer:
Managed code is executed on .Net platform. It makes use of CLR (Common Language Runtime) for all application code based on that platform. It is said to be managed because of.Net framework which uses an internal garbage collector to clear unused memory.
Unmanaged code, on the other hand, stands for code that is executed by application runtime on any other framework other than.Net frameworks. The application when run will take care of memory, security, and other factors related to performance. This is the basic C# Interview Questions which is asked in an interview.
Q2. What are the different types of classes in C#? Explain each class in short?
Answer:
There are four types of classes in C#. They are as follows:
1) Static class: This class does not allow inheritance. The members present in this class are static and they are denoted by the keyword static. When this keyword is used then the compiler checks for accidental instances of static class if any.
2) Abstract class: This class is denoted by the keyword abstract. The objects of these classes cannot be instantiated. This class can only be inherited and must contain at least one method.
3) Sealed class: This class cannot be inherited. To access an object of this class should be created. It is created using the keyword Sealed.
4) Partial class: A class can be denoted as this one by using the keyword partial. It helps its members to be divided or shared with multiple .cs files.
Let’s move on to the next C# Interview Questions
Q3. What are C# I/O classes? Which are the classes that are commonly used?
Answer:
C# uses the System.IO namespace which consists of classes that perform various operations like create, delete, open, close, etc. The commonly used I/O classes are:
- File: Helps in performing various operations on a file. It helps in the creation and manipulation of files.
- StreamWriter: It is used for writing characters to a stream.
- StreamReader: It is used for reading characters from a stream.
- StringWriter: It is used for writing a string to buffer.
- StringReader: It is used for reading a string from a buffer.
- Path: This can be used when a user wants to perform operations related to the path.
Q4. Explain StreamReader/ StreamWriter class?
Answer:
This is the common C# Interview Questions asked in an interview. Both these classes belong to namespace System.IO. These classes are used when a user wants to read or write characters, reader-based data, respectively.
StreamReader class includes members like: close(), read(), Readline().
StreamWrier class includes members like close(), write(), writeline().
class Program1
{
using(StreamReader sr = new StreamReader("C:\ReadMe.txt")
{
//----------------code to read-------------------//
}
using(StreamWriter sw = new StreamWriter("C:\ReadMe.txt"))
{
//-------------code to write-------------------//
}
}
Q5. Explain the concept of Boxing and Unboxing?
Answer:
When a value is converted to a reference type then it is known as boxing.
Example:
int value -= 20;
//-----------Boxing------------//
Object boxValue= value;
Here boxValue references ‘value’.
To convert back to value type from reference type is known as unboxing. This conversion is done explicitly.
Example:
//————UnBoxing——————//
int UnBoxing = int (boxedValue);
UnBoxing references back to original value.
Part 2 – C# Interview Questions (Advanced)
Let us now have a look at the advanced C# Interview Questions.
Q6. What are Regular Expressions? Write a regex to find a string using?
Answer:
Regular expressions patterns to templates to match a given set of input. These patterns may contain operators, character literals, symbols, etc. Regex is used when the user wants to parse the string or replace a character or characters from the string. It can be used to find any kind of pattern in a given file or input.
Example:
static void Main(string[] args)
{
string[] lang = { "C#", "Python", "Java" };
foreach(string s in lang)
{
if(System.Text.RegularExpressions.Regex.IsMatch(s,"Python"))
{
Console.WriteLine("Match found");
}
}
}
In this example, the language that is being searched in Python is in the given array. The regex .IsMatch is used here to find the correct match. It returns true whenever a given pattern is found. Any regular expression can be used to find particular matches in our input.
Q7. What are the different types of Delegates?
Answer:
The different types of delegates are:
- Single Delegate: When a delegate calls a single method then it is a single delegate.
- Multicast Delegate: When a delegate calls multiple methods then it is a multicast delegate. A user can use + and – operators to subscribe and unsubscribe.
- Generic Delegate: Generic delegates are of three types. They are Action, Funcs, and Predicates.
- Action: Replacement of definition of delegate and event is done using Action keyword. It defines a method that can be called on arguments and does not return a result.
- Func: This delegate helps in defining a method that can call arguments and returns a result.
- Predicate: It defines a method that can call arguments and will always return bool values.
Let us move on to the next C# Interview Questions.
Q8. Explain Publisher and Subscribers in Events?
Answer:
This class publishes a message from different types of other classes. This message will be nothing but an event. It is generated when an event runs successfully. If an event runs then other classes dependent on it receive this message.
Subscribers take the message in which they are interested and want updates. When any event runs successfully and the subscriber needs information regarding it then will get the messages for that particular event.
Q9. What are Get and Set Accessor properties?
Answer:
These is the frequently asked C# Interview Questions in an interview. These accessors are used by properties. These properties enable a user to read, write values to private fields. To ensure these fields are private accessors are used. Get property is used to return the value of the property. A set accessor is used to set the value for the property.
Q10. Explain some properties of Thread class?
Answer:
Properties of Thread class are:
1)IsAlive: Contains True when a thread is alive
2)Name: Returns name of the thread
3)Priority: Returns prioritized value of task set
4)ThreadState: It describes the state of the thread.
Recommended Articles
This has been a guide to the List Of C# Interview Questions and Answers. Here we have listed the top 10 Interview Questions and Answers that are commonly asked in interviews with detailed responses. You may also look at the following articles to learn more –