C# Cheatsheet

1735




Basics

Sample Program

using System;

namespace HelloWorld
{
	public class Program
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Hello, World!");
		}
	}
}
  • Using Keyword : using keyword is used to include namespaces in the program.
  • Namespace declaration : Namespace is a container for classes and other namespaces. The HelloWorld namespace contains the class Program.
  • Public Class : class is a container for data and methods, you are declaring Program as a class with public visibility.
  • Main : Beginning of your program
  • Console.WriteLine : Console is a class of the System namespace and WriteLine() is a method in it which is used to print text to the console.
  • C# statements end with a semicolon ;
  • C# is Case-sensitive
  • // : Single line Comment
  • /* */ : Multi Line Comments

Data types

Data typeDescriptionRangeMemory Size
byteused to store unsigned integer0 to 2551 byte
sbyteused to store signed integer-128 to 1271 byte
shortused to store signed integers-32,768 to 32,7672 bytes
intused to store signed integers-2,147,483,648 to 2,147,483,6474 bytes
longused to store signed integers-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8078 bytes
floatused to store fractional numbers6 to 7 decimal digits4 bytes
doubleused to store fractional numbers15 decimal digits8 bytes
charused to store a single character enclosed in single quoteone character2 bytes
boolBoolean data typeStores either true or false1 bit
StringStores a sequence of characters enclosed in double quotesSequence of Characters2 bytes per character

Variables

datatype variableName = value;
int x = 10; // declaring int variable and assigning value 10 to it
char grade = 'A'; // declaring char variable and assigning value A to it

Constants

const datatype variable-name = value;

String functions

Function nameDescription
str.Lengthto return the length of string str
+to concatenate two strings
string.Concat(str1,str2)to concatenate two strings str1 and str2
Copy(str1, str2)To copy string str2 into string str1.
Compare(str1, str2)returns 0 if str1 and str2 are the same and less than 0 if str1 < str2 and a positive number if str1 > str2
Join(str, String[])concatenate all the elements of the given string array with the specified separator between each element.
Split(Char[])splits a string into substrings based on the characters in an array
str.ToUpper()converts the string to upper case
str.ToLower()converts the string to lower case
ToString()to return instance of a string
Trim()removes all leading and trailing whitespaces from a given string
Clone()returns a reference to this instance of String
Format()it returns a formatted string
LastIndexOf()it returns the last occurence of the string
Replace()it replaces the specified string character with new character
Contains()it checks whether a string contains substring
ToCharArray()it converts a string to a character(char) array
isEmpty()it checks whether the string is empty or not and return true or false respectively
startsWith("char")it checks the string start with given character or not and return true or false respectively
endsWith("char")it checks the string end with given character or not and return true or false respectively
charAt(int)it returns the character at the specified index in a string
## Conditional Statements

1. If

if(conditional-expression)
{
    //code
}

2. If-else

if(conditional-expression)
{
    //code
} else {
    //code
}

3. If-else-if ladder

if(conditional-expression-1)
{
    //code
} else if(conditional-expression-2) {
    //code
} else if(conditional-expression-3) {
    //code
}
....
else {
    //code
}

4. Switch

switch(conditional-expression){    
case value1:    
 //code    
 break;  //optional  
case value2:    
 //code    
 break;  //optional  
...    
    
default:     
 //code to be executed when all the above cases are not matched;    
} 

Loops

1. For

for(Initialization; Condition; Increment/decrement){  
//code  
} 

2. While

while(condition){  
//code 
}  

3. Do-While

do{  
//code 
} while(condition); 

Arrays

data-type[] array-name; //declaration
array-name = new data-type[size]{ array-elements }; //initialization

[or]

data-type[] array-name =  new data-type[size]{ array-elements }; //declaration and initialization

[or]

data-type[] array-name =  { array-elements }; //short syntax of array declaration and initialization

Examples

int[] num = {1,2,3,4,5};

Functions

<AccessSpecifier> <return-type> FunctionName(<parameters>)  // functin definition
{  
//code
}
function_name (parameters); // calling a function

Structures

struct structure_name {

   member definition;
   member definition;
   ...
   member definition;
}; 

structure_name variable name; //declaring structure variables

enum

enum name{constant1, constant2, constant3, ....... } ;