C# Syntax
Basic Syntax
C# syntax is similar to other C-style languages like C, C++, and Java. If you know any of these languages, C# syntax will look familiar.
Example:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
// Output text to the console
Console.WriteLine("Hello World!");
// Ask for user input
Console.Write("What is your name? ");
// Read user input from the console
string name = Console.ReadLine();
// Output using the input
Console.WriteLine($"Hello, {name}!");
}
}
}
Let's break down this code:
using System;- This line imports the System namespace which contains the Console classnamespace HelloWorld { ... }- Groups your code into a namespace called "HelloWorld"class Program { ... }- Defines a class called "Program"static void Main(string[] args) { ... }- The Main method is the entry point of your programConsole.WriteLine("Hello World!");- Prints text to the console with a new line at the endConsole.Write("What is your name? ");- Prints text without a new linestring name = Console.ReadLine();- Reads a line of input from the user and stores it in a variableConsole.WriteLine($"Hello, {name}!");- Prints text with the user's input using string interpolation
C# Statement Structure
In C#, statements end with a semicolon (;) and code blocks are enclosed in curly braces { }.
// Single statements end with semicolons
int x = 10;
Console.WriteLine(x);
// Code blocks use curly braces
if (x > 5)
{
Console.WriteLine("x is greater than 5");
x = 5; // This is also within the block
}
// Single statement blocks can omit braces (not recommended)
if (x == 5)
Console.WriteLine("x equals 5"); // Only this line is in the if block
Important notes:
- Every statement must end with a semicolon
- Code blocks define the scope of variables declared within them
- While you can omit braces for single-statement blocks, it's generally not recommended for readability and to prevent bugs
C# Case Sensitivity
C# is a case-sensitive language. This means that myVariable and MyVariable are considered different identifiers.
// These are three different variables
int value = 10;
int Value = 20;
int VALUE = 30;
// This works
Console.WriteLine(value); // Outputs: 10
Console.WriteLine(Value); // Outputs: 20
Console.WriteLine(VALUE); // Outputs: 30
// This would cause an error
// Console.WriteLine(vAlUe); // Variable 'vAlUe' doesn't exist
C# Comments
Comments are used to explain code and are ignored by the compiler.
// This is a single-line comment
/* This is a
multi-line comment */
/// <summary>
/// XML documentation comments are used to generate documentation
/// </summary>
public void DocumentedMethod()
{
// Method implementation
}
C# supports three types of comments:
// Single-line comments- Everything after // on that line is a comment/* Multi-line comments */- Everything between /* and */ is a comment, even across multiple lines/// XML documentation comments- Special comments that can be used to generate documentation
C# Coding Conventions
Following are some common C# coding conventions:
- PascalCase: Used for class names, method names, and public members (e.g.,
ClassName,MethodName) - camelCase: Used for local variables and parameters (e.g.,
localVariable,parameterName) - _camelCase: Often used for private fields (e.g.,
_privateField) - ALL_CAPS: Sometimes used for constants (e.g.,
MAX_SIZE) - Braces on their own line (typically) for methods and classes
- Use four spaces for indentation (or a tab set to four spaces)