Skip to content

Lesson1

Foong Pak Chuen edited this page Nov 17, 2018 · 8 revisions

Table of Contents

1.Basic git

2.Basic C#

Basic Git

To clone a repo in GitHub:

Open Command Prompt (cmd)/Terminal (term)

In the cmd/term,

"cd" into your targeted directory

then, type "git clone <your github repo web address>"

image

Basic C#

System Data Types and Corresponding C# Keywords

Similar to any programming language, C# defines keywords for fundamental data types, which are used to represent local variables, class data member variables, method return values and parameters.

Below lists each system data type, its range, the corresponding C#keyword and the type's compliance with the common language specifications(CLS)

image

The for Loop

When you need to iterate over a block of code of a fixed number of times, the for statement provides a good deal of flexibility. Below it is a sample of the syntax:

// A basic for loop.
static void ForLoopExample()
{
// Note! "i" is only visible within the scope of the for loop.
for(int i = 0; i < 3; i++)
{
Console.WriteLine("Number is: {0} ", i);
}
// "i" is not visible here.
}

The foreach Loop

The C# foreach keyword allows you to iterate over all items in a container without the need to test for an upper limit. Unlike a for loop, the foreach loop will walk the container only in a linear (n+1) fashion. Thus you cannot go backward through the container,skip every third element, etc.

// Iterate array items using foreach.
static void ForEachLoopExample()
{
string[] carTypes = {"Ford", "BMW", "Yugo", "Honda" };
foreach (string c in carTypes)
Console.WriteLine(c);
int[] myInts = { 10, 20, 30, 40 };
foreach (int i in myInts)
Console.WriteLine(i);
}

The while and do/while Looping Constructs

The while looping construct is useful should you want to excecute the block of statements untul some terminating condition has been reached.

static void WhileLoopExample()
{
string userIsDone = "";
// Test on a lower-class copy of the string.
while(userIsDone.ToLower() != "yes")
{
Console.WriteLine("In while loop");
Console.Write("Are you done? [yes] [no]: ");
userIsDone = Console.ReadLine();
}
}

Like a simple while loop, do/while loop is used when you need to perform some action an undetermined number of times. The difference is that do/while loops are guaranteed to execute the corresponding block of code at least once. In contrast, it is possible that a simple while loop may never execute if the terminating condition is false from the onset.

static void DoWhileLoopExample()
{
string userIsDone = "";
do
{
Console.WriteLine("In do/while loop");
Console.Write("Are you done? [yes] [no]: ");
userIsDone = Console.ReadLine();
}while(userIsDone.ToLower() != "yes"); // Note the semicolon!
}

The if/else Statement

The if/else statement in C# operaters only on Boolean expresssions, not ad hoc values such as -1 or 0. Below shows an example of the syntax:

static void IfElseExample()
{
// This is illegal, given that Length returns an int, not a bool.
string stringData = "My textual data";
if(stringData.Length)
{
Console.WriteLine("string is greater than 0 characters");
}
}

The switch Statement

As in other C-based languages, the switch statement allows you to handle the program flow based on a predefined set of choices.

// Switch on a numerical value.
static void SwitchExample()
{
Console.WriteLine("1 [C#], 2 [VB]");
Console.Write("Please pick your language preference: ");
string langChoice = Console.ReadLine();
int n = int.Parse(langChoice);
switch (n)
{
case 1:
Console.WriteLine("Good choice, C# is a fine language.");
break;
case 2:
Console.WriteLine("VB: OOP, multithreading, and more!");
break;
default:
Console.WriteLine("Well...good luck with that!");
break;
}
}

Note C# demands that each case (including default) that contains executable statements have a terminating break or goto to avoid fall-through

Breakpoint

Breakpoints are most commonly used to interrupt a running program immediately before the execution of a programmer-specified instruction. This is often referred to as an instruction breakpoint.

Other kinds of conditions can also be used, such as the reading, writing, or modification of a specific location in an area of memory. This is often referred to as a conditional breakpoint, a data breakpoint, or a watchpoint.

Breakpoints can also be used to interrupt execution at a particular time, upon a keystroke etc.

image

When you debug, execution pauses at the breakpoint, before the code on that line is executed. The breakpoint symbol shows a yellow arrow. At the breakpoint in the following example above, the value of testInt is still 1.

Further Reading

  1. Git Cheat Sheet

  2. C# and the .Net 4.6 Framework

  3. Tutorial: Learn to debug using Visual Studio

  4. Use breakpoints in the Visual Studio debugger