• +91 9723535972
  • info@interviewmaterial.com

CSharp Interview Questions and Answers

CSharp Interview Questions and Answers

Question - 81 : - What are I/O classes in C#? Define some of the most commonly used ones.

Answer - 81 : -

The System.IO namespace in C# consists of several classes used for performing various file operations, such as creation, deletion, closing, and opening. Some of the most frequently used I/O classes in C# are:

  • File – Manipulates a file
  • Path – Performs operations related to some path information
  • StreamReader – Reads characters from a stream
  • StreamWriter – Writes characters to a stream
  • StringReader – Reads a string buffer
  • StringWriter – Writes a string buffer

Question - 82 : - What do you understand by regular expressions in C#? Write a program that searches a string using regular expressions.

Answer - 82 : -

Regular expression is a template for matching a set of input. It can consist of constructs, character literals, and operators. Regex is used for string parsing, as well as replacing the character string. Following code searches a string “C#” against the set of inputs from the languages array using Regex:

static void Main(strong[] args)
{
string[] languages = {“C#”, “Python”, “Java”};
foreach(string s in languages)
{
if(System.Text.RegularExpressions.Regex.IsMatch(s,“C#”))
{
Console.WriteLine(“Match found”);
}
}
}

Question - 83 : - Give a detailed explanation of Delegates in C#.

Answer - 83 : -

Delegates are variables that hold references to methods. It is a function pointer or reference type. Both the Delegate and the method to which it refers can have the same signature. All Delegates derives from the

System.Delegate namespace.
Following example demonstrates declaring a delegate:

public delegate AddNumbers(int n);
After declaring a delegate, the object must be created of the delegate using the new keyword, such as:

AddNumbers an1 = new AddNumbers(number);
The Delegate offers a kind of encapsulation to the reference method, which gets internally called with the calling of the delegate. In the following example, we have a delegate myDel that takes an integer value as a parameter: public delegate int myDel(int number); public class Program { public int AddNumbers(int a) { Int Sum = a + 10; return Sum; } public void Start() { myDel DelgateExample = AddNumbers; } }

Question - 84 : - Explain Reflection in C#.

Answer - 84 : -

The ability of code to access the metadata of the assembly during runtime is called Reflection. A program reflects upon itself and uses the metadata to:

Inform the user, or
Modify the behaviour
The system contains all classes and methods that manage the information of all the loaded types and methods. Reflection namespace. Implementation of reflection is in 2 steps:

  • Get the type of the object, then
  • Use the type to identify members, such as properties and methods

Question - 85 : - Name some of the most common places to look for a Deadlock in C#.

Answer - 85 : -

For recognizing deadlocks, one should look for threads that get stuck on one of the following:

  • .Result, .GetAwaiter().GetResult(), WaitAll(), and WaitAny() (When working with Tasks)
  • Dispatcher.Invoke() (When working in WPF)
  • Join() (When working with Threads)
  • lock statements (In all cases)
  • WaitOne() methods (When working with AutoResetEvent/EventWaitHandle/Mutex/Semaphore)

Question - 86 : - Define Serialization and its various types in C#.

Answer - 86 : -

The process of converting some code into its binary format is known as Serialization in C#. Doing so allows the code to be stored easily and written to a disk or some other storage device. We use Serialization when there is a strict need for not losing the original form of the code. A class marked with the attribute [Serializable] gets converted to its binary form. A stream that contains the serialized object and the System.Runtime.Serialization namespace can have classes for serialization. Serialization in C# is of three types:

  • Binary Serialization – Faster and demands less space; it converts any code into its binary form. Serialize and restore public and non-public properties.
  • SOAP – It produces a complete SOAP compliant envelope that is usable by any system capable of understanding SOAP. The classes about this type of serialization reside in System.Runtime.Serialization.
  • XML Serialization – Serializes all the public properties to the XML document. In addition to being easy to read, the XML document manipulated in several formats. The classes in this type of serialization reside in System.sml.Serialization.

Question - 87 : - Give a brief explanation of Thread Pooling in C#.

Answer - 87 : - 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. Classes that manage the thread in the thread pool, and its operations, are contained in the System.Threading.ThreadPool namespace.

Question - 88 : - Is it possible to use this keyword within a static method in C#?

Answer - 88 : - A special type of reference variable, this keyword is implicitly defined with each non-static method and constructor as the first parameter of the type class, which defines it. Static methods don’t belong to a particular instance. Instead, they exist without creating an instance of the class and calls with the name of the class. Because this keyword returns a reference to the current instance of the class containing it, it can’t be used in a static method. Although we can’t use this keyword within a static method, we can use it in the function parameters of Extension Methods.

Question - 89 : - What can you tell us about the XSD file in C#?

Answer - 89 : - XSD denotes XML Schema Definition. The XML file can have any attributes, elements, and tags if there is no XSD file associated with it. The XSD file gives a structure for the XML file, meaning that it determines what, and also the order of, the elements and properties that should be there in the XML file. Note: - During serialization of C# code, the classes are converted to XSD compliant format by the Xsd.exe tool.

Question - 90 : - What do you mean by Constructor Chaining in C#?

Answer - 90 : - Constructor chaining in C# is a way of connecting two or more classes in a relationship as an inheritance. Every child class constructor is mapped to the parent class constructor implicitly by using the base keyword in constructor chaining.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners