Document 2
Document 2
Document 2
Ayushi Upadhyay
E2262
C# PRACTICE QUESTIONS
Console.WriteLine(result1); // Output: 6
Console.WriteLine(result2); // Output: 22
}
}
// Accessing elements
int element = jaggedArray[1][0]; // Accessing the element 4
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
ArrayList:
- Declaration:
- 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`.
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.
Usage-
MyClass myObject = new MyClass();
myObject[2] = 42; // Setting value using indexer
int value = myObject[2]; // Getting value using indexer
Abstract Class:
Base class with abstract methods.
Cannot be instantiated on its own.
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!");
}
}
Field:
Direct variable storage.
Accessed directly.
Often lacks encapsulation.
Property:
Provides access via methods.
Encapsulation with getter/setter.
Allows additional logic and validation.
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.