Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to C#: Basic Syntax and Structure

1. Overview

C# is a modern, object-oriented programming language developed by Microsoft. It is designed to be simple, modern, and versatile, suitable for various applications ranging from web services to games. In this tutorial, we will explore the basic syntax and structure of a C# program.

2. Basic Structure of a C# Program

A basic C# program consists of the following elements:

  • Namespaces
  • Classes
  • Main Method
using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

In this example:

  • using System; - This is a using directive that includes the System namespace, which provides fundamental classes and base classes.
  • namespace HelloWorld - Defines a namespace called HelloWorld.
  • class Program - Defines a class called Program within the HelloWorld namespace.
  • static void Main(string[] args) - The Main method is the entry point of a C# program. It is where the program begins execution.
  • Console.WriteLine("Hello, World!"); - Outputs the string "Hello, World!" to the console.

3. Data Types

C# is a strongly-typed language, meaning each variable must have a type. Here are some common data types:

  • int - Integer type
  • float - Floating point type
  • double - Double precision floating point type
  • char - Character type
  • string - String type
  • bool - Boolean type (true or false)
int myInt = 5;
float myFloat = 5.75f;
double myDouble = 19.99;
char myChar = 'D';
string myString = "Hello";
bool myBool = true;

4. Variables

Variables are used to store data. In C#, you need to declare a variable before using it. The declaration includes the type and the variable name.

int age = 25;
string name = "John";

5. Operators

C# supports various operators for performing operations on variables. These include:

  • Arithmetic Operators - +, -, *, /, %
  • Comparison Operators - ==, !=, >, <, >=, <=
  • Logical Operators - &&, ||, !
int a = 10;
int b = 20;
Console.WriteLine(a + b); // 30
Console.WriteLine(a == b); // False
Console.WriteLine(a < b && b > 15); // True

6. Control Structures

Control structures allow you to control the flow of execution of the program. Common control structures include:

  • if-else
  • switch
  • for
  • while
  • do-while
int num = 10;

if (num > 0)
{
    Console.WriteLine("Positive number");
}
else
{
    Console.WriteLine("Negative number or zero");
}

switch (num)
{
    case 10:
        Console.WriteLine("Ten");
        break;
    default:
        Console.WriteLine("Not ten");
        break;
}

for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}

int j = 0;
while (j < 5)
{
    Console.WriteLine(j);
    j++;
}

int k = 0;
do
{
    Console.WriteLine(k);
    k++;
} while (k < 5);

7. Methods

Methods are blocks of code that perform a specific task. They help in modularizing and reusing code. A method is defined within a class and has a name, return type, and parameters.

class Program
{
    static void Main(string[] args)
    {
        int result = Add(5, 3);
        Console.WriteLine(result); // 8
    }

    static int Add(int a, int b)
    {
        return a + b;
    }
}

8. Conclusion

In this tutorial, we covered the basic syntax and structure of a C# program. We looked at the basic elements such as namespaces, classes, the Main method, data types, variables, operators, control structures, and methods. This foundational knowledge will help you as you continue to learn and explore C# programming.