Writing Code Compiling Code Generating Intermediate Language
Writing Code Compiling Code Generating Intermediate Language
NET Framework:
The Common Type System (CTS) is a standard in the .NET Framework that defines how data types are
declared, used, and managed. It ensures that types declared in different languages can interoperate with one
another.
1. Type Unification: Ensures all .NET-supported languages use the same data types (e.g., int in C#
and Integer in VB.NET map to the same type).
2. Value Types and Reference Types:
o Value Types: Contain data directly (e.g., int, float).
o Reference Types: Store references to data (e.g., class, object).
3. Cross-Language Interoperability: Enables interaction between code written in different .NET
languages.
The Common Intermediate Language (CIL) is the low-level programming language used by the .NET
Framework. It's generated when source code is compiled and is platform-independent.
1. Intermediate Language: Code written in any .NET language is compiled into CIL.
2. JIT Compilation: At runtime, CIL is converted into machine-specific code by the Just-In-Time
(JIT) compiler.
3. Platform Independence: Since CIL is hardware-independent, .NET assemblies can run on any
system with a compatible CLR.
The .NET Framework achieves platform independence through its CLR and CIL.
1. Write Once, Run Anywhere: Applications can be deployed across different environments.
2. Ease of Development: Developers focus on logic rather than hardware compatibility.
DLL Hell refers to the problems caused by the use of shared Dynamic Link Libraries (DLLs) in Windows,
particularly in earlier systems before the .NET Framework. It occurs when applications overwrite or modify
shared DLL files, leading to compatibility issues or application crashes.
1. Version Conflicts: An application may install a different version of a DLL that breaks other
applications using the same DLL.
2. Dependency Problems: Missing or outdated DLLs may cause applications to fail.
3. Difficulty in Managing Dependencies: It was challenging to track which applications were
dependent on specific DLL versions.
1. Assembly Versioning: Each .NET assembly has a unique version number, allowing multiple
versions of the same DLL to coexist.
2. Private Assemblies: Applications can use their private copy of a DLL, avoiding interference with
other applications.
3. Global Assembly Cache (GAC): Shared assemblies can be centrally managed in the GAC, ensuring
proper versioning and security.
Metadata in .NET
Metadata is data that describes other data. In the context of .NET, metadata provides a detailed description
of the types, members, and references within an assembly. It is stored alongside the Intermediate Language
(CIL) in the assembly (.dll or .exe file).
1. Describes the Code: Includes information about classes, methods, properties, events, and more.
2. Facilitates Reflection: Enables runtime type discovery and dynamic method invocation through
reflection.
3. Self-Describing Assemblies: Assemblies contain all the information needed for the CLR to execute
the code.
Namespace:
A namespace is a logical grouping of related classes, interfaces, enums, and other types.
It helps organize code and avoid name conflicts.
Example: System.Collections.Generic contains classes like List<T> and Dictionary<TKey,
TValue>.
Benefits of Namespace:
The most common way to store and use strings in C# is by using the string keyword (alias for
System.String).
Key Points:
o Strings are immutable.
o Any modification creates a new string object in memory.
StringBuilder sb = StringBuilder("Hello");
Key Points:
Key Points:
o Useful when working with individual characters.
o Can be modified, unlike immutable strings.
4. String Literals
Key Points:
o String literals are immutable.
o Ideal for static or unchanging values.
Boxing and unboxing are concepts in C# that deal with converting value types (like int, double) to
reference types (like object) and vice versa.
1. Boxing
Definition: Boxing is the process of converting a value type into an object (or any reference type
compatible with it).
Key Points:
2. Unboxing
Definition: Unboxing is the process of extracting a value type from the object (or reference type) it
was boxed into.
Key Points:
1. Memory Allocation:
o When a program creates an object, the runtime allocates memory for it from the managed
heap.
2. Mark-and-Sweep Algorithm:
o Mark: Identifies all objects that are still in use or referenced.
o Sweep: Reclaims the memory of objects that are no longer in use.
3. Generations in GC:
o GC classifies objects into three generations for optimization:
Generation 0: Short-lived objects (e.g., temporary variables).
Generation 1: Objects that survived one GC cycle.
Generation 2: Long-lived objects (e.g., static data, global variables).
4. Finalization:
o Before reclaiming an object, the GC runs the object's finalizer (if implemented) to clean up
resources like file handles or database connections.
5. Compacting Memory:
o After clearing unused objects, GC compacts the heap to eliminate fragmentation, ensuring
that memory is efficiently utilized.
A virtual method is a method defined in a class that allows derived classes to override it, providing their
own implementation.
Key Points:
Example:
An abstract method is a method declared in an abstract class that does not have a body. It must be
implemented in a non-abstract derived class.
Key Points:
Example:
class Program
{
static void Main()
{
Animal myDog = new Dog();
myDog.Speak(); // Outputs: Dog barks.
}
}
1. Primary Key:
o Purpose: Uniquely identifies records within its own table.
o Behavior: No duplicate or NULL values allowed in the column(s).
2. Foreign Key:
o Purpose: Links one table to another by referring to the primary key of another table.
o Behavior: Allows NULL values and enforces the integrity of the relationship (i.e., ensuring
that only valid keys from the referenced table are used).
Key Points:
The CLS is a set of rules that define how .NET languages should interact with each other. It ensures that
code written in different .NET languages can work together. The CLS defines a subset of the Common Type
System (CTS) that all .NET languages must follow.
Key Points:
Specifies common conventions for naming and types to ensure language interoperability.
Ensures that a component written in one .NET language can be used by another language.
Provides a baseline for writing code that can be shared across different languages.
A DLL is a file that contains compiled code that can be used by multiple programs simultaneously. It allows
code to be modularized and reused. DLL files typically contain functions, classes, and resources that can be
accessed by applications at runtime.
Key Points:
A Nullable Type is a data type that can hold both its normal range of values and a null value. It is
commonly used for value types (like int, float, etc.) when there is a need to represent an undefined or
missing value.
Key Points:
In C#, implicitly typed local variables are variables where the type is inferred by the compiler based on the
assigned value. The var keyword is used to declare these variables, and the compiler determines the type at
compile time.
Key Points:
The type of the variable is automatically determined by the compiler based on the right-hand side
expression.
The type is fixed and cannot change after initialization.
var can only be used for local variables (not fields, parameters, or properties).
Method Overriding
EXCEPTION HANDLING
In C#, Exception Handling works similarly to other object-oriented languages, allowing developers to
handle runtime errors in a structured and controlled way. It uses the try, catch, finally, throw, and
throws (in method signatures) blocks to deal with exceptions.
1. Exception: An error that occurs during the execution of the program. C# exceptions are derived from
the System.Exception class.
2. try Block: Contains the code that might throw an exception.
3. catch Block: Catches the exception thrown by the try block and allows you to handle it.
4. finally Block: A block of code that runs after the try and catch blocks, regardless of whether an
exception was thrown or not. This is typically used to release resources.
5. throw: Used to explicitly throw an exception.
6. throws: In C#, the throws keyword isn't used like in some other languages (e.g., Java). Instead, C#
methods simply throw exceptions as needed without declaring them in the method signature.
using System;
class ExceptionExample
{
public static void Main(string[] args)
{
try
{
int result = 10 / 0; // This will throw DivideByZeroException
}
catch (DivideByZeroException e)
{
Console.WriteLine("Error: Division by zero is not allowed.");
}
finally
{
Console.WriteLine("This block always runs.");
}
}
}
the internal keyword is an access modifier that controls the visibility and accessibility of types and
members within an assembly. When a class, method, or property is marked as internal, it is accessible
only within the same assembly (i.e., the project or compiled DLL) but not from outside that assembly.
Key Points about the internal Keyword:
1. Access Within Assembly: Any internal members or types are accessible only to code within the
same assembly. This helps encapsulate the functionality that should not be exposed outside the
assembly.
2. Default Access for Types: If no access modifier is specified for a class, it is internal by default.
This means the class will be accessible within the same assembly, but not from outside it.
3. Cannot be Accessed from Other Assemblies: internal members or types cannot be accessed from
code in other assemblies, even if they are referenced.
4. Use Case: The internal keyword is commonly used when you want to hide implementation details
within the same project or assembly, but still allow code in that project to access the functionality.
interface in C# is a defines a set of methods, properties that a class must implement. It only specifies the
signature of these members (i.e., their name, return type, and parameters), but does not provide any
implementation. Classes or structs that implement the interface are required to define the behavior of the
methods declared in the interface.
abstract class in C# is a class that can contain both fully implemented methods
(concrete methods) and unimplemented methods (abstract methods). Abstract
methods must be implemented by the derived classes. The purpose of an abstract
class is to provide a common base with shared functionality while allowing derived
classes to provide specific implementations for abstract methods.
Can only contain method signatures Can contain both abstract methods (without implementation)
Methods
(no implementation). and concrete methods (with implementation).
Can only contain property declarations (no Can have both abstract and concrete
Properties
implementation). properties.
Multiple A class can implement multiple A class can inherit from only one abstract
Inheritance interfaces. class.
Access All members are implicitly public (no Members can have various access modifiers
Modifiers access modifiers allowed). (public, private, protected, etc.).
Fields Cannot have fields. Can have fields.
Constructors Cannot have constructors. Can have constructors.