C# Syntax, Code Examples, and Usage Guide
Category Syntax / Code Example Use / Purpose
Basic Structure using System; Imports namespaces and defines
namespace MyApp { } code organization.
Class Definition class Person { Defines an object blueprint with
public string Name; properties and methods.
}
Method void Greet() { Defines reusable blocks of code.
[Link]("Hello");
}
Variables int age = 25; Stores integer data.
Constants const double PI = 3.14; Declares a constant that cannot
change.
Conditional if (x > 0) { Executes code when condition is
[Link]("Positive"); true.
}
Loop for (int i = 0; i < 5; i++) { Repeats a block of code.
[Link](i);
}
Array int[] nums = {1, 2, 3}; Stores multiple values of same
type.
List List list = new List(); Dynamic collection of elements.
Inheritance class Dog : Animal { } Inherits behavior from another
class.
Interface interface IShape { Defines contract for
void Draw(); implementation.
}
Exception Handling try { Handles runtime errors.
// code
} catch (Exception ex) {
[Link]([Link]);
}
Property public int Age { get; set; } Encapsulates field access.
LINQ var result = from x in list Query collections using LINQ
where x > 0 syntax.
select x;
Async async Task DoWork() { Asynchronous programming
await [Link](1000); pattern.
}
Casting int y = (int)x; Converts one data type to
another.
Category Syntax / Code Example Use / Purpose
Delegate delegate void MyDelegate(); Defines reference type for
methods.
Event event Action OnClick; Implements event handling
system.
Lambda var square = x => x * x; Anonymous inline function.