C# (pronounced "C-Sharp") is a modern, object-oriented programming language developed by Microsoft. It is widely used for building Windows applications, web applications, mobile apps, games, cloud services, and much more. Understanding the syntax and structure of C# is essential for writing efficient and maintainable code. In this guide, we'll explore the foundational syntax and structure of C# with detailed examples and best practices.
Syntax refers to the set of rules that define how a C# program must be written. The structure defines how these elements are organized in a project or a program file.
At a high level, a basic C# program consists of the following:
using System;
namespace HelloWorldApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
}
}
}
This is the simplest structure of a C# program. Letβs break it down.
Using directives allow you to use classes and functions defined in other namespaces.
using System;
This line gives access to classes like Console, Math, etc., under the System namespace.
namespace HelloWorldApp
A namespace is a container for classes and other namespaces. It helps organize code and prevents name conflicts.
class Program
Classes are the building blocks of any C# application. Everything in C# is wrapped inside a class or struct.
static void Main(string[] args)
This is the entry point of every C# console application. Execution starts from here.
Console.WriteLine("Hello, world!");
This prints the string to the console.
Keywords are reserved words that have special meaning in C#. They cannot be used as variable names. Examples include:
Comments are ignored by the compiler and are used to make code more readable.
// This is a single-line comment
/* This is a
multi-line comment */
C# is a statically-typed language, meaning every variable must have a declared type.
int age = 25;
string name = "Alice";
bool isStudent = true;
int number = 10;
double price = 99.99;
string title = "Learn C#";
Constants are read-only values assigned at the time of declaration.
const double Pi = 3.14159;
if (age > 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are a minor.");
}
switch (day)
{
case "Monday":
Console.WriteLine("Start of the week");
break;
case "Friday":
Console.WriteLine("End of the week");
break;
default:
Console.WriteLine("Midweek");
break;
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i < 5);
Methods are reusable blocks of code that perform a specific task.
public static int Add(int a, int b)
{
return a + b;
}
int result = Add(5, 3);
Console.WriteLine(result);
A class is a blueprint. An object is an instance of a class.
public class Person
{
public string Name;
public int Age;
public void Introduce()
{
Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old.");
}
}
Person p = new Person();
p.Name = "John";
p.Age = 30;
p.Introduce();
public class Calculator
{
public static int Multiply(int a, int b)
{
return a * b;
}
public int Subtract(int a, int b)
{
return a - b;
}
}
int result1 = Calculator.Multiply(4, 5); // Static
Calculator calc = new Calculator();
int result2 = calc.Subtract(9, 4); // Instance
Namespaces group related classes together.
namespace MyApp.Utilities
{
public class Logger
{
public void Log(string message)
{
Console.WriteLine("Log: " + message);
}
}
}
try
{
int x = int.Parse("abc");
}
catch (FormatException ex)
{
Console.WriteLine("Invalid format.");
}
finally
{
Console.WriteLine("Execution finished.");
}
Understanding the syntax and structure of C# is fundamental to becoming a proficient developer. C# offers a clean, powerful, and modern syntax that supports object-oriented programming principles. From variables, control flow, and loops to methods, classes, and exception handling, each feature helps you build robust and maintainable applications. As you dive deeper into C#, youβll explore advanced concepts like LINQ, async programming, delegates, events, and design patterns.
Whether you are building console applications, web APIs, desktop software, or mobile apps, mastering the syntax and structure of C# is your first step toward success in the .NET ecosystem.
Copyrights © 2024 letsupdateskills All rights reserved