Skip to content

Csharp

cotrTypesList

C# List Type

List<${1:type}>

cotrTypesNumAlt

C# Float Type

float

cotrVarNullable

C# Create Nullable Variable

${1:Type}? ${2:myVar} = $3;

cotrComment

C# Comment

// ${1:Your comment here}

cotrTypeConvert

C# Type Conversion

// C# Type Conversion:
// Implicit conversions (compiler performs automatically):
// - Smaller numeric types to larger numeric types (e.g., int to double).
// Explicit conversions (using casts):
// - (Type)variable // C-style cast
// - variable as Type // Safe cast (returns null if conversion fails)
// - Convert.ToType(variable) // Conversion methods in the Convert class
// Note:
// - Be cautious with explicit conversions, as they can lead to data loss or errors if the conversion is not valid.

cotrTypesDynamic

C# Dynamic Type

dynamic

cotrVarDouble

C# Create Double Variable

double ${1:myDouble} = ${2:0.0};

cotrVarMap

C# Create Dictionary Variable

Dictionary<${1:Key}, ${2:Value}> ${3:myDict} = new Dictionary<${1:Key}, ${2:Value}>();

cotrWhileLoop

C# While Loop

while (${1:condition}) {
${2:// Your code here}
}

cotrTypesNum

C# Double Type

double

cotrTernary

C# Ternary Operator

${1:condition} ? ${2:trueValue} : ${3:falseValue}

cotrTypesBoolFalse

C# Boolean False

false

cotrVarStatic

C# Static Variable

static ${2:Type} ${3:myStaticVar} = ${4:value};
// Access the static variable
// MyClass.${3:myStaticVar}

cotrFuncArgsNamed

C# Function Named Args

// Note: C# does not have named arguments in function definitions.
// You can use named parameters in method calls.
public ${1:void} ${2:MyFunction}(${3:type1} ${4:arg1}, ${5:type2} ${6:arg2})
{
${7:// Your code here}
}

cotrTryCatch

C# Try Catch

try {
${1:// Your code here}
} catch (${2:Exception} ${3:e}) {
${4:// Your code here}
}

cotrEqual

C# Equal To

==

cotrConcat

C# Concatenate Strings

string result = string.Concat(${1:First string}, ${2:Second string});

cotrThrow

C# Throw Exception

throw new ${1:Exception}(${2:'Your message here'});

cotrTypesDate

C# Date Type

DateTime

cotrInterpolate

C# Interpolate String

$"${1:Your string here}"

cotrVarSyntax

C# Variable Declaration Syntax

// C# Variable Declaration Syntax:
// - var: (Scope: Block)
// - Type is automatically inferred by the compiler.
// - Preferred for most variable declarations.
// - type: (Scope: Block)
// - Explicitly specify the variable's type.
// - Use when var deduction is not desired or not possible.
// - const: (Scope: Block)
// - Cannot be reassigned or redeclared.
// - Use for values that should remain constant.
// Note:
// - C# does not have a direct equivalent to 'let'.
// - Use 'var' for most variable declarations.
// - Use 'const' for values that should not change.

cotrVarString

C# Create String Variable

string ${1:myString} = ${2:'Your string here'};

cotrSwitch

C# Switch Statement

switch (${1:variable}) {
case ${2:value1}:
${3:// Your code here}
break;
case ${4:value2}:
${5:// Your code here}
break;
default:
${6:// Your code here}
}

cotrFuncAnon

C# Anonymous Function

(${1:parameters}) => {
${2:// Your code here}
};

cotrTypesNull

C# Null Type

null

cotrTypesMap

C# Map Type

Dictionary<${1:keyType}, ${2:valueType}>

cotrVar

C# Create Variable

${1:Type} ${2:myVar} = ${3:value};

cotrVarDate

C# Create Date Variable

DateTime ${1:myDate} = new DateTime(${2:year}, ${3:month}, ${4:day});

cotrPrint

C# Print

Console.WriteLine(${1:'Your message here'});

cotrTypesBool

C# Boolean Type

bool

cotrVarList

C# Create List Variable

List<${1:Type}> ${2:myList} = new List<${1:Type}>();

cotrDoWhileLoop

C# Do While Loop

do {
${1:// Your code here}
} while (${2:condition});

cotrCommentMulti

C# Multi-Line Comment

/*
* ${1:Your comment here}
*/

cotrTypeCheck

C# Type Check

${1:variable}.GetType()

cotrNotEqual

C# Not Equal To

!=

cotrNow

C# Date Now

DateTime.Now

cotrGenMap

C# Generate Map

var ${1:myMap} = Enumerable.Range(0, ${2:length}).ToDictionary(i => i, i => ${3:'item'} + i.ToString());

cotrStructure

C# Project Structure (High-Level)

// Recommended High-Level C# Project Structure:
// - src/
// - Contains the source code for the application.
// - Organize by feature or purpose within separate directories or projects,
// such as:
// - Services/
// - Controllers/
// - Models/
// - Views/ (for web applications)
// - lib/
// - Contains external libraries or dependencies, typically managed by NuGet.
// - tests/
// - Contains unit tests, integration tests, etc.
// - Organize tests to reflect the structure of the src/ directory.
// - docs/
// - Documentation related to the project.
// - tools/
// - Contains scripts and other tools for the project (e.g., build scripts).
// - build/
// - Contains build output.
// Note:
// - Projects in C# are often structured as solutions containing multiple
// projects (class libraries, web applications, etc.).
// - This structure can vary depending on the project type and developer preferences.

cotrTypeCompare

C# Type Comparison

// Check if two variables have the same type:
if (${1:variable1}.GetType() == ${2:variable2}.GetType()) {
// Your code here
}

cotrTypes

C# Types

$BLOCK_COMMENT_START
C# is a statically typed language.
Types in C# include:
- int: Integer
- float: Single-precision floating-point number
- double: Double-precision floating-point number
- char: Character
- bool: Boolean
- string: String
- int[]: Array of integers
- List<int>: List of integers
- Dictionary<K, V>: Dictionary with key type K and value type V
- struct MyStruct: Custom value type with named fields
- enum MyEnum: Enumeration type
- void: Type representing the absence of a value
- object: Base type of all other types
- dynamic: Type for dynamic binding
- var: Type inferred by the compiler
- Nullable<T>: Type representing a value of type T or null
- Tuple<T1, T2, ...>: Tuple with elements of different types
- Action, Func<T>: Delegate types for methods
Read more here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types
$BLOCK_COMMENT_END

cotrTypesInt

C# Integer Type

int

cotrConst

C# Create Constant

const ${1:Type} ${2:myConst} = ${3:value};

cotrIf

C# If Statement

if (${1:condition}) {
${2:// Your code here}
}

cotrOperatorsBool

C# Boolean Operators

// C# Boolean Operators
// Logical AND: &&
// Logical OR: ||
// Logical NOT: !
// Equality: ==
// Inequality: !=
// Greater than: >
// Less than: <
// Greater than or equal to: >=
// Less than or equal to: <=

cotrFuncArgs

C# Function Args

// In C#, functions can have arguments with default values.
public ${1:void} ${2:MyFunction}(${3:type1} ${4:arg1}, ${5:type2} ${6:arg2} = ${7:defaultValue})
{
${8:// Your code here}
}

cotrEnum

C# Enum

enum ${1:MyEnum} {
${2:value1},
${3:value2},
// Add more values here
}

cotrEntry

C# Entry Point

// C# Entry Point
// To run this program, use: `dotnet run` within the project directory
class Program
{
static void Main(string[] args)
{
// Your code here
}
}

cotrVarBool

C# Create Boolean Variable

bool ${1:myBool} = ${2:true};

cotrVarStringMulti

C# Create Multi-Line String Variable

string ${1:myString} = @"""
${2:Line 1}
${3:Line 2}
${4:Line 3}
""";

cotrOperators

C# Mathematical Operators

// C# Mathematical Operators
// Addition: +
// Subtraction: -
// Multiplication: *
// Exponentiation: **
// Division: /
// Modulus: %
// Increment: ++
// Decrement: --
// Assignment: =
// Addition assignment: +=
// Subtraction assignment: -=
// Multiplication assignment: *=
// Division assignment: /=
// Modulus assignment: %=

cotrTypesString

C# String Type

string

cotrVarInt

C# Create Integer Variable

int ${1:myInt} = ${2:0};

cotrGenList

C# Generate List

var ${1:myList} = Enumerable.Range(0, ${2:length}).Select(i => ${3:'item'} + i.ToString()).ToList();

cotrForLoop

C# For Loop

for (int ${1:i} = 0; $1 < ${2:10}; $1++) {
// Your code here
}

cotrForEachLoop

C# For Each Loop

foreach (${1:Type} ${2:item} in ${3:iterable}) {
${4:// Your code here}
}

cotrFuncArrow

C# Arrow Function

Func<${2:parameters}, ${1:returnType}> ${3:myFunction} = (${4:parameters}) => {
${5:// Your code here}
};

cotrInfo

C# Info

// Typing: Statically typed
// Paradigm: Multi-paradigm: structured, imperative, object-oriented, event-driven, task-driven, functional, generic, reflective, concurrent
// Compilation: Compiled (.NET Framework), Just-In-Time (JIT) compilation (.NET Core)
// Concurrency: Supports multi-threading, async/await

cotrClass

C# Class

public class ${1:MyClass} {
${2:// Your code here}
}

cotrIfElse

C# If Else Statement

if (${1:condition}) {
${2:// Your code here}
} else if (${3:condition}) {
${4:// Your code here}
} else {
${5:// Your code here}
}

cotrFuncSyntax

C# Function Syntax

// C# Function Syntax
// Basic function: public returnType FunctionName(parameters) { ... }
// Function with arguments: public returnType FunctionName(argType argName, ...) { ... }
// Named parameters in method calls: FunctionName(argName: value, ...);

cotrPrintMulti

C# Print Multi

Console.WriteLine(@"""
${1:Line 1}
${2:Line 2}
${3:Line 3}
""");

cotrTypesBoolTrue

C# Boolean True

true

cotrFunc

C# Function

public ${1:void} ${2:MyFunction}(${3:parameters})
{
${4:// Your code here}
}

cotrFuncLambda

C# Lambda Function

(${1:parameters}) => ${2:expression}

cotrVarTyped

C# Create Typed Variable

${1:Type} ${2:myVar} = $3;