Unit-1 .Net Framework And C# Programming
Unit-1 .Net Framework And C# Programming
Code
using System;
namespace HelloWorldApp {
class First {
sta c void Main(string[] args) {
Console.WriteLine("Hello World!");
}
}
}
The .NET Framework is a so ware development framework developed by Microso that provides a run me
environment and a set of libraries and tools for building and running applica ons on Windows opera ng systems. The
framework includes a variety of programming languages, such as C#, F#, and Visual Basic, and supports a range of
applica on types, including desktop, web, mobile, and gaming applica ons.
1. The .NET Framework includes two main components: the Common Language Run me (CLR) and the .NET
Framework Class Library. The CLR is responsible for managing the execu on of code wri en in any of the
supported languages, while the class library provides a large set of pre-built func ons and classes that can be
used to create a wide range of applica ons.
2. The .NET Framework also provides a number of features that help improve the security, reliability, and
performance of applica ons. These include features such as code access security, automa c memory
management, and just-in- me (JIT) compila on, which helps improve the speed of applica on execu on.
3. The .NET Framework is also designed to integrate with other Microso technologies, such as Microso SQL
Server, Microso SharePoint, and Microso Office, which can make it easier to build applica ons that work
seamlessly with other Microso products.
Overall, the .NET Framework is a powerful and versa le development pla orm that provides a wide range of tools and
libraries for building and running applica ons on Windows opera ng systems.
.NET Framework supports more than 60 programming languages of which 11 programming languages are designed and
developed by Microso . The remaining Non-Microso Languages are supported by .NET Framework but not designed
and developed by Microso .
Common Language Run me (CLR)
The Common Language Run me (CLR) is a component of the Microso .NET Framework that manages the execu on
of .NET applica ons. It is responsible for loading and execu ng the code wri en in various .NET programming
languages, including C#, VB.NET, F#, and others.
When a C# program is compiled, the resul ng executable code is in an intermediate language called Common
Intermediate Language (CIL) or Microso Intermediate Language (MSIL). This code is not machine-specific, and it can
run on any pla orm that has the CLR installed. When the CIL code is executed, the CLR compiles it into machine code
that can be executed by the processor.
The CLR provides many services to .NET applica ons, including memory management, type safety, security, and
excep on handling. It also provides Just-In-Time (JIT) compila on, which compiles the CIL code into machine code on
the fly as the program runs, op mizing performance.
Addi onally, the CLR provides a framework for developing and deploying .NET applica ons, including a set of libraries,
called the .NET Framework Class Library, which provides access to a wide range of func onality, such as input/output
opera ons, networking, database connec vity, and user interface design.
Overall, the CLR is a cri cal component of the .NET Framework and is responsible for ensuring that .NET applica ons
are executed in a safe, secure, and efficient manner, making it a fundamental aspect of C# programming.
Enumera ons
Enumera on (Enum) is a special "value type" that allows us to define a set of named constants such as season, days,
month, size etc. Enums are useful when we have a collec on of related values and want to work with them in a more
readable and structured way. Enums make code more understandable and reduce errors, especially when dealing with
fixed sets of values. The Enum constants are also known as enumerators. Enum in C# can be declared within or outside
class and structs. Enum constants has default values which starts from 0 and incremented to one by one. But we can
change the default value.
C# code for Enumera on: -
using System;
namespace Example {
class Program {
enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
public sta c void Main(string[] args){
Console.WriteLine("The value of Sunday in Days " + (int)Days.Sunday);
Console.WriteLine("The value of Monday in Days " + (int)Days.Monday);
Console.WriteLine("The value of Friday in Days " + (int)Days.Friday);
}
}
}
Structures
Structure is a value type and a collec on of variables of different data types under a single unit. It is almost similar to a
class because both are user-defined data types and both hold a bunch of different data types. Structs are used to create
lightweight objects that are typically smaller, simpler, and o en more efficient in memory compared to classes. Unlike
classes, structures are stored on the stack rather than the heap, making them ideal for small data types where
performance is cri cal.
C# code for Structure: -
using System;
namespace ConsoleApplica on{
class Geeks{
public struct Person{
public string Name;
public int Age;
public int Weight;
}
sta c void Main(string[] args)
{
Person P1;
P1.Name = "Keshav Gupta";
P1.Age = 21;
P1.Weight = 80;
Console.WriteLine("Data Stored in P1 is " + P1.Name + ", age is " +
P1.Age + " and weight is " +
P1.Weight);
}
}
}
Namespaces
Namespaces are used to organize the classes. It helps to control the scope of methods and classes in larger .Net
programming projects. In simpler words you can say that it provides a way to keep one set of names(like class names)
different from other sets of names. The biggest advantage of using namespace is that the class names which are
declared in one namespace will not clash with the same class names declared in another namespace. It is also referred
as named group of classes having common features. The members of a namespace can be namespaces, interfaces,
structures, and delegates.
C# code for Namespace: -
using System;
namespace First {
public class Hello
{
public void sayHello() { Console.WriteLine("Hello First Namespace"); }
}
}
namespace Second
{
public class Hello
{
public void sayHello() { Console.WriteLine("Hello Second Namespace"); }
}
}
public class TestNamespace
{
public sta c void Main()
{
First.Hello h1 = new First.Hello();
Second.Hello h2 = new Second.Hello();
h1.sayHello();
h2.sayHello();
}
}