CSharp Programming For Human Beings
CSharp Programming For Human Beings
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to
master applying your C# skills to Unity!
Unlimited access to EVERY course on our Courses that will quickly get you coding
platform! Get new courses each month, with the world's most popular languages!
help from expert mentors, and guided Discover Python, web development, game
learning paths on popular topics. development, VR, AR, & more.
No experience is required to take this Learn how to build games with C# and
project-based course, which covers Unity! You'll master popular genres
variables, functions, conditionals, loops, including RPGs, idle games, Platformers,
and object-oriented programming. and FPS games.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to
master applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
In this lesson, we are going to download our IDE, Visual Studio Community 2015, learn how to install
and configure it and write our first program that shows on the screen the sentence “Hello World”.
Before we get start, we need to know, what is an IDE?. IDE stands for Integrated Development
Environment, in the computer area. An IDE is a software suite where developers can write and test their
software. We will use Visual Studio Community 2015 as our IDE for these tutorial series.
Visual Studio is a powerful IDE from Microsoft. It not only has tools to create software since UI design,
coding, testing and debugging, but also websites, applications, and services. Our focus in this course is to
learn the language C#.
As I said, we will use C# as our programming language. ‘C-Sharp’ is an object-oriented language created
by Microsoft. Based on C++ and JAVA, C# offers the same power as the C++ with the ease of Visual Basic
programming. So, let’s get started!
Click here to visit Visual Studio website and click on ‘Download Community Free’.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
After the installation, if it asks for RESTART, please do it; otherwise, click LAUNCH.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Here you can you choose your color theme. I use Blue theme. You can change it later. After selecting
your theme, click on Start Visual Studio.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
HELLO WORLD
To write our programs, we will use a simple console application. Follow the steps below to start writing
our first program and run it in a console application.
Installed -> Templates -> Visual C# -> Windows –> Console Application.
Name: HelloWorld;
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Write or copy and paste the code below into Visual Studio code area. In the following tutorials, we are
going to understand every single line of the program.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Console.Read(): wait for the user to press a key to end the program.
The using above everything is a type of statement used to refer to a namespace that can be present in
a dll in order to easily access classes that are within this namespace. In our case, we are using “System”.
Without it, instead of writing Console.Writeline, we should write System.Console.Writeline.
To run the application, we simply click on Start or press F5. Let’s run our application.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This is the end of this tutorial. I hope you have enjoyed and I see you in the next lesson!
Thank you!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
All of the source code for this tutorial can be downloaded here.
In this lesson, we are going to see two most used types of operators: arithmetic and logical/relational. In
addition, we are going to learn about some data types in C# and what is and how to declare a variable.
Last but not least, we are going to work with the data type string.
OPERATORS
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
All of the source code for this tutorial can be downloaded here.
In this lesson, we are going to see two most used types of operators: arithmetic and logical/relational. In
addition, we are going to learn about some data types in C# and what is and how to declare a variable.
Last but not least, we are going to work with the data type string.
OPERATORS
A + B = 7;
A – B = -3;
A * B = 10;
B / A = 2;
B % A = 1;
++A = 3;
– –B = 4;
DATA TYPES
A data type is a group of data with predefined characteristics in its value. Some example of these values
are found on the table below:
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
VARIABLES
A variable is a name given to a storage area that our programs can manipulate. It can be local or
attribute of a class. To declare a variable, we need to specify its type and name (<data_type>
<variable_name>). In addition, there are some access modifiers we are going to learn in this lesson that
are not so important now.
Each programming language has its own rules for naming variables. Here we have rules for giving a
name to a variable in C#.
Variables may contain letters and the digits 0 to 9. Not allowed: space or special characters, for instance
? ! @ # + – % ^ & *() [] {}.’;:”/ and \.
Variable names cannot be a Keywords belonging to C#. Example: continue, checked, public, return, etc;
It is time to write some code using what we have learned so far. We are going to write examples using
the Arithmetic Operator. Logical and Relational operators will be used as examples in lesson 4.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Installed -> Templates -> Visual C# -> Windows –> Console Application.
Name: Operators;
Write or copy and paste the code below into Visual Studio code area.
1 using System;
2
3 namespace Operators
4 {
5 public class Program
6 {
7 static void Main(string[] args)
8 {
9 int a;
10 int b;
11
12 a = 5;
13 b = 10;
14
15 Console.WriteLine("A + B = "+ (a + b));
16 Console.WriteLine("A - B = "+ (a - b));
17 Console.WriteLine("A * B = "+ (a * b));
18 Console.WriteLine("B / A = "+ (b / a));
19 Console.WriteLine("B % A = "+ (b % a));
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Console.Writeln(“A + B”+ (a+b)); = after the string “A+B” we add the “+” (plus) to concatenate to a
variable. Also, we wrote (a+b) inside a parentheses to indicate that this action performs first, and then
we concatenate with the string. If we had writing (“A + B ”+ a+b), the results would be “ A + B = 510 ”.
The program would concatenate a and b, not sum them.
To run the application we simple click on Start or press F5. Let’s run our application: F5
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
The data type string is so powerful that has its own class and also has some methods. In Lesson 5 we are
going to learn more about classes and how to work with methods. We will have a preview of these
contents here, but it is not our focus now. So, let’s begin!
String Properties
As I mentioned above, the class string holds methods and properties. When we declare a string, we can
have access to its properties by pressing dot and our IDE will show us what methods and properties are
accessible to use.
In the example below, we have chosen to use the property Length. This property gets the number of
characters in the current string, including spaces.
Let’s write some code. Start a new project, name it Strings and write or copy and paste the code below.
1 using System;
2
3 namespace Strings
4 {
5 class Program
6 {
7 static void Main(string[] args)
8 {
9 string str1 = "Learning C# with Zenva!";
10
11 Console.WriteLine("The length of str1 is:" + str1.Length);
12 Console.Read();
13 }
14 }
15 }
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Note that to access the properties from the variable, in this case, string type, after the name of the
variable, we put a dot(.) to see its properties and methods. In our example, we access the property
“Length”.
String methods
Below we have a few methods that we can use with the String class. Some of these methods, we are
going to write a the code for them using the structure conditional ‘IF’. This structure we are going to
learn in Lesson 3 which wraps the flow control in C#.
Compare: This method compares two strings if they are equals or not and return 0 if it is true, else 1 if it
is false.
Concat: This method concatenate two strings and return as resulst both strings together.
Contains: This method verify if there is a(some) word(s) in the current string I passed.
ToUpper: This method gets the string I am calling this method from and return the same string but now
in uppercase.
ToLower: This method gets the string I am calling this method from and return the same string but now
in lowercase.
The table below show us some examples using these methods we learnt above.
Let’s consider we have two variables type string: str1 = “Learning C# with” and str2 = “ Zenva.”;
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
These are just a few examples of string methods. When writing code with the IDE Visual Studio, it shows
us tips and how some methods, for example, work. Get used to started learning with Visual Studio too.
This is the end of this tutorial. I hope you have enjoyed and I see you in the next lesson!
Thank you!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
All of the source code for this tutorial can be downloaded here.
Sometimes, a program needs to repeat a process until some condition is met. We call that “loop”. In
this lesson, we will see how to work with some structures as for instance, if statement, switch
statements, while, do while, for and foreach. Let’s begin.
IF STATEMENT
1 if(expression)
2 {
3 statement;
4 }
The “if” statement determines which way the program will continue. With the “if” statement, the
program has to decide if it will execute some code or not.
First example: Let’s write a code using the method “Compare” from the String class we learned in the
last lesson.
Start a new project, name it FlowControlIfExample and write or copy and paste the code below.
1 using System;
2 namespace FlowControlIfExample
3 {
4 class Program
5 {
6 static void Main(string[] args)
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
const string str1 = “Learning C# with”, str2 = “Zenva.”; Here I am declaring two constants, type string
and initializing its value. The word const before everything means that this constant will not be
modified. The two constants are separable by the comma.
As we observe, the program has an If statement to decide which way to go. In our example, we are
comparing two strings if they are equals. If the result is 0 or (true), the program executes the first
statement, else (meaning not true or 1) the program executes the second statement.
Running our program we have that the two strings are not equal.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
1 int n1 = 5, n2 = 1;
2 string result = n1 > n2 ? "N1 is greater" : "N2 is greater";
3
4 //result: N1 is greater
1 using System;
2 namespace FlowControlIfExample
3 {
4 class Program
5 {
6 static void Main(string[] args)
7 {
8 // const string str1 = "Learning C# with ", str2 = "Zenva";
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
To comment the code we can use “//” in front of the line or /* */ for a block. When we have
commented code, our application will not execute that line or block.
So in this code, we verify, if the first condition is NOT TRUE, (remember the operator “!”), we show a
message that the word we chose to check in contains method does not exist in the specified sentence. If
the word exists, then we go and check if exists the word “Ferrari”. If exists we ask for the user what
word he wants instead of Ferrari. We read what the user type and store in the variable str2 that we just
created.
Using the method replace, we pass the str1 + the new word using the replace method to the str3 and
show it in the console application.
SWITCH STATEMENT
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
1 switch(expression)
2 {
3 case constant-expression:
4 statement(s);
5 break;
6 default: default expression;
7 }
Example: The program asks the user to type a value between 1 and 7 which determines the day of the
week.
For this example, let’s create another project and give it a name FlowControlExamples.
1 using System;
2
3 namespace FlowControlExamples
4 {
5 class Program
6 {
7 static void Main(string[] args)
8 {
9 Console.Write("Type a number 1 - 7 to see the day of the wee
");
10
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
As everything the user writes is a string, a number written in the console application is a
string that needs to be converted to int type when the user writes numbers.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
While: used to repeat a block of code until a condition is true. The condition to execute the loop must
be a boolean value.
1 While (expression)
2 {
3 statements;
4 }
Do/While: works as same as the while, but using this statement we have our code executed at least
once.
1 using System;
2
3 namespace FlowControlExamples
4 {
5 class Program
6 {
7 static void Main(string[] args)
8 {
9 #region
10
11 int x = 0; //creating a variable int starting with 0 as
value.
12
13
//Here the program shows us 0 as result
14
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
As you can see, there is a different word in the code: #region. My last code is all commented inside this
“region”.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
FOR / FOREACH
Also used for loops, but the difference is that here we already know how many times the condition will
repeat. We declare a counter variable, which is automatically increased or decreased in value during
each repetition of the loop.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
With the for statement we specify the loop bounds (minimum or maximum) while with the foreach
statement we do not need to specify a boundary.
We will write some code using the command for and foreach in the next lesson!
This is the end of this tutorial. I hope you have enjoyed and I see you in the next lesson!
Thank you!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
All of the source code for this tutorial can be downloaded here.
In this lesson, we will see what is an array and what is a list; the difference between them; how to
declare and use them together with the for statement we learnt in the last lesson.
ARRAYS
An Array is a data collection of the same type. A feature of the arrays is that they are static, that is, the
programmer cannot increase the amount of positions in runtime. So if we declare an array of int with 10
positions, these positions will remain until the end of the program for this array.
When we declare an array, it does not mean it is initialized in the memory because by now, we cannot
assign values to it.
Arrays, lists and objects, need to be initialized so we can assign them values. To initialized these structs,
we use the word new. For example:
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Time to write some code! Let’s start a new Project and name it ArrayAndList. Write or copy and paste
the code below.
Array example:
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
We can also use the structure for to fill our array with numbers, for instance:
LISTS
A List(T) has the same characteristics of an array. However, a List(T) can be increased dynamically. In a
List(T), we do not establish its length. As we need more values to be put into a list, we simply write the
method ‘Add’ and we add a new value.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Both arrays and lists are very easy to work. Also, they are simple and powerful.
List example:
1 using System;
2 using System.Collections.Generic;
3
4 namespace ArrayAndList
5 {
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 #region
11
12 //Declaring a list "int".
13 List<int> list = new List<int>();
14
15 //Populating the list with 10 elements
16 for(int i = 0; i <= 10; i++)
17 {
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Clear: To remove all the elements from a list we use the function Clear().
1 orders.Clear();
2
3 Console.WriteLine(orders.Count);
4
5 //Result: 0
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Distinct: Remove duplicates elements in the list. This method belongs to System.Linq, so we need to add
this using System.Linq in our application.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Thank you!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
All of the source code for this tutorial can be downloaded here.
A method is a group of statements that together not only perform a task but also help to organize our
code. Every C# program has at least one class with a method named Main. A class is where you will
organise your methods and variables. In this lesson, we will discuss more about it. When we first create
a new project, the project comes with a class called Program and a method called Main(), have you
noticed it? In this lesson, we are going to learn how to define methods and use them, handle exceptions
and treat them using the command “try”.
Before we start, remember back in Lesson 2, I said about access modifiers. They suit for both variables
and methods. Let’s understand them. The three most used are:
Defining Methods
Each method has his own signature, that is, name and parameters. I can have two methods with the
same name but different parameters. This includes a number of parameters and type.
Let’s start a new project, choose console application and name it MethodsAndException. Write or copy
and paste the code below.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
In this example, I made my class and methods public. The method SumTwoNumbers receives as
parameters two integers, number1, and number2. It has on its return type, void, that is, the method
does not return a value. This method simply sums the two numbers and shows on screen. Note that I am
doing the operation in the message itself. I could create a variable to receive the sum and then write this
variable, “result” for example, in the message. To call a method, we write its name and provide the
parameters needed.
1 using System;
2
3 namespace MethodsAndException
4 {
5 public class Program
6 {
7 ////method is static because I am calling it from the static me
8 //public static void SumTwoNumbers(int number1, int number2)
9 //{
10 // Console.WriteLine(number1 + " + " + number2 + " = " + (nu
11 //}
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
The method WhoIsGreater also receives two parameters (integer), A and B. This method verify if A is
greater than B and return true if condition applies or false if B is greater than A. In this case, we set
return type as bool, that is, returns a Boolean value(true or false).
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
What if, instead of a number, the user types a word or some character that is not a number? An
exception would occur:
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
1 try
2 {
3
4 }
5 catch (Exception)
6 {
7 throw;
8 }
We try to do something. If an exception occurs, it goes directly to “catch” and we can show a message
to the user. So, let’s put our code where we try to convert what the user type to int, inside the try-catch.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
A class consists of a group of variables, methods, and events that together enables you to create a
construct by grouping these items. A class is simply a representation of an object type. It is like a
structure that describes an object. By the way, an object is basically a block of memory that has been
allocated and configured in accordance with the model. A program can create multiple objects of the
same class. The object orientation is a bridge between the real world and the virtual, from this, it is
possible to transcribe the way we see real world elements to our source code, in order to enable us to
build complex systems based on objects.
Before we follow the example, let’s start a new project, choose console application and name it
ClassesAndObjects, write or copy and paste the codes below.
So, let’s think about a person. A person has some attributes. In our case, we will create the followings:
first name, last name, and age. With what you learnt until now, our code would look like this:
1 namespace ClassesAndObjects
2 {
3 public class Program
4 {
5 public static void Main(string[] args)
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
But what if we need to add one more person? New variables, firstName2, lastName2, age2? What about
5, 10, 100 records? Let’s create a class!!
1 namespace ClassesAndObjects
2 {
3 public class Person
4 {
5 public string firstName { get; set; }
6 public string lastName { get; set; }
7 public int age { get; set; }
8 }
9
10 public class Program
11 {
12 public static void Main(string[] args)
13 {
14
15 }
16 }
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Note that, when we are programming object-oriented, we do not use fields as our variables. We now
use Property.
A Property is a member that provides flexible ways to read, write, or compute values of fields. A get
property accessor is used to return the property value, and a set accessor is used to assign a new value.
Tip: To write a property, try writing prop and press ‘tab’ twice.
Ok, but now, how to have access to these properties of this class? We create an object! To create an
object, we need to instantiate the class and by doing that, we now have access to the class properties
and methods. By pressing “dot” on my object p type Person(class), it shows all methods and properties
accessible of this class.
Let’s set some values and show these values in our application.
1 using System;
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Now if you want a new Person, it is easy. Just create new objects of Person with different names, for
instance, p2, p3, p4… as you want.
Note that our message is full of ” “ and +. We certainly can get confused when we have to show a lot of
information. So, there are two ways to show the same message:
Using format: We can use {n} to determine the quantity of our variables.
Now with C# 6.0 we can write like this: We put a $ before the string, so ‘{ }’ are not recognised as part
of the string and yes a property.
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Now let’s receive the user age and check if the user is of legal age by calling the method
AgeOfMajority();
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!
Thank you!
This book is brought to you by Zenva - Enroll in our Unity Game Development Mini-Degree to master
applying your C# skills to Unity!