BCA / B.Tech 49 min read

.NET with C# All Important Questions and Answers in English (MDSU)

1. What is the .NET Framework?

Answer: The .NET Framework is a software development platform developed by Microsoft. It is used to create Windows applications, web applications, and mobile applications. It includes the CLR (Common Language Runtime) and class libraries.


2. What is the CLR (Common Language Runtime)?

Answer: The CLR is an important part of the .NET Framework that compiles and executes code. It provides Garbage Collection, Exception Handling, and Security.


3. What are the main features of the .NET Framework?

Answer:

  1. Platform Independent - Can run on Windows, Linux, and Mac.

  2. Object-Oriented Programming Support - Can use OOP languages like C#, VB.NET.

  3. Garbage Collection - Manages memory.

  4. Security - Provides strong security for code execution.

  5. Multi-language support - C#, VB.NET, F#, etc. can be used.


4. What is MSIL (Microsoft Intermediate Language)?

Answer: MSIL is the code into which code like C# or VB.NET is first compiled. The JIT (Just-In-Time) Compiler converts it into machine code at run-time.


5. What are Assemblies in .NET?

Answer: An Assembly in .NET is a compiled code that has an EXE or DLL format. It stores code and metadata and helps in running the program.


6. What is Visual Studio and what are its main components?

Answer: Visual Studio is an Integrated Development Environment (IDE) from Microsoft used for coding, debugging, and testing in languages like C#, VB.NET. Its main components are:

  1. Menu Bar

  2. Toolbar

  3. Solution Explorer

  4. Toolbox

  5. Properties Window

  6. Form Designer

  7. Output Window


7. How many types of data types are there in .NET?

Answer:

  1. Value Type - int, float, char, bool, etc.

  2. Reference Type - string, array, class, object, etc.

  3. Pointer Type - Used in unsafe mode in C#.


8. What is the difference between a Variable and a Constant in C#?

DifferenceVariableConstant
DefinitionA variable is a storage location that holds data and can change at runtime.A constant is a permanent value that cannot be changed after being assigned.
Changing ValueThe value of a variable can be changed during the program.The value of a constant cannot be changed; it always remains stable.
DeclarationVariables are declared like int x = 10;.Constants are declared like const int x = 10;.
Exampleint age = 25; age = 30; (value can be changed)const double PI = 3.14; (value cannot be changed)


9. What are the Control Flow Statements in C#?

Answer:

  1. Conditional Statements: if, if-else, switch

  2. Loop Statements: for, while, do-while, foreach

  3. Jump Statements: break, continue, return


10. How many types of Arrays are there in C#?

There are three types of Arrays in C#:

  1. Single Dimensional Array

    • It is like a simple list, where data is stored in a single row.

    int[] numbers = { 1, 2, 3, 4, 5 };
    
  2. Multi-Dimensional Array

    • It stores data in the form of a 2D or 3D grid.

    int[,] matrix = { { 1, 2 }, { 3, 4 } };
    
  3. Jagged Array

    • It can have arrays of different sizes at each index.

    int[][] jaggedArray = new int[2][];
    jaggedArray[0] = new int[] { 1, 2, 3 };
    jaggedArray[1] = new int[] { 4, 5 };

11. What are Collections in C#?

Answer: A Collection is a Data Structure that provides the facility of Dynamic Memory Allocation. Such as:

  • List

  • Dictionary

  • Queue

  • Stack

List numbers = new List {1, 2, 3, 4};

12. What is the difference between a Function and a Subroutine in C#?

Answer: A Function returns a Value, while a Subroutine (void method) does not return anything.

int Add(int a, int b) { return a + b; } // Function
void ShowMessage() { Console.WriteLine("Hello"); } // Subroutine

13. What is a Constructor?

Answer: A Constructor is a special method that is automatically called when an object is created.

class Person {
    public string name;
    public Person(string name) {
        this.name = name;
    }
}

14. How many types of Inheritance are there in C#?

