C# CheatSheet

If you're looking for a reliable and comprehensive resource to help you master C#, look no further than our C# cheat sheet. This professionally crafted reference guide provides all the essential syntax, commands, and concepts you need to excel in this powerful programming language.

Our cheat sheet is designed to be both attractive and functional, with clear, concise explanations and helpful examples that make it easy to understand even the most complex C# concepts. Whether you're a seasoned developer seeking to enhance your skills or a newcomer just starting to explore the world of coding, our C# cheat sheet is an invaluable tool that will help you achieve your goals.

With our C# cheat sheet, you'll have everything you need at your fingertips, including detailed information on data types, variables, operators, control structures, classes, methods, and more. You'll also find handy tips and tricks to help you optimize your code and improve your productivity.

So why wait? Download our C# cheat sheet today and take your programming skills to the next level! With its attractive design, professional formatting, and wealth of useful information, it's the perfect resource for any developer looking to succeed in the exciting world of C#.


Table of Content




# Getting started C#


What is C# ?

C# is a general-purpose, modern and object-oriented programming language pronounced as "C sharp".

C# was developed by Microsoft led by Anders Hejlsberg and his team within the .Net initiative and was approved by the European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO).

C# is among the languages for Common Language Infrastructure.

C# is a lot similar to Java syntactically and is easy for the users who have knowledge of C, C++ or Java.

C# is used for:

  • Mobile applications
  • Desktop applications
  • Web applications
  • Web services
  • Web sites
  • Games
  • VR
  • Database applications
  • And much, much more!

Why C# ?

  1. Easy to start: C# is a high-level language so it is closer to other popular programming languages like C, C++, and Java and thus becomes easy to learn for anyone.
  2. Widely used for developing Desktop and Web Application: C# is widely used for developing web applications and Desktop applications. It is one of the most popular languages that is used in professional desktops. If anyone wants to create Microsoft apps, C# is their first choice.
  3. Community:The larger the community the better it is as new tools and software will be developed to make it better. C# has a large community so the developments are done to make it exist in the system and not become extinct.
  4. Game Development: C# is widely used in game development and will continue to dominate. C# integrates with Microsoft and thus has a large target audience. The C# features such as Automatic Garbage Collection, interfaces, object-oriented, etc. make C# a popular game developing language.

Advantages of C#

  • C# is very efficient in managing the system. All the garbage is automatically collected in C#.
  • There is no problem of memory leak in C# because of its high memory backup.
  • Cost of maintenance is less and is safer to run as compared to other languages.
  • C# code is compiled to a intermediate language (Common (.Net) Intermediate Language) which is a standard language, independently irrespective of the target operating system and architecture.

Disadvantages of C#

  • C# is less flexible as it depends alot on .Net framework.
  • C# runs slowly and program needs to be compiled each time when any changes are made.

C# Program structure

The key organizational concepts in C# are programs, namespaces, types, members, and assemblies. Programs declare types, which contain members and can be organized into namespaces. Classes, structs, and interfaces are examples of types. Fields, methods, properties, and events are examples of members. When C# programs are compiled, they're physically packaged into assemblies. Assemblies typically have the file extension .exe or .dll, depending on whether they implement applications or libraries, respectively.

HelloWorld.cs

class HelloWorld {
    // main methord to execute
    static void main(string[] args) {
        Console.WriteLine("Hello world! Wellcome to bestforstudy.com C#");
        // Output: Hello world! Wellcome to bestforstudy.com C#
    }
}

// Compile and Run
$ dotnet run
Hello, world!

Variables

int number = 8;
long number = 8888888;
float number = 8.88;
double number = 88.888;
decimal number = 88.8888;
char characterLetter = 'D';
bool boolValue = false;
string siteString = "https://bestforstudy.com";
var num = 888;
var str = "888";
var bool = true;

Primitive Data Types

Data Type Size Range
int 4 bytes -231 to 231-1
long 8 bytes -263 to 263-1
float 4 bytes 6 to 7 decimals digits
double 8 bytes 15 decimals
decimal 16 bytes N/A
char 2 bytes 0 to 65535
bool 1 bit true / false
string 2 bytes per char N/A

String

string wellcome = "Welcome to:";
string site = "https://bestforstudy.com";
string fullText = wellcome + " " + site;
Console.WriteLine(fullText);
// Output: Welcome to: https://bestforstudy.com

User Input

Console.WriteLine("Please enter your name:");
string yourname = Console.ReadLine();
Console.WriteLine("Your name is: " + yourname);
// Output: Please enter your name:
// Output: Your name is: ...

Conditionals

int number = 10;

if (number == 10) {
    Console.WriteLine("Your number is 10");
} else if (number > 10) {
    Console.WriteLine("Your number is greater than 10");
} else {
    Console.WriteLine("Your number is less than 10");
}

Arrays

char[] chars = new char[10];
chars[0] = 'a'
chars[1] = 'b'
string[] letters = {"A", "B", "C"};
int[] mylist = {100, 200};
boolean[] answers = {true, false};

Loops

int numbers = 20

// basic loop with index
for(int i = 0; i < numbers; i++) {
    Console.WriteLine(numbers[i]);
}
// Output: 0...19


// foreach loop
foreach(int num in numbers) {
    Console.WriteLine(num);
}
// Output: 0...19

# Strings in C#


Definition

In C#, you can use strings as an array of characters, However, a more common practice is to use the string keyword to declare a string variable. The string keyword is an alias for the System.String class.

You can create string object using one of the following methods

  • By assigning a string literal to a String variable

  • By using a String class constructor

  • By using the string concatenation operator (+)

  • By retrieving a property or calling a method that returns a string

  • By calling a formatting method to convert a value or an object to its string representation

String concatenation

string hello = "Hello, Wellcome to";
string websiteName = "bestforstudy.com";

string sayHello = hello + " " + websiteName;
Console.WriteLine(sayHello); 
// Result: Hello, Wellcome to bestforstudy.com

String interpolation

string hello = "Hello, Wellcome to";
string website = "bestforstudy.com";

string sayHello = $"{hello} {website}";
Console.WriteLine(sayHello); 
// Result: Hello, Wellcome to bestforstudy.com

Verbatim strings

string verbatimStr = @"I can type anycharacters 
in here !#@$%^&*()__+ '' \n \t except 
double quotes and 
I will be taken literally. 
I even work with multiple lines.";

Member Example

// Using property of System.String
string strLength = "How long?";
strLength.Length           
// Result: 9

// Using methods of System.String
strLength.Contains("How"); 
// Result: true

String Members

Member Description
Length A property that returns the length of the string.
Compare() A static method that compares two strings.
Contains() Determines if the string contains a specific substring.
Equals() Determines if the two strings have the same character data.
Format() Formats a string via the {0} notation and by using other primitives.
Trim() Removes all instances of specific characters from trailing and leading characters. Defaults to removing leading and trailing spaces.
Split() Removes the provided character and creates an array out of the remaining characters on either side.


Best Suggest