0% found this document useful (0 votes)
17 views10 pages

Writing Code Compiling Code Generating Intermediate Language

Uploaded by

onpointads91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

Writing Code Compiling Code Generating Intermediate Language

Uploaded by

onpointads91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Key Components of the .

NET Framework:

1. Common Language Runtime (CLR):


o The execution environment that handles code execution, memory management, garbage
collection, and exception handling.
o It converts Intermediate Language (IL) code to machine code using the JIT (Just-In-Time)
compiler.
2. Framework Class Library (FCL):
o A collection of reusable classes, interfaces, and APIs for performing various tasks like file
handling, database interaction, and web development.
3. Languages:
o The .NET Framework supports multiple programming languages, such as C#, VB.NET, and
F#, enabling developers to build applications using their preferred language.
4. ASP.NET:
o A part of the framework used for building dynamic web applications.

Key Features of the .NET Framework:

 Cross-language Interoperability: Enables communication between applications written in different


.NET languages.
 Managed Code: Provides a secure environment by running code under the management of the CLR.
 Base Class Library (BCL): Provides a consistent object-oriented programming environment.

The process of compiling in .NET Framework involves several steps:

 Writing code: The developer writes code in a programming language


 Compiling code: The code is compiled using a compiler
 Generating intermediate language: The compiler converts the source code into
an intermediate language, like Microsoft Intermediate Language (MSIL) or
Common Intermediate Language (CIL)
 Converting to native code: The Common Language Runtime (CLR) converts
the intermediate language into native code for the machine
 Executing code: The processor runs the native code

CTS (Common Type System):

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.

Key Points of CTS:

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.

Role of CIL (Common Intermediate Language):

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.

Key Points of CIL:

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.

Platform Independence in .NET:

The .NET Framework achieves platform independence through its CLR and CIL.

How Platform Independence Works:

1. Compilation to CIL: Source code is compiled into CIL, making it platform-independent.


2. Runtime Execution: The CLR on the target system compiles the CIL into machine-specific
instructions using JIT.
3. Framework Portability: With tools like .NET Core (now .NET), applications can run on Windows,
macOS, and Linux.

Advantages of Platform Independence:

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.

Key Issues in DLL Hell:

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.

How .NET Resolves DLL Hell:

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).

Key Features of Metadata:

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:

 Prevents name clashes between different libraries or classes.


 Makes code more readable and manageable.

STRING IN C# ALONG WITH DIFFERENT WAYS TO DECLARE TO STRING

In C#, a string is a sequence of characters used to represent text. Strings are


immutable, meaning their content cannot be changed once they are created. The
string keyword in C# is an alias for the System.String class.

Immutable String (string)

 The most common way to store and use strings in C# is by using the string keyword (alias for
System.String).

string message = "Hello, World!";

 Key Points:
o Strings are immutable.
o Any modification creates a new string object in memory.

2. Mutable String (StringBuilder)

 Use StringBuilder when frequent modifications to the string are required.

StringBuilder sb = StringBuilder("Hello");

Key Points:

o Ideal for scenarios where the string changes frequently.


o Provides better performance compared to immutable strings.

3. Character Array (char[])


 Strings can be stored as a sequence of characters in a character array.

char[] charArray = { 'H', 'e', 'l', 'l', 'o' };


string str = new string(charArray);

 Key Points:
o Useful when working with individual characters.
o Can be modified, unlike immutable strings.

4. String Literals

 Store constant string values in the code directly.

const string literal = "This is a constant string.";

 Key Points:
o String literals are immutable.
o Ideal for static or unchanging values.

BOXING AND UNBOXING

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).

int num = 10; // Value type

object obj = num; // Boxing: 'num' is wrapped into an object

Key Points:

 Boxing creates a new object on the heap.


 Useful for storing value types in collections like ArrayList or when polymorphism is required.

2. Unboxing

 Definition: Unboxing is the process of extracting a value type from the object (or reference type) it
was boxed into.

object obj = 10; // Boxing

int num = (int)obj; // Unboxing: The value is extracted back as 'int'

Key Points:

 Requires explicit casting.


 Throws an exception if the cast fails.

Garbage Collector in .NET


The Garbage Collector (GC) in .NET is an automatic memory management system that helps manage the
allocation and release of memory for .NET applications. Its main role is to reclaim memory occupied by
objects that are no longer in use, thereby avoiding memory leaks and optimizing the use of system resources.

Key Features of Garbage Collector

1. Automatic Memory Management:


o Frees developers from manually managing memory allocation and deallocation.
2. Efficient Resource Utilization:
o Optimizes memory usage by reclaiming unused memory.
3. Object Lifetime Tracking:
o Determines the lifetime of objects and clears memory when objects are no longer reachable.

