MICRO
MICRO
9. Community support: Community support is essential for any framework to C# is a modern, object-oriented programming language developed by 4. What is exception handling ?
framework. ensure that developers can get help and share knowledge with each other. Microsoft as part of the .NET Framework. It is designed to be simple, efficient,
2. what is CLR ? describe the services provided by CLR. and easy to learn, making it a popular choice for developing a wide variety of Exception handling is a mechanism in programming that allows developers to
applications. handle errors and unexpected events that may occur during the execution of a
.NET Framework is a software development framework developed by Microsoft
CLR stands for Common Language Runtime, which is a component of the .NET program. When an error occurs, an exception is thrown, which can be caught
that provides a programming model, a comprehensive set of class libraries, and
Framework that provides a runtime environment for executing managed code. Some of the key features of C# include: and handled by the program.
a runtime environment for developing and executing applications on Windows-
It is responsible for managing memory, enforcing security, handling exceptions, 1. Object-oriented programming: C# supports object-oriented programming
based operating systems.
and providing other services that are required by .NET applications. concepts such as inheritance, polymorphism, and encapsulation. 1. try: This keyword is used to enclose a block of code that may throw an
The components of the .NET Framework : exception. The code inside the try block is executed normally, but if an
1. Common Language Runtime (CLR): It is the runtime environment that The services provided by CLR include: 2. Garbage collection: C# includes automatic memory management through exception is thrown, the program will jump to the catch block.
manages memory, security, and code execution for .NET applications. It also garbage collection, which helps developers avoid common memory-related
provides services such as garbage collection, exception handling, and thread 1. Memory management: CLR manages memory allocation and deallocation errors. 2. catch: This keyword is used to define a block of code that handles an
management. for .NET applications, including garbage collection to automatically free up exception that has been thrown. The catch block specifies the type of exception
memory that is no longer needed. 3. Type safety: C# enforces type safety at compile time, reducing the likelihood it can handle, and if that type of exception is thrown, the code inside the catch
2. Class Library: It is a collection of reusable classes, interfaces, and value types of runtime errors caused by type mismatches. block is executed.
that provide access to system functionality such as file I/O, networking, and 2. Code execution: CLR compiles and executes .NET code, including Just-In-
database connectivity. Time (JIT) compilation to convert intermediate language (IL) code into machine 4. Cross-platform development: C# can be used to develop applications that 3. finally: This keyword is used to define a block of code that is executed
code at runtime. run on Windows, Linux, and macOS. regardless of whether an exception was thrown or not. The finally block is
3. Language Interoperability: It allows developers to use multiple programming always executed, even if there is no catch block or if the catch block does not
languages to write .NET applications, including C#, Visual Basic .NET, F#, and 3. Security: CLR provides a secure execution environment for .NET applications, 5. Asynchronous programming: C# includes support for asynchronous handle the exception.
others. including code access security and role-based security. programming, which allows developers to write code that runs concurrently
without blocking the main thread. 4. throw: This keyword is used to manually throw an exception from within a
4. APIs: Application Programming Interfaces (APIs) are a set of protocols, 4. Exception handling: CLR provides a mechanism for handling exceptions that block of code. It allows you to create your own custom exceptions and throw
routines, and tools for building software applications. occur during the execution of .NET applications. 6. LINQ: C# includes Language-Integrated Query (LINQ), which provides a them when needed. The throw keyword is typically used in conjunction with
powerful way to query data from different sources using a single syntax. try-catch blocks to handle exceptions more effectively.
5. Architecture: The framework architecture is the high-level design of the 5. Type safety: CLR enforces type safety for .NET applications, preventing type For example, consider the following code:
framework, which defines how its components interact with each other. mismatches and other type-related errors. 7. Extension methods: C# allows developers to add new methods to existing
classes without modifying the original code. try
6. Documentation: Documentation is essential for any framework to ensure 6. Interoperability: CLR provides support for interoperability between .NET {
that developers can understand how to use it. code and code written in other languages, including COM and Win32 APIs. Some of the different types of applications that can be developed in C# int x = 10;
include: int y = 0;
7. Testing tools: Framework testing tools are used to test the functionality of 7. Debugging: CLR includes a debugging infrastructure that allows developers int result = x / y; // This will throw a DivideByZeroException
the framework and ensure that it works as expected. to debug .NET applications using tools such as Visual Studio. 1. Desktop applications }
2. Web applications catch (DivideByZeroException ex)
8. Development tools: Framework development tools are used to develop and 3. what is C# ? describe different features of C#. discuss 3. Mobile applications {
debug code within the framework. different types of application developed in C#. 4. Games Console.WriteLine("Error: " + ex.Message);
5. Machine learning }
public Person(string name) Console.WriteLine("Hi, my name is " + name + " and I'm studying
{ " + major + " in college"); 1. Default Constructor: This is a constructor that takes no parameters
For example, consider a class called Car with the following properties:
this.name = name; } and initializes the object with default values.
} }
public virtual void Introduce() 2. Parameterized Constructor: This is a constructor that takes one or c#
{ class Program
more parameters and initializes the object with those values. public class Car
Console.WriteLine("Hi, my name is " + name); {
} static void Main(string[] args) {
} { 3. Copy Constructor: This is a constructor that creates a new object by public string Make { get; set; }
class Student : Person Person person = new Person("John");
{ person.Introduce(); copying the values of an existing object. public string Model { get; set; }
protected int grade; public int Year { get; set; }
Student student = new Student("Jane", 10);
public Student(string name, int grade) : base(name) student.Introduce();
{ public Car(string make, string model, int year)
this.grade = grade; CollegeStudent collegeStudent = new CollegeStudent("Mary", 12,
A parameterized constructor is a constructor that takes one or more {
} "Computer Science");
public override void Introduce() collegeStudent.Introduce(); parameters. It is used to initialize the object with specific values passed Make = make;
{ as arguments. The parameters can be of any data type, including Model = model;
Console.WriteLine("Hi, my name is " + name + " and I'm in grade Console.ReadKey();
" + grade); } custom classes. Year = year;
} } } }
}
Formal Parameter: A formal parameter is a parameter that is float f = i; //implicit conversion from int to float Invocation of methods refers to the process of calling or executing a a namespace is a way to organize and group related classes, structures,
specified in the method signature or declaration. It is also known as a method in a program. When a method is invoked, the program jumps to the interfaces, enums, and delegates. It provides a way to avoid naming
parameter variable. Formal parameters are used to define the input In this example, the integer value 10 is implicitly converted to a float method's code and executes it. The method may or may not return a value conflicts between different sets of code and allows for easier code
values that a method accepts. value without any explicit conversion code. depending on its implementation. Method invocation typically involves maintenance and organization. Namespaces can be declared using the
Example: passing one or more arguments (actual parameters) to the method, which are namespace keyword and can be nested within other namespaces. They
public void PrintName(string name) used by the method to perform its task. Once the method completes its are often used to group related functionality into separate logical units
2. Explicit Type Conversion: This type of conversion is done explicitly
execution, control is returned to the calling code. Method invocation is a and to provide a hierarchical structure to the code. Namespaces can be
{ by the programmer using casting operators. There are two types of
fundamental concept in object-oriented programming and is used extensively imported or referenced in other parts of the code using the using
Console.WriteLine("Hello, " + name); explicit type conversion: narrowing and widening.
in developing software applications. keyword, which allows for easier access to the types defined within
} the namespace.
In this example, `name` is the formal parameter of the `PrintName` a. Narrowing Conversion: This type of conversion is used to convert a Structure
method. larger data type to a smaller data type, which can result in data loss. a structure is a user-defined data type that encapsulates a set of related Jagged array
For example: data members and behaviors into a single unit. It is similar to a class, Jagged array is a array of arrays such that member arrays can be of
Actual Parameter: An actual parameter is the value that is passed to but with some key differences. Structures are value types, meaning that different sizes. In other words, the length of each array index can differ. The
a method when it is called. It is also known as an argument. Actual double d = 123.45; they are stored on the stack rather than the heap, and they do not elements of Jagged Array are reference types and initialized to null by default.
parameters are used to provide the input values to a method.
support inheritance. They are often used for small, simple data Jagged Array can also be mixed with multidimensional arrays. Here, the number
int i = (int)d; //explicit narrowing conversion from double to int
structures such as points, rectangles, and other geometric shapes. of rows will be fixed at the declaration time, but you can vary the number of
Structures can also include methods, properties, and events, just like columns.
Example: In this example, the double value 123.45 is explicitly converted to an classes. In Jagged arrays, user has to provide the number of rows only. If the user is also
PrintName("John"); integer value by truncating the decimal part, resulting in a potential going to provide the number of columns, then this array will be no more Jagged
In this example, `"John"` is the actual parameter that is passed to the loss of precision. Enumeration Array.
`PrintName` method. an enumeration is a user-defined data type that consists of a set of Syntax:
named constants, also known as enumerators. Each enumerator data_type[][] name_of_array = new data_type[rows][]
b. Widening Conversion: This type of conversion is used to convert a represents a distinct value of the enumeration type. Enumerations are Example:
Type conversion, also known as type casting, is the process of smaller data type to a larger data type, which does not result in data often used to define a set of related values that have a fixed number of int[][] jagged_arr = new int[4][]
converting a value from one data type to another. In C#, there are two loss. For example: possible values, such as the days of the week or the months of the year.
types of type conversion: implicit and explicit. Enumerations can be declared using the enum keyword and can include
int i = 10; custom values, attributes, and methods. They are often used in switch
statements and other control flow constructs to provide more In the above example, a single-dimensional array is declared that has 4
1. Implicit Type Conversion: This type of conversion is done float f = (float)i; //explicit widening conversion from int to float elements(rows), each of which is a 1-D array of integers
meaningful and readable code.
automatically by the compiler when there is no risk of data loss. For
example:
Namespace
int i = 10; Invocation of methods
To declare an array in C#, you use square brackets after the data type In this example, we access the third element of the numbers array
and specify the number of elements in the array: using its index (which is 2 because of zero-indexing) and assign it to
the variable thirdNumber.
c#
int[] numbers = new int[5]; Arrays in C# are reference types, meaning that when you pass an
array to a method or assign it to another variable, you are actually
passing or assigning a reference to the original array in memory. This
In this example, we declare an integer array called numbers with 5 can have implications for how you manipulate arrays and how
elements. The new keyword is used to create a new instance of the changes to an array in one part of your code affect other parts of your
array with the specified size. code.
You can also initialize an array with values using an array initializer:
c#
int[] numbers = { 1, 2, 3, 4, 5 };