Dev Tasks
Before you can start tinkering, you need to get your tools up and running.
Task: Open Visual Studio.
Task: Create a .NET console project.
Task: Figure out how to run the program.
A console project is a program that executes within a console terminal. It can output text to the user and also receive text input from the user. This is the mose basic and arguably one of the most useful types of program. It is generally considered to be a non-UI (user interface) program, but the console can actually be thought of as a basic UI.
Task: Create a console project.
Task: Write the text "Hello World" to the console. Show Example
You can write to the console with Console.WriteLine();
Example:
Console.WriteLine("Hello World");
Variables are the heart and soul of programming, they allow you to store bits of data for later use. Without them we'd really have no programs at all.
Question: What is a variable? Show Example
A variable is a container for a value. It allows you to store information for later use.
Before you can use a variable, you must declare it. You can declare the variable using the following format:
[variableType] [variableName] = [variableValue];
When you declare a variable, you have to define its type. Variables can only contain the type of data that is specified at the time of their declaration.
Question: What are some basic varible types? Show Example
int //integer, use for whole numbers.
string //string is used to store text.
double //used to store decimal numbers, like 3.14
bool //used to store TRUE and FALSE.
Task: Declare a interger variable and set its value to 100. Show Example
Declare variables first by typing its type, then its name. Optionally followed by setting its value.
int value;
int value = 10;
int variableName = 100;
Task: Declare a string variable, set its value to "Hello" and write the variable to the console window. Show Example
Declare the variable:
string variableName = "Hello";
You can write to the console with Console.WriteLine();
Example:
string variableName = "Hello";
Console.WriteLine(variableName);
Task: Declare an integer variable, set it to 10, increment its value by 1 and then write the variable to the console window. Show Example
You can increment a variable in several ways (you can do the same with subtraction)
value = value + 1; //Adds 1 to value;
value++; //Adds 1 to value;
value = value + 10; //Adds 10 to value;
value += 10; //Adds 10 to value;
Example:
int value = 10;
value++;
Console.WriteLine(value);
Basic flow control functions allow your program to behaive differently based on input and variables. These functions include IF, SWITCH, WHILE, DO, and FOREACH.
Question: What is an IF statement? Show Example
An IF statement is the most common, widely used and most useful flow control statement in existence. Is tells the program to only do somehitng if a condition is true. This allows your program to act differently based on inputs.
Example:
if(userName = = "Aiden")
{
Console.WriteLine("Hello Aiden");
}
Question: What is an ELSE statement? Show Example
An else statement is optional and is used in conjunction with an IF statement. It tells the program what to do if the IF statement is NOT true.
Example:
if(userName == "Aiden")
{
Console.WriteLine("Hello Aiden!");
}
else (This is the ELSE statement.)
{
Console.WriteLine("Hello Sombody else!");
}
Question: What is an ELSE-IF statement? Show Example
An ELSE-IF statement is optional and is used in conjunction with an IF statement. It tells the program what to do if the original IF statement is NOT true but the next statement IS true.
Example:
if(userName == "Aiden")
{
Console.WriteLine("Hello Aiden!");
}
else if(userName = = "Cole") (This is the ELSE IF)
{
Console.WriteLine("Hello Cole!");
}
else
{
Console.WriteLine("Hello Sombody else!");
}
Task: Write a program that declares an interger with a value of 10 and outputs to the console "BIG" if the value is over 5, otherwise the program should output the text "SMALL"? Show Example
You will need to use an IF and an ELSE such as in the following example:
int value = 10;
if(value > 5)
{
Console.WriteLine("BIG");
}
else
{
Console.WriteLine("SMALL");
}
Functions allow you to neatly package up peices of code into a callable collection. They allow you to reuse important peices of code and also break down your code into manageable or logical chunks.
Question: What is a function? Show Example
A function is a collection of code that can be reused and called from various places in code.
For example, the function String.ToString() is a function that you will use alot. It contains all of the code to convert your variable to a string so you dont have to write all that code yourself each time.
A function typically takes parameters and returns a value and typically looks like this pseudo code:
[ReturnType] [FunctionName]([parameters])
{
[code body....]
return [return value];
}
Example:
int Times10(int value)
{
return value * 10;
}
Task: Write a function that takes two parameters int x and int y. It should multiply them together and return the result. Show Example
Example:
int MyMultiply(int x, int y)
{
return x * y;
}
Task: Using the function from above, write the return value to the console. Show Example
void main()
{
Console.WiteLine(MyMultiply(5, 10));
}
int MyMultiply(int x, int y)
{
return x * y;
}
A loop allows you to perform a task over-and-over again while a condition is true.
Question: What are the different types of loops? Show Example
while
for
do-while
Question: What is a loop flow-control statement? Show Example
You can control the flow of a loop with break; and continue;
Break tells the loop to exit now, even if the loop condition is still true.
Continue tells the program to go back to the top of the loop and start over without reaching the end of the loop.
Task: Declare an integer variable, set it to 0. In a while loop, increment your variable by 1 until it reaches 10. Each time you loop, write the result to the console. Show Example
While is like "if", it takes a condition and continues to loop over the scope defined by the curly brackets as long as that condition is true.
Example:
int value = 0;
while(value < 10)
{
Console.WriteLine(value);
value++; //This increments the value by one.
}
Task: Write a console program that allows the user to enter text in an infinite loop. Output the users text to the console. Show Example
For this task you will need to use a while loop, Console.ReadLine and Console.WriteLine.
Just like you can write text with Console.WriteLine(), you can get text from the user with the Console.ReadLine() function.
To make a loop infinite, you need to specify a condition that is always true. Such as 1 = = 1 or simply true.
Example:
while(true)
{
userInput = Console.ReadLine();
Console.WriteLine(userInput);
}
Task: In an infinite loop, write a console program that allows the user to enter text. Exit the loop when the user types "exit". Show Example
Again we will be using Console.ReadLine() but will also be using the flow control function BREAK to exit the loop.
Example:
while(true)
{
string text = Console.ReadLine();
if(text == "exit")
{
break;
}
}
Task: Ask the user for their name and save it in a variable. In an infinite loop allow the user to input text, if they type "HI" then output their name. Show Example
Example:
Console.WriteLine("What is your name?");
string personName = Console.ReadLine();
while(true)
{
string text = Console.ReadLine();
if(text == "hi")
{
Console.WriteLine($"Hello {personName}!");
}
}
Classes are what make up the bulk of the code in modern programming. Programming with classes is know as OOP or Object Oriented Programming. All of the functionality you write can be wrapped into classes and subcategories into smaller peices into sub-classes.
Writing code with classes is called encapsulation because the code is enclosed inside a class.
Question: What is a class? Show Example
A class is a collection of variables and functions that is "packaged up" and reusable. They also support things like inheritence, which we will get into later.
Classes generally have to be instantiated (which means to create an instance of). Just like you can declare a string called firstName and string called lastName, you can create multiple instances of your own classes too.
Question: Can you name any classes in the C# library? Show Example
string
int
double
Console
MessageBox
Question: What are class accessibility levels? Show Example
Class accessibility levels tell the class whether a function, property, or variable can be accessed from outside of the class or project.
Question: What are the major class accessibility levels? Show Example
public - The function or variable can be access by all code.
private - The function or variable can only be accessed by the class itself.
More advanced protection levels are (more on these later):
protected - The function or variable can only be accessed by the class itself and other classes derived from that class. More on that later.
internal - The function or variable is only accessible to the assembly (think: program),
Question: What is a class Function/Method? Show Example
Just like regular functions, a class function is a collection of C# code that can be reused, in this case that function would belong to a class.
Question: What is a class Property? Show Example
On the most basic level. a class property is like a variable that belongs to a class. Its value can be set and retreived. While it is not a function, you can write code that controls how the property is set and retreived.
Task: Create a class for a Automobile that can have a color and weight. Show Example
Example:
public class Automobile {
public string Color;
public int Weight;
}
Task: Using your Automobile class, declare an instance of the class and set the color to RED and the weight to 1000. Show Example
Before using a class we have to declare a variable of the class type (just like we would declare any other variable), but then we also have to instanciate it. This means we have to create an instance of the class and set it to our variable.
*Example:*
//Declare the variable:
Automobile mycar;
//Instanciate the variable
mycar = new Automobile();
//Set some class instance variables:
mycar.Color = "RED";
mycar.Weight = 1000;
Question: What is a static class? Show Example
Previously when we looked at classes we saw hoe we have to create an instance of a class in order to use it. This is not always the case. When we declare a class as static, we are staying that there is only one instance and it is already instanciated.
Task: Create a static class for a person that contains a FirsName and LastName. Show Example
Example:
static class Person {
public string FirstName;
public string LastName;
}
Task: Using the static Person class from above, set the value of FirstName to John and LastName to Smith and then write them to the console.. Show Example
As you can see from the example below, we do not have to declare a variable for the Person class or instanciate it because its a STATIC class.
Example:
Person.FirstName = "John";
Person.LastName = "Smith";
Console.WriteLine($"{Person.FirstName} {Person.LastName}")'
String as probably the most used type of variable in C#. They are used to store text, or more accurately strings of characters - which is why they are called strings.
Task: Output the length of the string "Hello Cruel World". Show Example
A string is just a class, it has properties and one of those properties is the LENGTH property. This allows us yo easily figure out how long a string of text is.
someVariable.Length -or- "".Length
Example:
string text = "Hello Cruel World";
Console.WriteLine(text.Length);
Task: Using the string "Hello Cruel World", extract the middle word "Cruel" and output it to the console. Show Example
You can isolate parts of a string with the string class method SubString()
In this example you will see that the SubScring fnuction takes two parameters: a starting position and a length. We will pass 6 as the starting position which is the length of "HELLO ". Then we will pass 5 as the length which is the length of the word "Cruel". This will extract that part of the string and return it.
Example:
string text = "Hello Cruel World";
Console.WriteLine(text.SubString(6, 5));
Ignore these until they are added to the list above
Winforms Projects
Basic
Task: Make a program that displays a message box that says "HELLO!" when you press a button.
Task: Make a program that adds two numbers and displays the result in a message box when you press a button.
Events
Task: Create a program that has a button, but add the click event manually. Show Example
You can see the events exposed to a class (yes, button is a class) by hitting dot (.) after the class instance name.
buttonOk.Clicked +=
In visual studio, the program will auto complete the various components to "wire up" the event when you hit tab.
Other
- File I/O - Learn how to read and write to files using System.IO.
- Arrays
- Lists
- Linq
- Threads