C# - Basic Syntax


In our last chapter we created our first console application with file named Program.cs. To understand the basic syntax of C#, we will use the same file code to print “Hello World!” as output.

using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}

Using Keyword

The using keyword used to include the namespaces in the program. We can have multiple using statement to include multiple namespaces.

using System;  // Importing the System namespace
using System.Collections.Generic;  // Importing the System.Collections.Generic namespace

class Program
{
    static void Main()
    {
        // You can now use types from both System and System.Collections.Generic namespaces
        // Example using List from System.Collections.Generic
        List numbers = new List();
        numbers.Add(1);
        numbers.Add(2);

        // Example using Console from System
        Console.WriteLine("List of Numbers:");
        foreach (var number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}

In C#, namespaces are containers that contain related classes, interfaces, and other types. They give unique names to classes, so the same class name can exist in multiple namespaces. 

Class Keyword

The Class keyword used to declaring a class in application.

What is class?

Class is a blueprint or template that is used to declare an object. A class combines variables and methods into single unit. For example: in a real life, a car is an object. Car has different properties like color, weight etc. It also has different methods like drive, brake.

using System;

// Define a class named 'Person'
class Person
{
    // Fields (attributes or variables)
    public string Name;
    public int Age;

    // Constructor (a special method called when an object is created)
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    // Method (behavior or function)
    public void DisplayInformation()
    {
        Console.WriteLine($"Name: {Name}, Age: {Age}");
    }
}

class Program
{
    static void Main()
    {
        // Create an instance (object) of the Person class
        Person person1 = new Person("John Doe", 25);

        // Access and modify the object's properties
        person1.Age = 26;

        // Call a method on the object
        person1.DisplayInformation();

        // Create another instance of the Person class
        Person person2 = new Person("Jane Doe", 30);
        person2.DisplayInformation();
    }
}

In this example, The Person class has two fields (Name and Age), a constructor (public Person(...)), and a method (DisplayInformation). The Program class contains the Main method, where instances of the Person class (person1 and person2) are created, and their properties and methods are accessed.

Comments

Comments are used to add description/explanation in code. Compiler ignore the commented lines. They are two way to add comment in code.

Multi Line starts with /* and ends with */ as shown below.

using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("Please enter your name:");
        string userName = Console.ReadLine();
        Console.WriteLine($"Hello, {userName}! Welcome to C#!");
    }
}

Single-line comments are added by "//" symbol as shown below.

using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("Please enter your name:");
        string userName = Console.ReadLine();
        Console.WriteLine($"Hello, {userName}! Welcome to C#!");
    }
}

C# Keywords

Keywords are predefined, reserved identifiers that have special meanings to the compiler. These keywords have specific meanings in the C# programming language and are reserved for their respective uses within the code.

Here's a list of reserved keywords in C#:

abstract as base
break byte case
catch char checked
class const continue
decimal default delegate
do double else
enum event explicit
extern false finally
fixed float for
foreach goto if
implicit in int
interface internal is
lock long namespace
new null object
operator out override
params private protected
public readonly ref
return sbyte sealed
short sizeof stackalloc
static string struct
switch this throw
true try typeof
uint ulong unchecked
unsafe ushort using
static var virtual
void volatile while