C# Syntax Structure
Learn the basics of C# syntax, including code blocks, comments, and variable naming rules, with clear explanations and practical examples.
In C#, syntax refers to the set of rules that must be followed for the code
to be correctly understood by the computer. These rules cover a wide range,
from how blank lines are handled, the use of semicolons (;),
the writing of string and char literals, to class and method structure.
Mastering syntax is the fundamental step to writing error-free and readable code.
Blank Lines
The C# compiler completely ignores blank lines. So, adding blank lines in your code is only for readability; it does not affect the execution of the program.
Semicolon (;)
Most C# statements end with a semicolon ;.
For example, variable assignments or method calls.
int age = 25;
Console.WriteLine(age);
However, control structures such as if, for, while,
and switch do not end with a semicolon;
instead, the following code blocks are defined with curly braces { }.
Strings and Chars
In C#, string values are enclosed in double quotes " ",
while char (a single character) values are enclosed in single quotes ' '.
string message = "Sample text";
char letter = 'A';
Namespace
A namespace organizes classes and types in projects into logical groups. It prevents naming conflicts and makes the code more structured.
namespace SampleApp
{
class Program
{
static void Main()
{
Console.WriteLine("Sample output!");
}
}
}
Class and Method
At the core, C# programs contain one or more classes.
Inside classes, there are methods. To access a class’s method or property,
the dot operator (.) is used.
string text = "CSharp";
Console.WriteLine(text.ToUpper()); // The ToUpper method converts the string to uppercase
Methods are always called with parentheses ().
You can pass data (arguments) inside the parentheses.
Code Blocks and Scope
Code blocks are defined with curly braces { }.
Variables defined inside a block are only accessible within that block.
int x = 10;
if (x > 5)
{
int y = 20;
Console.WriteLine(y); // Valid
}
Console.WriteLine(x); // Valid
// Console.WriteLine(y); // ERROR: y is only defined inside the if block
Variable Naming Rules
- Must start with a letter or an underscore (
_). - Cannot contain spaces or hyphens (
-). - Cannot start with a numeric character.
- C# is case-sensitive (
nameandNameare different). - camelCase is generally preferred:
studentName.
int studentAge = 20; // Valid
int _score = 100; // Valid
// int 2number = 5; // Invalid
// int student-name = 5; // Invalid
Other Syntax Rules
- Every program must have at least one
Mainmethod. usingstatements must be at the top of the file.- Case sensitivity applies everywhere.
- Every opened curly brace must be closed.
TL;DR
- Blank lines are ignored, they are for readability only.
- A semicolon
;is placed at the end of statements. " "is for strings,' 'is for chars.- Namespaces organize code into logical groups.
- Classes contain methods, and methods are called with parentheses
(). - Variables defined inside a block cannot be accessed outside of it.
- Variable names must follow rules and are case-sensitive.
Sample Program
The following example is a simple console application where you can see C# syntax rules together. It combines variable declaration, an if block, string operations, and method calls.
using System;
namespace SyntaxExample
{
class Program
{
static void Main()
{
// Variable declarations
string word = "Hello";
int number = 12;
// Code block and condition
if (number % 2 == 0)
{
Console.WriteLine(number + " is even.");
}
else
{
Console.WriteLine(number + " is odd.");
}
// String method call (Length)
Console.WriteLine("Word length: " + word.Length);
// Mathematical operation (Math library)
double squareRoot = Math.Sqrt(number);
Console.WriteLine("Square root of " + number + ": " + squareRoot);
}
}
}
// Output:
12 is even.
Word length: 5
Square root of 12: 3.46410161513775