techmore.in

Csharp Introduction

C# (pronounced "C-sharp") is a modern, object-oriented programming language developed by Microsoft. It is part of the .NET framework and is designed for building a wide range of applications, from web services to desktop applications. Here's a broad introduction to C#:

Key Features

  1. Object-Oriented Programming: C# supports encapsulation, inheritance, and polymorphism, making it a powerful language for building complex and reusable software.

  2. Type Safety: C# is a statically typed language, which helps catch errors at compile time and ensures that variables are used in a type-safe manner.

  3. Automatic Memory Management: C# includes garbage collection to manage memory automatically, which helps reduce memory leaks and other related issues.

  4. Rich Standard Library: C# comes with a comprehensive standard library that provides a wide range of functionality, including collections, file I/O, and network communications.

  5. Interoperability: C# can interoperate with other .NET languages, such as VB.NET and F#, and can also call native APIs through P/Invoke.

  6. Modern Language Features: C# includes features such as properties, events, delegates, and asynchronous programming with async/await.

Basic Syntax and Concepts

  1. Hello World Example:

    csharp
    using System; class Program { static void Main() { Console.WriteLine("Hello, World!"); } }
    • using System; imports the System namespace which contains fundamental classes.
    • class Program defines a class named Program.
    • static void Main() is the entry point of the application.
  2. Variables and Data Types:

    csharp
    int age = 30; double height = 5.9; string name = "John"; bool isStudent = true;

    C# provides various data types, including int, double, string, and bool.

  3. Control Flow Statements:

    • If-Else:

      csharp
      int number = 10; if (number > 0) { Console.WriteLine("Positive"); } else { Console.WriteLine("Non-positive"); }
    • For Loop:

      csharp
      for (int i = 0; i < 5; i++) { Console.WriteLine(i); }
    • While Loop:

      csharp
      int count = 0; while (count < 5) { Console.WriteLine(count); count++; }
  4. Functions and Methods:

    csharp
    int Add(int a, int b) { return a + b; } // Calling the method int result = Add(5, 3);

    Methods can have parameters and return values.

  5. Classes and Objects:

    csharp
    class Person { public string Name { get; set; } public int Age { get; set; } public void Greet() { Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old."); } } // Using the class Person person = new Person(); person.Name = "Alice"; person.Age = 25; person.Greet();
    • public specifies the access level.
    • get and set are accessors for properties.
    • void means the method does not return a value.
  6. Inheritance and Polymorphism:

    csharp
    class Animal { public virtual void MakeSound() { Console.WriteLine("Some sound"); } } class Dog : Animal { public override void MakeSound() { Console.WriteLine("Bark"); } } // Using inheritance Animal myDog = new Dog(); myDog.MakeSound(); // Outputs: Bark
    • virtual allows a method to be overridden in derived classes.
    • override provides a new implementation for the method.
  7. Exception Handling:

    csharp
    try { int result = 10 / 0; // This will cause a divide by zero exception } catch (DivideByZeroException ex) { Console.WriteLine("Cannot divide by zero."); } finally { Console.WriteLine("This block always executes."); }
    • try block contains code that might throw an exception.
    • catch block handles the exception.
    • finally block is optional and executes regardless of whether an exception is thrown.

Development Tools

  • Visual Studio: The primary IDE for C# development, providing a rich set of tools for debugging, testing, and building applications.
  • Visual Studio Code: A lightweight editor that can be configured for C# development with extensions.

Getting Started

To start developing in C#, you’ll need to:

  1. Install the .NET SDK: Includes the C# compiler and necessary tools.
  2. Choose an IDE: Install Visual Studio or Visual Studio Code.
  3. Write Your Code: Create C# files and write your programs.
  4. Build and Run: Use the .NET CLI or your IDE’s build tools to compile and run your application.

C# is a versatile language that can be used for various types of applications, including web, desktop, mobile, and cloud services.