C# Basics

C# is a versatile programming language developed by Microsoft, widely used for building a variety of applications, from web to desktop and mobile. This post covers the basics of C#, including syntax, data types, and object-oriented programming concepts. Dotnet is a framework that supports C# and provides a rich set of libraries and tools for developers.

Syntax

C# syntax is similar to other C-style languages like Java and C++. Here are some basic elements:

  • Variables: Declared with a type followed by the variable name.

    1
    2
    
    int age = 30;
    string name = "Aum";
    
  • Data Types: C# has several built-in data types, including:

    • int: Represents integer values.
    • double: Represents floating-point numbers.
    • bool: Represents boolean values (true or false).
    • string: Represents a sequence of characters.
  • Control Structures: C# supports various control structures, such as:

    • If statements: Used for conditional execution.
    • For loops: Used for iterating over a range of values.
    • Switch statements: Used for selecting one of many code blocks to execute.
  • Functions: Defined using the void keyword for functions that do not return a value, or with a specific return type.

    1
    2
    3
    
    void Greet(string name) {
        Console.WriteLine($"Hello, {name}!");
    }
    
  • Classes and Objects: C# is an object-oriented language, allowing you to define classes and create objects.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    
    class Person {
        public string Name { get; set; }
        public int Age { get; set; }
    
        public void Introduce() {
            Console.WriteLine($"My name is {Name} and I am {Age} years old.");
        }
    }
    
    Person person = new Person();
    person.Name = "Aum";
    person.Age = 30;
    person.Introduce();
    

Preprocessor Directives

C# supports preprocessor directives, which are commands that are processed before the compilation of the code

  • Using Directives: Used to include namespaces in your code.
    1
    
    using System;
    
  • Conditional Compilation: Allows you to include or exclude code based on certain conditions.
    1
    2
    3
    
    #if DEBUG
    Console.WriteLine("Debug mode is enabled.");
    #endif
    

Basic IO statements

C# provides several methods for input and output operations:

  • Console Input/Output: The Console class provides methods for reading from and writing to the console.

    1
    2
    3
    
    Console.WriteLine("Enter your name:");
    string name = Console.ReadLine();
    Console.WriteLine($"Hello, {name}!");
    
    • Taking in a number: You can read a number from the console and convert it to an integer.
    1
    2
    3
    4
    5
    6
    7
    8
    
    Console.WriteLine("Enter your age:");
    string input = Console.ReadLine();
    int age;
    if (int.TryParse(input, out age)) {
        Console.WriteLine($"You are {age} years old.");
    } else {
        Console.WriteLine("Invalid input. Please enter a valid number.");
    }