0% found this document useful (0 votes)
25 views

Document 2

The document contains questions and answers about various C# concepts like params keyword, jagged arrays, hashtable vs dictionary, array vs ArrayList, managed vs unmanaged code, garbage collection, indexer, abstract vs sealed vs partial vs concrete classes, fields vs properties, extension methods, serialization, reflection, and delegates. It provides definitions and explanations of these concepts in C# programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Document 2

The document contains questions and answers about various C# concepts like params keyword, jagged arrays, hashtable vs dictionary, array vs ArrayList, managed vs unmanaged code, garbage collection, indexer, abstract vs sealed vs partial vs concrete classes, fields vs properties, extension methods, serialization, reflection, and delegates. It provides definitions and explanations of these concepts in C# programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PART 2

Ayushi Upadhyay
E2262
C# PRACTICE QUESTIONS

What are the Params in C#?


In C#, the params keyword is used in a method parameter to indicate that
the parameter can accept a variable number of arguments. This allows
you to pass a variable number of arguments of the specified type to the
method. The params keyword is particularly useful when you want to
create methods that can take a flexible number of parameters without
explicitly specifying an array.

public class ParamsExample


{
public static int Sum(params int[] numbers)
{
int result = 0;
foreach (int num in numbers)
{
result += num;
}
return result;
}

public static void Main()


{
int result1 = Sum(1, 2, 3); // Sum of 1, 2, and 3
int result2 = Sum(4, 5, 6, 7); // Sum of 4, 5, 6, and 7

Console.WriteLine(result1); // Output: 6
Console.WriteLine(result2); // Output: 22
}
}

What is Jagged Array?


A jagged array in C# is an array of arrays, where each element of the
main array is an array itself. Unlike a multidimensional array, the sub-
arrays in a jagged array can have different lengths. This provides a
flexible way to represent irregular or ragged data structures.
int[][] jaggedArray = new int[3][]; // Creating a jagged array with 3 sub-
arrays

// Initializing sub-arrays with different lengths


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

// Accessing elements
int element = jaggedArray[1][0]; // Accessing the element 4

Explain Hashtable versus Dictionary?


Hashtable:

 Namespace: System.Collections
 Type: Part of the non-generic collection classes.
 Key Characteristics:
 Stores key-value pairs.
 Supports access, insertion, and deletion of elements based on
keys.
 Allows storing different data types for keys and values
(unlike the generic Dictionary).
 Not type-safe; requires explicit casting when retrieving
values.

Dictionary:

 Namespace: System.Collections.Generic
 Type: Part of the generic collection classes.
 Key Characteristics:
 Stores key-value pairs.
 Supports access, insertion, and deletion of elements based on
keys.
 Type-safe; keys and values have specified data types.
 Provides better performance and type safety compared to
Hashtable.

Key Differences:
 Type Safety: Dictionary is type-safe, ensuring that keys and
values have specific data types. Hashtable requires explicit casting.
 Performance: Dictionary is generally more efficient due to being
part of the generic collections and avoiding boxing/unboxing operations.
 Null Values: Dictionary allows null values for both keys and
values. In Hashtable, null is allowed only for values, not for keys.
 Enumeration: Dictionary supports strongly typed enumeration
(foreach) without explicit casting

Explain Array and ArrayList?


Array:
- Declaration:

int[] numbers = new int[5]; // Array of integers with length 5


- Characteristics:
- Fixed-size, type-safe collection.
- Direct access using indices.

ArrayList:
- Declaration:

ArrayList list = new ArrayList(); // Creating an ArrayList

- Characteristics:
- Dynamic, not type-safe collection.
- Elements of different data types.

Example:

// Array example
int[] numbers = new int[] { 1, 2, 3, 4, 5 };

// ArrayList example
ArrayList list = new ArrayList();
list.Add(1);
list.Add("two"); // Allowed in ArrayList
list.Add(3.0);

