.NET is a free, open-source, cross-platform framework developed by Microsoft for building applications. It supports multiple programming languages like C#, VB.NET, and F#. The framework provides a unified environment for creating a wide range of applications, such as:
- Desktop Applications using Windows Forms or WPF.
- Web Applications using ASP.NET Core.
- Mobile Applications using Xamarin.
- Cloud Services using Azure.
Key Components:
- Common Language Runtime (CLR): Manages code execution, memory, and garbage collection.
- Base Class Library (BCL): A collection of reusable classes, interfaces, and value types.
- .NET SDK: A set of tools and libraries to build applications.
Scenario: Create a simple console application to demonstrate .NET basics.
-
Install .NET SDK from dotnet.microsoft.com.
-
Create a new project:
dotnet new console -n HelloWorldApp cd HelloWorldApp dotnet run -
Modify the
Program.cs:Console.WriteLine("Hello, .NET World!");
-
Execute it using:
dotnet run
Types of Loops:
-
For Loop:
for (int i = 0; i < 5; i++) { Console.WriteLine($"Iteration {i}"); }
-
While Loop:
int i = 0; while (i < 5) { Console.WriteLine($"Iteration {i}"); i++; }
-
Do-While Loop:
int i = 0; do { Console.WriteLine($"Iteration {i}"); i++; } while (i < 5);
-
Foreach Loop:
string[] fruits = { "Apple", "Banana", "Cherry" }; foreach (var fruit in fruits) { Console.WriteLine(fruit); }
-
Boxing: Converting a value type to a reference type.
int num = 10; object obj = num; // Boxing
-
Unboxing: Extracting a value type from an object.
int unboxedNum = (int)obj; // Unboxing
Defining multiple methods with the same name but different parameters.
class Calculator
{
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
}A constructor initializes an object of a class.
class Person
{
public string Name { get; }
public Person(string name) { Name = name; }
}Static methods belong to the class, not an instance.
class MathUtil
{
public static int Square(int num) => num * num;
}Derived classes inherit members of the base class.
class Animal { public void Eat() => Console.WriteLine("Eating"); }
class Dog : Animal { public void Bark() => Console.WriteLine("Barking"); }-
Compile-time Polymorphism (Method Overloading).
-
Runtime Polymorphism (Method Overriding).
class Parent { public virtual void Speak() => Console.WriteLine("Parent speaks."); } class Child : Parent { public override void Speak() => Console.WriteLine("Child speaks."); }
-
String: Immutable.
string str = "Hello"; str += " World"; // New string is created.
-
StringBuilder: Mutable.
StringBuilder sb = new StringBuilder("Hello"); sb.Append(" World"); // Modifies the existing object.
Handle runtime errors using try-catch-finally.
try
{
int result = 10 / 0;
}
catch (DivideByZeroException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Execution finished.");
}An abstract class provides a blueprint for derived classes.
abstract class Shape
{
public abstract double CalculateArea();
}
class Circle : Shape
{
public double Radius { get; set; }
public override double CalculateArea() => Math.PI * Radius * Radius;
}Connected architecture communicates directly with the database using SqlConnection and SqlCommand.
using System.Data.SqlClient;
string connectionString = "your_connection_string";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM Users", connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["Username"]);
}
}LINQ (Language-Integrated Query):
int[] numbers = { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0);Lambda Expressions:
Func<int, int> square = x => x * x;
Console.WriteLine(square(5)); // Output: 25