Setup
LinqPad is an .NET scratchpad to quickly test your C# code snippets. The standard edition is free and a perfect tool for beginners to execute language statements, expressions and programs.
Alternatively, you could also download Visual Studio Community 2015 which is an extensible IDE used by most professionals for creating enterprise applications.
Your First C# Program
//this is the single line comment
/** This is multiline comment,
compiler ignores any code inside comment blocks.
**/
//This is the namespace, part of the standard .NET Framework Class Library
using System;
// namespace defines the scope of related objects into packages
namespace Learning.CSharp
{
// name of the class, should be same as of .cs file
public class Program
{
//entry point method for console applications
public static void Main()
{
//print lines on console
Console.WriteLine("Hello, World!");
//Reads the next line of characters from the standard input stream.Most common use is to pause program execution before clearing the console.
Console.ReadLine();
}
}
}
Every C# console application must have a Main method which is the entry point of the program.
Edit HelloWorld in .NET Fiddle, a tool inspired by JSFiddle where you can alter the code snippets and check the output for yourself. Note, this is just to share and test the code snippets, not to be used for developing applications.
If you are using visual Studio, follow this tutorial to create console application and understand your first C# program.
Types and Variables
C# is a strongly typed language. Every variable has a type. Every expression or statement evaluates to a value. There are two kinds of types in C#:
- Value types
- Reference types.
Value Types : Variables that are value types directly contain values. Assigning one value type variable to another copies the contained value.
int a = 10;
int b = 20;
a=b;
Console.WriteLine(a); //prints 20
Console.WriteLine(b); //prints 20
Note that in other dynamic languages this could be different, but in C# this is always a value copy. When value type is created, a single space most likely in stack is created, which is a “LIFO” (last in, first out) data structure. The stack has size limits and memory operations are efficient. Few examples of built-in data types are int, float, double, decimal, char and string
.
Type | Example | Description |
---|---|---|
Integer | int fooInt = 7; |
Signed 32-bit Integer |
Long | long fooLong = 3000L; |
Signed 64-bit integer.L is used to specify that this variable value is of type long/ulong |
Double | double fooDouble = 20.99; |
Precision: 15-16 digits |
Float | float fooFloat = 314.5f; |
Precision: 7 digits.F is used to specify that this variable value is of type float |
Decimal | decimal fooDecimal = 23.3m; |
Precision: 28-29 digits.Its more precision and smaller range makes it appropriate for financial and monetary calculations |
Char | char fooChar = 'Z'; |
A single 16-bit Unicode character |
Boolean | bool fooBoolean = false; |
Boolean - true & false |
String | string fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs); |
A string of Unicode characters. |
For complete list of all built-in data types see here
Reference types : Variables of reference types store references to their objects, which means they store the address to the location of data on the stack, also known as pointers. Actual data is stored on the heap memory. Assigning reference type to another doesn’t copy the data, instead it creates the second copy of reference which points to the same location on the heap.
In heap, objects are allocated and deallocated in random order that is why this requires the overhead of memory management and garbage collection.aspx).
Unless you are writing unsafe code or dealing with unmanaged code.aspx), you don’t need to worry about the lifetime of your memory locations. .NET compiler and CLR will take care of this, but it’s still good to keep this mind in order to optimize performance of your applications.
More information here.
Flow Control Statements
If else statement
int myScore = 700;
if (myScore == 700) {
Console.WriteLine("I get printed on the console");
} else if (myScore > 10) {
Console.WriteLine("I don't");
} else {
Console.WriteLine("I also don't");
}
/** Ternary operators
A simple if/else can also be written as follows
<condition> ? <true> : <false> **/
int myNumber = 10;
string isTrue = myNumber == 10 ? "Yes" : "No";
Switch statement
using System;
public class Program {
public static void Main() {
int myNumber = 0;
switch (myNumber) { // A switch section can have more than one case label.
case 0:
case 1:
{
Console.WriteLine(“Case 0 or 1”);
break;
}
}
For & Foreach
for (int i = 0; i < 10; i++) {
Console.WriteLine(i); //prints 0-9 }
Console.WriteLine(Environment.NewLine);
for (int i = 0; i <= 10; i++) {
Console.WriteLine(i); //prints 0-10 }
Console.WriteLine(Environment.NewLine);
for (int i = 10 - 1; i >= 0; i—) //decrement loop
{
Console.WriteLine(i); //prints 9-0 }
Console.WriteLine(Environment.NewLine); //for (; ; ) { // All of the expressions are optional. This statement //creates an infinite loop.* //}
While & do-while
// Continue the while-loop until index is equal to 10.
int i = 0;
while (i < 10) {
Console.Write(“While statement”);
Console.WriteLine(i); // Write the index to the screen. i++;// Increment the variable. }
int number = 0; // do work first, until condition is satisfied i.e Terminates when number equals 4. do
{
Console.WriteLine(number); //prints the value from 0-4
number++; // Add one to number.
}
while (number <= 4);