C Sharp Syntax

C# Syntax – Complete Beginner Guide
Understanding C# syntax is the first step toward becoming a confident C# developer.
Syntax refers to the rules that define how C# code must be written so the compiler can understand it.
If your syntax is incorrect, your program will not compile.
In this complete beginner guide, you’ll learn:
What C# syntax means
Basic structure of a C# program
Statements and semicolons
Curly braces
Case sensitivity
Variables and declarations
Comments
Whitespace rules
Naming conventions
Common beginner mistakes
Best practices
Let’s get started
What is Syntax in C#?
Syntax is the set of rules that define how code must be written.
Think of it like grammar in English.
If grammar is wrong → sentence is incorrect.
If syntax is wrong → program gives error.
Basic Structure of a C# Program
Here is a simple C# program:
1 2 3 4 5 6 7 8 9 | using System; class Program { static void Main() { Console.WriteLine("Hello, World!"); } } |
Let’s understand each part.
using Statement
This imports built-in classes from the .NET library.
Without this, Console.WriteLine() would not work.
Class Declaration
C# is an object-oriented language.
Every C# program must contain at least one class.
Main Method
1 | static void Main() |
Main is the entry point of the program.
Execution always starts here.
Statements End with Semicolon
In C#, every statement ends with a semicolon ;.
Example:
1 | Console.WriteLine("Hello"); |
If you forget the semicolon, you get a compile error.
Wrong:
1 | Console.WriteLine("Hello") |
Correct:
1 | Console.WriteLine("Hello"); |
Curly Braces { }
Curly braces define blocks of code.
Used in:
Classes
Methods
Loops
Conditions
Example:
1 2 3 4 | if (true) { Console.WriteLine("Yes"); } |
Always match opening and closing braces.
C# is Case-Sensitive
C# treats uppercase and lowercase differently.
Example:
1 | Console.WriteLine("Hello"); |
Wrong:
1 | console.writeline("Hello"); |
Keywords, variable names, and methods must match exact case.
Variables in C#
Variables store data.
Syntax:
1 | dataType variableName = value; |
Example:
1 2 3 | int age = 25; string name = "John"; double salary = 50000.50; |
Rules:
Must start with letter or underscore
Cannot start with number
Cannot use reserved keywords
Comments in C#
Comments explain code and are ignored by compiler.
Single-line comment:
1 | // This is a comment |
Multi-line comment:
1 2 3 4 | /* This is a multi-line comment */ |
Comments improve readability.
Whitespace and Formatting
C# ignores extra spaces and line breaks.
These are valid:
1 2 3 4 5 6 7 8 | int age=25; int age = 25; |
But good formatting improves readability:
1 | int age = 25; |
Use consistent indentation.
Naming Conventions in C#
C# follows specific naming rules.
Variables
camelCase:
1 2 | int userAge; string firstName; |
Classes
PascalCase:
1 | class StudentDetails |
Methods
PascalCase:
1 | void CalculateTotal() |
Following conventions makes code professional.
Basic Output Syntax
Printing text:
1 | Console.WriteLine("Hello"); |
Printing variables:
1 2 | int age = 25; Console.WriteLine(age); |
String concatenation:
1 | Console.WriteLine("Age: " + age); |
Basic Input Syntax
Taking input from user:
1 | string name = Console.ReadLine(); |
For numbers:
1 | int age = Convert.ToInt32(Console.ReadLine()); |
Important to convert string input to number type.
If Statement Syntax
1 2 3 4 | if (age >= 18) { Console.WriteLine("Adult"); } |
Syntax rules:
Condition inside parentheses
Block inside curly braces
Loop Syntax
Example of for loop:
1 2 3 4 | for (int i = 0; i < 5; i++) { Console.WriteLine(i); } |
Loop structure includes:
Initialization
Condition
Increment
Method Syntax
1 2 3 4 | void Greet() { Console.WriteLine("Hello"); } |
Calling method:
1 | Greet(); |
Methods help organize code.
Common Beginner Syntax Errors
- Missing semicolon
- Mismatched braces
- Incorrect capitalization
- Using reserved keywords as variable names
- Forgetting parentheses in method calls
Example error:
1 | Console.WriteLine "Hello"; |
Correct:
1 | Console.WriteLine("Hello"); |
Reserved Keywords in C#
Some words cannot be used as variable names.
Examples:
int
class
void
public
static
return
These are reserved by language.
Example: Complete Beginner Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; class Program { static void Main() { Console.Write("Enter your name: "); string name = Console.ReadLine(); Console.Write("Enter your age: "); int age = Convert.ToInt32(Console.ReadLine()); if (age >= 18) { Console.WriteLine("Hello " + name + ", you are an adult."); } else { Console.WriteLine("Hello " + name + ", you are a minor."); } } } |
This example demonstrates:
Input
Variables
Conditions
Output
Proper syntax
Best Practices for Writing Clean C# Syntax
- Always use proper indentation
- Use meaningful variable names
- Follow naming conventions
- Keep methods small
- Avoid unnecessary code
- Comment complex logic
Why Syntax Matters
Correct syntax:
Prevents compile errors
Improves readability
Makes debugging easier
Helps teamwork
Makes code professional
Good syntax habits from the beginning make you a better developer.
Frequently Asked Questions (FAQs)
1. What is syntax in C#?
Syntax refers to the rules that define how C# code must be written so the compiler can understand it.
2. Does every C# statement require a semicolon?
Yes. Most statements must end with a semicolon.
3. Is C# case-sensitive?
Yes. C# distinguishes between uppercase and lowercase letters.
4. What is the entry point of a C# program?
The Main method is the entry point.
5. Can I write C# without using classes?
No. C# programs must be inside a class.
Final Thoughts
C# syntax is:
Structured
Logical
Clean
Beginner-friendly
Mastering syntax allows you to:
Avoid errors
Write readable programs
Build real applications
Move toward advanced topics
Once you understand syntax clearly, learning C# becomes much easier.
