Introduction to C# - Textnotes

Introduction to C#


Introduction to C#

1. What is C#?

C# (pronounced C-Sharp) is a modern, object-oriented programming language developed by Microsoft.

It is designed for building a variety of applications that run on the .NET framework.

Key Points:

  1. Strongly-typed, statically-typed language.
  2. Designed for simplicity and safety.
  3. Supports Object-Oriented Programming (OOP).
  4. Can be used for Web, Desktop, Mobile, Cloud, and Game development.

2. History & Evolution of C#

  1. 1999: Developed by Anders Hejlsberg at Microsoft.
  2. 2000: C# 1.0 released with .NET Framework 1.0.
  3. 2005: C# 2.0 introduced Generics, Partial Classes.
  4. 2007: C# 3.0 added LINQ and Lambda expressions.
  5. 2010: C# 4.0 included dynamic types and named arguments.
  6. 2012-2015: C# 5.0 & 6.0 added Async/Await and improved syntax.
  7. 2017-2021: C# 7.0 to 9.0 added Tuples, Pattern Matching, Records.
  8. 2022+: C# 10, 11, 12 (latest) improved performance, minimal APIs, global using, and enhanced pattern matching.

3. Features of C#

  1. Simple & Modern: Clean syntax, easy to learn for beginners.
  2. Object-Oriented: Supports OOP concepts like Classes, Objects, Inheritance, Polymorphism, Encapsulation, and Abstraction.
  3. Type-Safe: Prevents type errors at compile time.
  4. Managed Code: Runs on .NET CLR (Common Language Runtime).
  5. Interoperable: Can work with other languages on the .NET platform.
  6. Versatile: Can build Web, Desktop, Mobile, Cloud, and Games.
  7. Rich Library: Access to .NET libraries for networking, file handling, database, GUI, etc.

4. .NET Framework vs .NET Core vs .NET 5/6/7/8

Feature.NET Framework.NET Core.NET 5/6/7/8
Platform SupportWindows onlyCross-platform (Win, Mac, Linux)Cross-platform
Open SourcePartiallyOpen SourceOpen Source
PerformanceModerateHighHigh & optimized
App TypesDesktop, Web, Windows ServicesConsole, Web, CloudConsole, Web, Cloud, Mobile
Future SupportLegacySupported, but replaced by .NET 5+Latest & active support

Recommendation:

Use .NET 6 or .NET 8 for new projects because it is the latest LTS version and cross-platform.

5. Applications of C#

C# can be used to develop:

  1. Web Applications: ASP.NET Core, Blazor, REST APIs.
  2. Desktop Applications: Windows Forms, WPF.
  3. Mobile Applications: Xamarin, .NET MAUI (iOS & Android).
  4. Game Development: Unity engine.
  5. Cloud Applications: Azure services.
  6. IoT Applications: .NET IoT libraries.

6. Setting up Environment

To start programming in C#, you need an IDE:

Option 1: Visual Studio (Recommended for Windows)

  1. Download: Visual Studio
  2. Choose Community Edition (free) or Professional/Enterprise.
  3. During installation, select “.NET desktop development” workload.
  4. Install & launch Visual Studio.

Option 2: VS Code (Lightweight & Cross-platform)

  1. Download: Visual Studio Code
  2. Install .NET SDK (latest version) from here.
  3. Install C# extension in VS Code.
  4. Open terminal and verify installation:

dotnet --version

7. Your First Program: Hello World

Step 1: Create a Console Project


dotnet new console -n HelloWorldApp
cd HelloWorldApp

Step 2: Open Program.cs


using System;

namespace HelloWorldApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("Welcome to C# programming.");
}
}
}

Step 3: Run the Program


dotnet run

Expected Output:


Hello, World!
Welcome to C# programming.

Explanation:

  1. using System; → Includes the System namespace (contains Console class).
  2. namespace HelloWorldApp → Declares a namespace to organize code.
  3. class Program → Defines a class.
  4. static void Main(string[] args) → Entry point of the application.
  5. Console.WriteLine() → Prints text to the console.

Summary of Chapter 1:

  1. C# is a modern, versatile, object-oriented language.
  2. It runs on the .NET platform and supports cross-platform development.
  3. Applications include Web, Desktop, Mobile, Games, and Cloud.
  4. Environment setup can be done with Visual Studio or VS Code.
  5. Your first “Hello World” program introduces namespaces, classes, methods, and console output.