.Net Series Day 1

.Net Series Day 1

Why Do We Need MS .NET?

The .NET Framework is a software development framework designed by Microsoft. It acts as a platform for building, deploying, and running applications, providing developers with tools and libraries for various programming tasks. Here's why it's essential:

  1. Platform Independence:

    • The .NET Framework operates in two phases:

      • Phase 1 (Development): Developers write code in languages like C#, VB.NET, or J#. The code is compiled into an intermediate assembly language called MSIL (Microsoft Intermediate Language). This step is known as partial compilation, converting high-level code into low-level code.

      • Phase 2 (Execution): The MSIL code is translated into binary code or machine-executable code on the client machine using the .NET runtime.

  2. Language Interoperability:

    • .NET supports multiple programming languages and allows them to work together.

    • Any code written in C#, VB.NET, or J# is compiled into MSIL, which is language-agnostic. After compilation, it's impossible to determine the original programming language.


Key Components of .NET Framework

  1. CLR (Common Language Runtime):

    • Acts as the runtime environment for executing .NET applications.

    • Provides services like garbage collection, exception handling, and application monitoring.

    • Ensures smooth communication between the application and the operating system.

  2. FCL (Framework Class Library):

    • Provides a rich set of pre-defined classes and methods for tasks like database operations, multithreading, file I/O, and more.

Setting Up the Environment

  1. Installed Visual Studio 2022 for coding.

  2. Hands-on with examples using C#.

Notes While Coding in C#:

  • C# is case-sensitive.

  • Statements end with a semicolon (;).

  • Output Method: Console.WriteLine("Hello!!");

  • Input Method: Console.ReadLine();

  • Naming Conventions:

    • Class Names: PascalCase (e.g., EmployeeDetails)

    • Method Names: PascalCase (e.g., GetEmployeeData())

    • Variable and Object Names: camelCase (e.g., employeeName)


Basic C# Programs

1. Display Output

Using Console.WriteLine():

csharpCopy codeConsole.WriteLine("Welcome to .NET Programming!");

2. Arithmetic Operations

Program to add two numbers:

csharpCopy codeclass Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter first number:");
        int num1 = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter second number:");
        int num2 = int.Parse(Console.ReadLine());

        int sum = num1 + num2;
        Console.WriteLine($"The sum is: {sum}");
    }
}

3. Conditional Statements

Calculate Discount Based on Price:

  • If Statement:
csharpCopy codeif (price > 1000)
{
    discount = price * 0.10;
}
  • If-Else Statement:
csharpCopy codeif (price > 1000)
{
    discount = price * 0.10;
}
else
{
    discount = price * 0.05;
}
  • If-Else-If Ladder:
csharpCopy codeif (price > 2000)
{
    discount = price * 0.15;
}
else if (price > 1000)
{
    discount = price * 0.10;
}
else
{
    discount = price * 0.05;
}

4. Loops

For Loop (Factorial):

csharpCopy codeclass Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter a number:");
        int num = int.Parse(Console.ReadLine());

        int factorial = 1;
        for (int i = 1; i <= num; i++)
        {
            factorial *= i;
        }
        Console.WriteLine($"Factorial of {num} is {factorial}");
    }
}

While Loop (Example):

csharpCopy codeint counter = 1;
while (counter <= 5)
{
    Console.WriteLine($"Counter: {counter}");
    counter++;
}

Do-While Loop (Example):

csharpCopy codeint counter = 1;
do
{
    Console.WriteLine($"Counter: {counter}");
    counter++;
} while (counter <= 5);

5. Switch Case

Menu-Driven Program:

csharpCopy codeclass Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Menu:");
        Console.WriteLine("1. Add");
        Console.WriteLine("2. Subtract");
        Console.WriteLine("3. Multiply");
        Console.WriteLine("4. Divide");
        Console.WriteLine("Enter your choice:");

        int choice = int.Parse(Console.ReadLine());

        switch (choice)
        {
            case 1:
                Console.WriteLine("Addition Selected");
                break;
            case 2:
                Console.WriteLine("Subtraction Selected");
                break;
            case 3:
                Console.WriteLine("Multiplication Selected");
                break;
            case 4:
                Console.WriteLine("Division Selected");
                break;
            default:
                Console.WriteLine("Invalid Choice");
                break;
        }
    }
}

Summary

The .NET Framework simplifies application development by providing a unified runtime environment and pre-defined libraries. C# is a case-sensitive, powerful, and easy-to-use programming language within the .NET ecosystem. Start coding with simple examples and gradually move to advanced concepts like multithreading and database connectivity!