How Garbage Collector Works

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.

Virtual Methods in C#:

A virtual method is a method defined in a class that allows derived classes to override it, providing their
own implementation.

Key Points:

 It has a default implementation in the base class.


 It can be overridden in derived classes using the override keyword.
 The method is called based on the runtime type of the object.

Example:

public class Animal


{
public virtual void Speak()
{
Console.WriteLine("Animal makes a sound.");
}
}

public class Dog : Animal


{
public override void Speak()
{
Console.WriteLine("Dog barks.");
}
}

Abstract Methods in C#:

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:

 It has no implementation in the base class.


 Derived classes must implement the method.
 It is used when you want to enforce that all derived classes must provide their own implementation
of the method.

Example:

public abstract class Animal


{
// Abstract method, no body
public abstract void Speak();
}

public class Dog : Animal


{
public override void Speak()
{
Console.WriteLine("Dog barks.");
}
}

class Program
{
static void Main()
{
Animal myDog = new Dog();
myDog.Speak(); // Outputs: Dog barks.
}
}

Primary Key vs Foreign Key Relationship

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).

(i) CLR (Common Language Runtime)


The CLR is the runtime environment in the .NET Framework responsible for managing the execution of
code. It provides services like garbage collection, exception handling, type safety, and security. The CLR
ensures that programs written in different .NET languages (such as C#, VB.NET) can interoperate
seamlessly.

Key Points:

 Manages memory through garbage collection.


 Provides type safety, ensuring that code only accesses allowed memory locations.
 Supports Just-In-Time (JIT) compilation, converting Intermediate Language (IL) code to machine
code at runtime.

(ii) CLS (Common Language Specification)

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.

(iii) DLL (Dynamic-Link Library)

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:

 Allows code sharing and reuse between programs.


 Supports dynamic linking, meaning functions can be called at runtime.
 Can contain executable code, resources, and data that programs can access.

(iv) Nullable Type

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:

 Defined using ?, e.g., int? or bool?.


 Allows value types to hold null in addition to their normal range of values.

IMPLICITLY TYPED LOCAL VARIABLE

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).

var number = 10; // Implicitly typed as int


var message = "Hello, World!"; // Implicitly typed as string

Differences Between var and dynamic

Aspect var dynamic


Type Type is determined at compile time
Type is determined at runtime, not at compile time.
Determination based on the assigned value.
Type-safe: Errors are caught during
Not type-safe: Errors occur at runtime if the type
Type Safety compilation if the assigned value
doesn't match the expected operations.
doesn't match the inferred type.
Used when you need to bypass compile-time type
Used for local variables where the checking, typically in scenarios like working with
Usage
type can be inferred. COM objects, reflection, or dynamically typed
languages.
Must be initialized with a value at Can be declared without initialization and assigned a
Initialization
the time of declaration. value later.
Faster performance due to compile- Slower performance as types are resolved at
Performance
time type checking. runtime.

Method Overloading Constructor


A feature in OOP that allows a class to have A special method that is called automatically
Definition multiple methods with the same name but when an object is created to initialize the
different parameter lists. object.
To perform similar operations with different input parameters, To initialize the state of an object
Purpose
improving code readability and flexibility. when it is created.
Method overloading occurs with methods that share the same The constructor always has the
Naming
name but differ in the number or type of parameters. same name as the class.
Return Methods can have any return type, and it can vary A constructor does not have a return
Type across overloaded methods. type, not even void.
Methods are called explicitly A constructor is called implicitly when an object is created
Invocation
when needed. using the new keyword.
Used to perform the same operation with different types or Used to initialize an object at the time
Usage
numbers of arguments. of its creation.
Methods can be overloaded by changing Constructors can also be overloaded (i.e., multiple
Overloading
the number or type of parameters. constructors with different parameters).
java<br>int add(int a) { return a +
java<br>Person(String name, int age)
Example 10; }<br>int add(int a, int b)
{ this.name = name; this.age = age; }
{ return a + b; }
Call Explicitly called by the program wherever Implicitly called when an object is created, e.g., new
Type needed. Person();.

Method Overriding

 Occurs between a parent class and its child class.


 Methods have the same name, parameters, and return type.
 Allows subclasses to provide specific implementations of inherited methods.
 Example:

public class Animal


{
public virtual void MakeSound() { ... }
}

public class Dog : Animal


{
public override void MakeSound() { ... }
}

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.

Key Concepts in C# Exception Handling:

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.

Internal vs Public vs Private:

 public: Accessible from any code, inside or outside the assembly.


 private: Accessible only within the same class or struct.
 internal: Accessible only within the same assembly. (internal k code dekh lena )

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.

Interface. Abstract class

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.

You might also like