Answer: There are 5 types of Inheritance in C#:

  1. Single Inheritance

    • One class inherits properties from only one other class.

    class Parent { }
    class Child : Parent { }
    
  2. Multiple Inheritance (not possible in C#)

    • In C#, a class cannot directly inherit from two classes at the same time, but it can be done through Interfaces.

    interface A { }
    interface B { }
    class C : A, B { }
    
  3. Multi-Level Inheritance

    • One class inherits from another class, and then that same class is inherited by the next class.

    class A { }
    class B : A { }
    class C : B { }
    
  4. Hierarchical Inheritance

    • Multiple Child Classes inherit from a single Parent Class.

    class Parent { }
    class Child1 : Parent { }
    class Child2 : Parent { }
    
  5. Hybrid Inheritance (not possible in C#)

    • It is a mixture of Multiple and Multi-Level Inheritance, but it is not directly supported in C#.


15. What is an Interface in C#?

Answer: An Interface is like a blueprint that contains only Method Declarations but no Implementation.

Key features:

  • Defines Abstract Methods.

  • Supports Multiple Inheritance.

  • It is implements in a class to provide an Implementation.

interface IAnimal
{
    void MakeSound();
}
class Dog : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("Bark");
    }
}


16. What is Form Handling?

Answer: Form Handling means taking, processing, and validating user input.

Main tasks:

  1. Taking data from TextBox, Button, and other controls.

  2. Processing or storing the data.

  3. Validating the user's input.

private void button1_Click(object sender, EventArgs e)
{
    string name = textBox1.Text;
    MessageBox.Show("Welcome " + name);
}

17. What is ADO.NET?

Answer: ADO.NET is a Data Access Technology that helps in creating connections with SQL Server, MySQL, etc.

SqlConnection con = new SqlConnection("connection string");

18. How is Exception Handling done in C#?

Answer: By using the try-catch-finally block.

try { int x = 10 / 0; }
catch (Exception ex) { Console.WriteLine(ex.Message); }
finally { Console.WriteLine("Execution Done!"); }

19. What are Delegates in C#?

Answer: A Delegate is a Pointer that references a Method.

delegate void MyDelegate(string msg);

20. What is the difference between an Event and a Delegate?

Answer:
DifferenceDelegateEvent
DefinitionIt is a type-safe function pointer.It is an advanced form of a delegate that is triggered.
Who can call it?Any method can call it.It can only be called from within the class where it is declared.
Multicasting SupportYes, more than one Method can be assigned to a Delegate.Yes, an Event also supports multicasting.
Exampledelegate void MyDelegate();event MyDelegate MyEvent;
public delegate void Notify();
public class ProcessBusinessLogic
{
    public event Notify ProcessCompleted;
}

21. What are Namespaces in C#?

Answer: Namespaces are used to organize Classes and Methods into different Modules.

namespace MyNamespace { class MyClass { } }

22. What is the difference between Properties and Indexers?

DifferencePropertiesIndexers
DefinitionIt provides get and set accessors to control changes in data.It allows a class to be used like an Array.
Where is it used?When we need to get/set any data.When we want to access an object by an index number.
Syntaxpublic int Age { get; set; }public int this[int index] { get; set; }
Exampleobj.Age = 25;obj[0] = 10;

Properties Example:

class Person
{
    public string Name { get; set; }
}

Indexers Example:

class SampleCollection
{
    private int[] arr = new int[10];
    public int this[int index]
    {
        get { return arr[index]; }
        set { arr[index] = value; }
    }
}

23. How is Multithreading done in C#?

Answer: The Thread class is used.

Thread t = new Thread(new ThreadStart(MyMethod));

24. Why is Crystal ReportViewer used?

Answer: It is used to Generate Reports in .NET.


25. When are Pointers used in C#?

Answer:

1. What is a Pointer?

A Pointer is a variable that stores a memory address. The use of Pointers is very common in languages like C and C++, but in C#, Pointers cannot be used in Safe code. Pointers are only used in Unsafe Code.

In C#, Pointers are generally not needed because C# is a Managed Language, and the Garbage Collector (GC) automatically manages memory. But in some special situations, when we need direct memory access, Pointers are used.


2. When and why are Pointers used in C#?

The use of Pointers in C# is very limited and is only done in cases where high performance, direct memory access, and low-level programming are required.

Pointers are used in these situations:
  1. High-Performance Applications:

    • When direct access to memory is required and performance is critical.

    • Used in game development, image processing, and networking applications.

  2. Interop with Unmanaged Code:

    • When we need to interact with C, C++, or other Unmanaged Code (which works outside the .NET framework).

    • In cases of P/Invoke and COM Interop, Pointers are used.

  3. Memory Management and Optimization:

    • When we need to access direct memory addresses from the Heap or Stack.

    • Especially used in Embedded Systems and Hardware-Level Programming.

  4. Optimized operations on Arrays and Buffers:

    • When we need to work with very large datasets and increase speed with direct memory access.