use arrays when you need a fixed-size, type-safe collection with direct access.
Use `ArrayList` when you need a dynamic collection that can hold elements of
different data types. For modern C# development, prefer generic collections
like `List<T>` over `ArrayList`.

What is Managed and Unmanaged Code ?


Managed Code:

Definition: Code written in .NET languages such as C# or VB.NET that runs


within the Common Language Runtime (CLR).

 Characteristics:
 Executed by the CLR, providing services like memory
management, security, and exception handling.
 Managed by the .NET runtime, ensuring type safety and
automatic garbage collection.
 C# and VB.NET are examples of managed languages.

Unmanaged Code:

 Definition: Code that does not run within the CLR and is typically
written in languages like C or C++.
 Characteristics:
 Does not benefit from CLR services; manual memory
management is often required.
 Not subject to CLR's type safety and automatic garbage
collection.
 Examples include native Windows API calls and legacy code
written without CLR support.

How Code Compiled in C#?


 Compilation:
 C# code is compiled into Intermediate Language (IL) code.
 Execution:
 IL code is Just-In-Time (JIT) compiled to native machine code at
runtime by the CLR.
 Result:
 Platform-agnostic assemblies (.dll or .exe) containing IL code.

What is meant by Garbage Collection in C#?


 Automatic process for deallocating memory in C#.
 Identifies and frees up memory that is no longer in use.
 Enhances program stability and simplifies memory management for
developers.

Explain Indexer in C#?


 Definition: Special type of property that allows instances of a class or
struct to be indexed like arrays.
 Purpose: Enables objects to be treated like arrays, providing a way to
access elements using indexers.
public class MyClass
{
private int[] data = new int[5];

public int this[int index]


{
get { return data[index]; }
set { data[index] = value; }
}
}

Usage-
MyClass myObject = new MyClass();
myObject[2] = 42; // Setting value using indexer
int value = myObject[2]; // Getting value using indexer

What is Abstract class?


What is Partial Class?
What is sealed class?
What is Concrete class?


Abstract Class:

 Base class with abstract methods.
 Cannot be instantiated on its own.

public abstract class Shape


{
public abstract void Draw();
}

Partial Class:

 Class split into separate files for organization.
 Useful for large classes and code generation.

// File1.cs
partial class MyClass
{
// Part 1 of the class
}

// File2.cs
partial class MyClass
{
// Part 2 of the class
}

Sealed Class:

 Class that cannot be inherited.
 Restricts further derivation.

Concrete Class:

 Class with complete implementations.
 Can be instantiated and used to create objects.
public class Dog
{
public void Bark()
{
Console.WriteLine("Woof!");
}
}

State the difference between field and properties in C#?


Field:

 Direct variable storage.
 Accessed directly.
 Often lacks encapsulation.

Property:

 Provides access via methods.
 Encapsulation with getter/setter.
 Allows additional logic and validation.

What are Extension methods used in C#?


 Methods that allow adding new methods to existing types without
modifying them, even if the types are sealed or cannot be modified.
 Purpose:
 Enhances existing types with additional functionalities.
 Improves code readability and maintainability.

What do you mean by Serialization in C#?


Serialization:

The process of converting an object or data structure into a format that can be
easily persisted, transmitted, or reconstructed.
 Converts objects into a format for storage or transmission.
 Types include binary, XML, and JSON serialization.
 Facilitates interoperability and persistence of object state.

What is Reflection In C#?


Reflection:
 Inspects and interacts with metadata at runtime.
 Enables dynamic discovery and usage of types.
 Useful for scenarios where types are not known at compile-time
Type type = typeof(MyClass); // Get the Type object for MyClass
MethodInfo method = type.GetMethod("MyMethod"); // Get a method by
name
object instance = Activator.CreateInstance(type); // Create an instance
dynamically
method.Invoke(instance, null); // Invoke the method on the instance

Explain Delegate in C#?


 Represents a method signature.
 Enables defining, passing, and invoking methods dynamically.
 Commonly used for callback mechanisms and event handling in C#

You might also like