jcollard / CSharpCookbook

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C# Cookbook

This repository is meant to be used as a reference during a programming session. It contains "recipes" for the syntax for performing various tasks in C# as well as common error messages.

This Cookbook is a work in progress! Feel free to make suggestions, open Issues, and create Pull Requests.

Common Errors

This is a collection of all the errors that appear in the Cookbook. If you have an error message, you can look it up by it's error number here and find links to relevant sections within the Cookbook.

Local Variables

A variable allows a programmer to manage the complexity of a program by naming a location in memory.

In C#, there are two types of variables: local variables and member variables. This section discusses local variables.

Recipes

<variable-type> <variable-name>; // Declaring a variable
<variable-name> = <expression>; // Assigning a variable
<variable-type> <variable-name> = <expression>; // Initializing a variable

If Statements

An if statement is a programming construct that we use to create selections within a program. Closely coupled with the if statement is the else statement which can be used to create a "this" or "that" selection.

Recipes

if (<boolean-expression-0>)
{
    // Body to execute if boolean-expression-0 is true
} 
else if (<boolean-expression-1>)
{
    // Body to execute if boolean-expression-0 is false and boolean-expression-1 is true.
}
else if (<boolean-expression-2>)
{
    // Body to execute if boolean-expression-0 and boolean-expression1 are false and boolean-expression-2 is true.
} 
//... (any number of else if statements to follow)
else // The final else statement is optional
{
    // Body to execute if all boolean-expressions are false.
}
// All branches continue executing at the end of the decision tree.

Basic Loops

There are three types of basic loops in C#: while, for, and do...while loops.

Recipes

while (<boolean-expression>) // While Loop
{
    // Body to execute
}

for (<init-counter>; <boolean-expression>; <increment-counter>) // For loop
{
    // Body to execute
}

do  // Do ... While loop
{
    // Body to be executed
}
while (<boolean-expression>);

For Each Loops

It is so common to iterate over each element in a list or other collection type that C# provides a special foreach loop to accomplish this.

foreach (<variable-type> <variable-name> in <iterable>)
{
    // Body to be executed for each element
}

Static Methods

Methods provide a way to manage the complexity of a program by breaking it into logical chunks that can be "called". In this section, we will see the recipe for public static methods which are roughly equivalent to functions in other programming languages.

// Declaring a public static method
public static <return-type> <method-name>(<method-parameters>)
{
    // Body of the method
}

// Calling a public static method
// Within an expression:
<method-name>(<method-arguments>);

Lists

TODO:

Arrays

TODO:

Custom Data Types

Classes and Member Variables (Fields)

TODO:

Constructors

TODO:

Methods

TODO:

Properties

TODO:

Static Class Variables

TODO:

About