26. What is a Windows Forms Application?

Answer:
A Windows Forms Application is a graphical user interface (GUI) based application, developed using C# or VB.NET in the .NET Framework. It is made for the Windows operating system and its interface is designed using various controls (like buttons, textboxes, labels).
Important tools used in a Windows Forms application:

  • Form Designer – To create the GUI.

  • Toolbox – To add Controls (Button, Label, TextBox).

  • Properties Window – To set the properties of a control.

Example:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
}

27. What are the Common Controls in Windows Forms?

Answer:
There are many types of UI controls in Windows Forms, which are used to improve interaction in an application. Some important controls are as follows:

  1. Label – To display text.

  2. TextBox – To take input from the user.

  3. Button – To trigger an action.

  4. ComboBox – To show options as a dropdown menu.

  5. CheckBox – To select more than one option.

  6. RadioButton – To select only one option.

  7. PictureBox – To display an image.

  8. Panel – To group content.

Example:

private void button1_Click(object sender, EventArgs e) {
    label1.Text = "Button Clicked!";
}

28. What is Event-Driven Programming?

Answer:
Event-Driven Programming is a programming model in which the execution of code is done when an event occurs (like a button click, mouse movement).

Main elements:

  1. Event Source – The object that generates the event (like a Button).

  2. Event Handler – The method that processes the event.

  3. Delegate – A mechanism that connects an event to a method.

Example:

private void button1_Click(object sender, EventArgs e) {
    MessageBox.Show("Button Clicked!");
}

29. What are MessageBox and InputBox?

Answer:
MessageBox and InputBox are used to display information to the user and take input.

  • MessageBox: To show the user information or a warning.

  • InputBox: To take input from the user.

Example:

MessageBox.Show("This is a message box!", "Notification", MessageBoxButtons.OK);
string input = Microsoft.VisualBasic.Interaction.InputBox("Enter your name", "Input Box", "Default Text");

30. What is ADO.NET and how does it work?

Answer:
ADO.NET is a technology for accessing databases in the .NET Framework. It provides a connection to work with databases like SQL Server, MySQL, Oracle.

ADO.NET Components:

  1. Connection – To connect to the database.

  2. Command – To Execute an SQL Query.

  3. DataReader – To read Read-only and Forward-only data.

  4. DataSet & DataAdapter – To store data in a disconnected architecture.

Example:

SqlConnection con = new SqlConnection("your_connection_string");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Users", con);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) {
    Console.WriteLine(reader["Username"]);
}
con.Close();

31. What is the difference between SQLDataAdapter and SQLDataReader?

Answer:

Feature SqlDataAdapter SqlDataReader
Nature Disconnected Connected
Performance Slower Faster
Data Retrieval Loads all data at once Loads data as a stream
Read-Only No Yes

32. What is Object-Oriented Programming (OOP)?

Answer:
OOP is a programming style in which data and the methods that process it are divided into objects.

The 4 main principles of OOP:

  1. Encapsulation – Hiding data.

  2. Abstraction – Showing necessary information, hiding the rest.

  3. Inheritance – Taking the properties of one class into another.

  4. Polymorphism – Having multiple methods with the same name.

Example:

class Animal {
    public virtual void Sound() { Console.WriteLine("Animal Sound"); }
}
class Dog : Animal {
    public override void Sound() { Console.WriteLine("Bark"); }
}

33. What is the difference between a Delegate and an Event?

Answer:
A Delegate is a function pointer, while an Event is a special Delegate used in UI Components.

public delegate void MyDelegate();
public event MyDelegate MyEvent;

34. What is Crystal ReportViewer?

Answer:
Crystal ReportViewer is used to generate reports in .NET applications. It is used to load data from a database and save it in PDF, Excel formats.


35. What are Indexers?

Answer:
Indexers allow a class to be accessed like an Array.

class Sample {
    private int[] arr = new int[5];
    public int this[int index] {
        get { return arr[index]; }
        set { arr[index] = value; }
    }
}

36. What is the difference between Properties and Fields?

Answer:

  • Field: Stores data inside a class.

  • Property: Provides a way to access and update data.

