C# Introduction

Understanding the foundation of C# programming

๐Ÿ’Ž What is C#?

C# is a modern, object-oriented programming language created by Microsoft. It runs on the .NET framework and is used for building Windows applications, games, web services, and more.


// Simple C# program
using System;

class Welcome
{
    static void Main()
    {
        Console.WriteLine("Welcome to C#!");
    }
}
                                    

Output:

Welcome to C#!

Key C# Features

๐ŸŽฏ

Object-Oriented

Everything is an object with properties and methods

class Car
{
    public string Brand;
}
๐Ÿ”’

Type-Safe

Catches errors at compile time, not runtime

int age = 25;
string name = "John";
โšก

Fast & Efficient

Compiled code runs quickly and efficiently

for(int i = 0; i < 1000; i++)
{
    // Fast loop
}
๐Ÿ› ๏ธ

Rich Libraries

Thousands of built-in classes and methods

using System;
using System.Linq;
using System.IO;

๐Ÿ”น C# Program Structure

Every C# program is organized within namespaces, classes, and methods, starting execution at the Main method. A typical structure includes: using System;, namespace MyApp { class Program { static void Main() { Console.WriteLine("Hello"); } } }. The Main method is the entry point. This structured approach ensures code modularity, reusability, and clear separation of concerns, which is essential for building scalable applications.

using System;  // Import namespace

namespace MyApp  // Define namespace
{
    class Program  // Define class
    {
        static void Main(string[] args)  // Main method
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Output:

Hello World!

๐Ÿ”น Variables and Data Types

C# is statically typed, meaning every variable must declare its data type before use. Common types include int for integers, string for text, double for decimals, and bool for true/false values. For example: int age = 25; string name = "Alice";. Type safety prevents errors by catching mismatches at compile time. Understanding data types is fundamental for efficient memory usage and robust application development in C#.

using System;

class DataTypes
{
    static void Main()
    {
        // Integer
        int age = 25;
        
        // Decimal
        double price = 19.99;
        
        // Text
        string name = "Alice";
        
        // True/False
        bool isStudent = true;
        
        Console.WriteLine("Name: " + name);
        Console.WriteLine("Age: " + age);
        Console.WriteLine("Price: $" + price);
        Console.WriteLine("Student: " + isStudent);
    }
}

Output:

Name: Alice
Age: 25
Price: $19.99
Student: True

๐Ÿ”น Basic Operations

C# supports arithmetic, concatenation, and comparison operators to perform calculations and evaluations. Arithmetic includes +, -, *, /. String concatenation uses +, like "Hello" + "World". Comparisons (>, ==, <=) return Boolean results. For example: int sum = 10 + 5; or bool isGreater = 10 > 5;. These operations form the core logic for data manipulation and decision-making in programs.

using System;

class Operations
{
    static void Main()
    {
        // Math operations
        int sum = 10 + 5;
        int product = 10 * 5;
        
        // String concatenation
        string greeting = "Hello" + " " + "World";
        
        // Comparison
        bool isGreater = 10 > 5;
        
        Console.WriteLine("Sum: " + sum);
        Console.WriteLine("Product: " + product);
        Console.WriteLine(greeting);
        Console.WriteLine("10 > 5: " + isGreater);
    }
}

Output:

Sum: 15
Product: 50
Hello World
10 > 5: True

๐Ÿ”น Why Choose C#?

C# is a versatile, modern language ideal for desktop, web, mobile, and game development, particularly within the .NET ecosystem. Its strong typing reduces runtime errors, while features like LINQ, async/await, and garbage collection boost productivity. Backed by Microsoft with extensive documentation and community support, C# offers excellent performance, scalability, and cross-platform capabilities via .NET Core. It's a top choice for building enterprise applications, Unity games, and cloud services.

For Beginners:

  • Clear Syntax: Easy to read and understand
  • Great Documentation: Microsoft provides excellent learning resources
  • Visual Studio: Powerful free IDE with IntelliSense
  • Strong Community: Millions of developers to help you

For Professionals:

  • Enterprise Ready: Used by Fortune 500 companies
  • Cross-Platform: Run on Windows, Mac, and Linux with .NET Core
  • High Performance: Optimized for speed and efficiency
  • Job Opportunities: High demand in the job market

๐Ÿ”น C# vs Other Languages

Understanding how C# compares to languages like Java, Python, and JavaScript helps in selecting the right tool for a project. C# and Java share similar syntax, but C# offers more modern features like properties and events. Compared to Python, C# is faster and type-safe but requires more verbose code. Against JavaScript, C# is compiled and server-side, while JS is interpreted and client-side. Each language excels in different domainsโ€”C# in robust, scalable systems.

C# vs Java:

C# and Java are both object-oriented, C#-style languages with similar syntax, but C# includes more modern language features out-of-the-box. C# offers properties, indexers, delegates, and events as first-class citizens, while Java uses getter/setter methods. C# has LINQ for data querying and async/await for asynchronous programming built into the language. Both run on virtual machines (CLR vs. JVM) and are used for enterprise applications, but C# integrates more seamlessly with the Microsoft ecosystem and .NET libraries.

C# vs Python:

C# is statically typed and compiled, offering performance benefits, while Python is dynamically typed and interpreted, favoring rapid development. C# requires explicit type declarations, catching errors early during compilation. Python uses duck typing, allowing flexible but potentially error-prone code. C# excels in performance-critical applications like game development (via Unity) and Windows services. Python is preferred for data science, scripting, and machine learning due to its concise syntax and extensive libraries like NumPy and TensorFlow.

C# vs JavaScript:

C# is a compiled, strongly-typed language primarily for server-side and desktop applications, whereas JavaScript is an interpreted language for web client-side scripting. C# runs on the .NET runtime, ensuring type safety and performance. JavaScript executes in browsers, enabling dynamic web content. With Node.js, JavaScript can also run server-side. C# is used for Windows apps, backend APIs, and games; JavaScript powers interactive websites, frontend frameworks (React, Angular), and full-stack JavaScript applications.

๐Ÿง  Test Your Knowledge

What is the entry point of a C# program?