Here's a cheat sheet summarizing some of the key concepts, syntax, and features of the C# programming language:
csharp
// C# Language Cheat Sheet
// Comments: Single-line comment
/*
Multi-line
comment
*/
// Data Types:
int // Integer
float // Floating-point number
double // Double precision floating-point number
char // Character
bool // Boolean
string // String
decimal // Decimal number
DateTime // Date and time
var // Implicitly typed variable
// Variables:
int age = 25; // Variable declaration and initialization
float pi = 3.14;
char letter = 'A';
// Constants:
const int MAX_VALUE = 100; // Constant declaration
// Input and Output:
Console.WriteLine("Hello, World!"); // Output to console
int age = Convert.ToInt32(Console.ReadLine()); // Input from user
// Arithmetic Operators:
+, -, *, /, % // Addition, Subtraction, Multiplication, Division, Modulo
// Relational Operators:
==, !=, >, <, >=, <= // Equal to, Not equal to, Greater than, Less than, Greater than or equal to, Less than or equal to
// Logical Operators:
&& // Logical AND
|| // Logical OR
! // Logical NOT
// Control Flow:
if (condition) {
// Code executed if condition is true
}
else if (condition2) {
// Code executed if condition2 is true
}
else {
// Code executed if no previous conditions are true
}
switch (variable) {
case value1:
// Code executed if variable is equal to value1
break;
case value2:
// Code executed if variable is equal to value2
break;
default:
// Code executed if no previous cases match
}
while (condition) {
// Code executed repeatedly while condition is true
}
for (initialization; condition; increment/decrement) {
// Code executed repeatedly until condition is false
}
// Functions:
return_type function_name(parameter1, parameter2) {
// Code executed by the function
return value; // Optional return statement
}
// Classes and Objects:
class MyClass {
private int x;
public void SetX(int value) {
x = value;
}
public int GetX() {
return x;
}
}
MyClass obj = new MyClass(); // Object instantiation
obj.SetX(5); // Accessing member functions
int value = obj.GetX();
// Inheritance:
class DerivedClass : BaseClass {
// Derived class definition
}
// Interfaces:
interface IMyInterface {
void MyMethod();
}
class MyClass : IMyInterface {
public void MyMethod() {
// Method implementation
}
}
// Properties:
public int MyProperty { get; set; } // Automatic property
// Exception Handling:
try {
// Code that may throw an exception
}
catch (ExceptionType e) {
// Code to handle the exception
}
finally {
// Code that always executes, regardless of whether an exception is thrown
}
// Generics:
class MyClass<T> {
T MyMethod(T parameter) {
// Method implementation
return parameter;
}
}
// LINQ (Language-Integrated Query):
var result = from variable in collection
where condition
select variable;
// Collections (e.g., Lists, Arrays, Dictionaries, etc.):
List<int> numbers = new List<int>(); // List declaration
int[] array = new int[5]; // Array declaration
Dictionary<string, int> dict = new Dictionary<string, int>(); // Dictionary declaration
This cheat sheet covers some of the most commonly used features and syntax in the C# programming language. It serves as a handy reference for quick look-ups and reminders while programming in C#.
Comments
Post a Comment