private int _age;
public int Age {
    get { return _age; }
    set { _age = value; }
}

37. What is Multithreading and why is it used?

Answer:
Multithreading is used to perform multiple tasks at the same time.

Thread t1 = new Thread(Method1);
t1.Start();

38. Why are Pointers used?

Answer:
Pointers are used for Low-Level memory access, but they only work in unsafe mode in C#.

unsafe {
    int a = 10;
    int* ptr = &a;
}

Here are 12 more questions to make a total of 50.


39. What is a Destructor and how does it work?

Answer:
A Destructor is a special method that is called before an object is destroyed and is used for memory cleanup.
In C#, the name of a Destructor is written by putting a ~ (tilde) before the class name.

class Demo {
    ~Demo() {
        Console.WriteLine("Destructor called");
    }
}

A Destructor cannot be called manually; it is automatically called by the Garbage Collector.


40. What is the difference between a Static Class and a Non-Static Class in C#?

Answer:

Feature Static Class Non-Static Class
Object Creation Cannot create an object Can create an object
Memory Allocation Only once New every time
Methods Only static methods Can have both static and non-static methods
static class MathUtils {
    public static int Square(int num) {
        return num * num;
    }
}

MathUtils.Square(5); // 25


41. What is a Partial Class and what is its use?

Answer:
A Partial Class allows a class to be divided into more than one file.

public partial class Person {
    public string Name { get; set; }
}
public partial class Person {
    public int Age { get; set; }
}

A partial class is used for dividing a large class, auto-generated code, and team work.


42. What is a Sealed Class?

Answer:
A Sealed Class is a class that cannot be inherited.

sealed class FinalClass {
    public void Show() { Console.WriteLine("Hello"); }
}

A sealed class is used when we want to fix a class and not make it a child class.


43. What is a Namespace in C# and why is it used?

Answer:
A Namespace is a logical grouping used to prevent conflicts between different classes with the same name.

namespace MyNamespace {
    class MyClass {
        public void Show() { Console.WriteLine("Inside MyNamespace"); }
    }
}

It is used for organizing large applications and managing different libraries.


44. What is the difference between an Interface and an Abstract Class?

Answer:

Feature Interface Abstract Class
Implementation Only declaration Can be partially implemented
Constructor Does not have one Can have one
Access Modifiers Default public Can use any access modifier
Multiple Inheritance Allows Does not allow
interface IAnimal {
    void Speak();
}
abstract class Animal {
    public abstract void Speak();
}

45. What are Generics in C#?

Answer:
Generics are used to increase Code Reusability.

class Box {
    public T Data;
}
Box intBox = new Box();
Box strBox = new Box();

Generics are used in data storage structures like List, Dictionary, Stack.


46. What are Try, Catch, and Finally Blocks?

Answer:
try, catch, and finally are used for Exception Handling.

try {
    int x = 10 / 0;
} catch (Exception e) {
    Console.WriteLine(e.Message);
} finally {
    Console.WriteLine("This will always execute.");
}

The finally block always executes, whether there is an exception or not.


47. What is LINQ (Language Integrated Query) and what is its use?

Answer:
LINQ (Language Integrated Query) is a modern way to query data in C#.

int[] numbers = { 1, 2, 3, 4, 5 };
var evenNumbers = from num in numbers where num % 2 == 0 select num;

LINQ provides a SQL-like syntax and works on Databases, Collections, XML, and other data sources.


48. What are Nullable Types in C#?

Answer:
Nullable Types (? operator) are used to store NULL in a variable.

int? num = null;
Console.WriteLine(num ?? 0); // Default value

It is used to get NULL values from a database.


49. What is the difference between ref and out?

Answer:

Feature ref out
Value Initialization Must be initialized beforehand Does not need to be initialized beforehand
Usage Modifies an existing value Assigns a new value
void Modify(ref int x) { x += 10; }
void Assign(out int x) { x = 100; }

50. How to Serialize and Deserialize JSON in C#?

Answer:
Newtonsoft.Json is used to convert JSON data into a C# object (Deserialize) and to convert a C# object into JSON (Serialize).

using Newtonsoft.Json;
class Person {
    public string Name { get; set; }
}
Person p = new Person { Name = "Ratan" };
string json = JsonConvert.SerializeObject(p);
Person newPerson = JsonConvert.DeserializeObject(json);