Documente Academic
Documente Profesional
Documente Cultură
First edition
II Advanced C#
Iteration Statements 17
For Iteration 17
While Iteration 19
foreach Iteration 20
C# Classes In Depth 22
Constructor 22
Class Inheritance 24
Namespace 26
Interfaces 27
III Web Development With ASP.NET
Hello There! We hope you enjoyed this book. You can get the
full course on Udemy for only $9.99 from this link:
https://www.udemy.com/net-csharp-beginners-bootcamp/
?couponCode=KINDLE
3
Introduction to .NET and C#
4
C# language basics
5
.NET BEGINNERS GUIDE
Now, let’s run the console and let’s the results of this console
app. To do so, You can either hit F5 or from the menu Debug ->
Start Debugging. Notice that the output of the console is shown
in the Debug Console:
Also, you can pause the execution of the console to see the values
while debugging. To do so, you can place a breakpoint by clicking
on the far left inside the editor before the line number. Notice it
will place a red circle on the line:
6
C# LANGUAGE BASICS
and when you run the console app it will stop where you have
placed the breakpoint and show you the values:
7
.NET BEGINNERS GUIDE
C# Variables
Notice how we first put the type of the varaible we want to define
before it’s name so we tell the compiler what type of variable this
is and what type of data to be stored inside it. Remember that
C# is strongly type language so fi you define a variable of type
integer you cannot store inside it a string, for example using
“5” (Five with double quotes) will generate an error. Unlike
JavaScript this will be okay!
8
C# LANGUAGE BASICS
int x = 5;
int y = 7;
Console.WriteLine(x + y);
Always make sure the variables names are meaningful and easy
to understand.
C# Methods
9
.NET BEGINNERS GUIDE
The public keyword refers to the access level for this method.
Public indicates that this method can be accessed from the
outside world. Void is the return type and void indicates that this
method does not return any data. CalculatePoints refers to the
name of the method. The parenthesis refers to the parameters
that can be sent to this method. Parameters are the values that
can be sent to this method to be processed.
10
C# LANGUAGE BASICS
C# Class
11
.NET BEGINNERS GUIDE
To create a class in C#, we add a new file with the class name
and the extension of that file is (.cs). So, let’s add a new class
inside our project and let’s add the following code inside the
class so we declare the Student Class:
12
C# LANGUAGE BASICS
using System;
public class Student
{
public string Name { get; set; }
public void SayHello(){
Console.WriteLine($“Hello, {Name}“);
}
}
Now, we have our class ready to store the name of the student
and we can say hello with his name. But how we can call the
class and store data and call the methods inside it. We can do
this by creating an object of the class. Creating an object of the
class will create a new location in the memory that stores the
data we have placed inside it. So, let’s create a new object of the
class. To do so, we place the name of the class first and then the
name of the object of the class. After that we add the equal sign
and then the name of the class as follow:
13
.NET BEGINNERS GUIDE
as follow:
student.Name = “John”;
This way, we are setting the value of the property inside the
class object. Also we want to call the “SayHello” method so we
print the hello message for the student. This is also possible via
the object we created as follow:
student.SayHello();
14
II
Advanced C#
For Iteration
for is used to go over a code and make sure it’s executed more
than once and will stop as soon as the condition in the statement
are met. The following example are for a simple for that will be
executed 10 times and it will print on the console screen 10 hello
world.
17
.NET BEGINNERS GUIDE
{
Console.WriteLine(“Hello World!“);
}
If you also look closely to the code you will notice that there
is a condition. The i < 10 indicates that the loop will continue
unless the value of i is greater than 10. When the condition is met.
The loop will run for 10 times. That is because we are staring
from the zero value and increasing the i value until it reach 10.
We can also go the other way with the for loop. We can go from
higher number to lower. To do so, we invert the condition and
the statement that controls the value to be decreasing the value
18
ITERATION STATEMENTS
This statement will run for 10 times as the previous one. But it
will take the i value from higher to lower. These types of loops
can be used to make sure that we are not out of range for an
element or a type.
But what if we made a mistake and made the for statement
without an increment condition. What will happen? This way
we will never meet the condition to go out of the loop and the for
loop will keep going. This case is called infinte loop. The loop
that never ends. Be careful of creating of a such loop or it will
make your program unusable and always stops to execute the
loop:
While Iteration
19
.NET BEGINNERS GUIDE
So the previous code will run 10 times before it’s get termi-
nated. Note that if you do not include the increment of the x
inside the while it will become an infinite loop.
foreach Iteration
20
ITERATION STATEMENTS
In the previous code, we can see that the we will go into each
letter inisde the word variable and print it out. When using the
foreach iteration, we don’t need to set a condition for the loop to
stop, it will stop when it reach the last element inside it. Foreach
is better used with list of elements or items to go through them
and do some work on them.
21
C# Classes In Depth
Now, let’s get deeper with C# Classes as they are used a lot with
the C# applications development as a holder of data and to do
operations on them. We will first start with the Constructor of
the class, where we place our initial data and do operations as
soon as we create a new instance of the class. And then, we will
continue with the class inheritance. And we will finish off with
Namespaces where we can use them to organize classes.
Constructor
22
C# CLASSES IN DEPTH
Now the object we create of this class will look like this:
As you can see from the previous example, you now need to pass
a data to be stored inside the property in the class. This way we
can always guarantee that there is a data inside our class when
someone creates an object of it. But what if we want to allow
also the class property to be empty and also allow it to store
23
.NET BEGINNERS GUIDE
}
public Student(string name){
Name = name;
}
}
Now you have two options to create a new object of the class.
You either use the empty constructor and pass no data or use
the constructor which allows the value to be passed:
Class Inheritance
Class inheritance is the way that we use the class to give it’s
property to child classes instead of repeating them. For example,
we can create a shape class that has two property one for area
24
C# CLASSES IN DEPTH
and another for color. Then, we can create a square class that
inherit the shape class and now we have the property area and
also color available for us to be used. Next, we can create another
class for a circle and also we can inherit the shape class and also
have the property that we need without rewriting them again.
To inherit a class, we place it’s name with a colon before it on
the child class as follow:
Not that only, we can also create methods inside the parent class
and it will be visible for us in the child class as well. For example,
if we add the PrintArea method in the parent class it will allow us
to print the area value from all our child classes without writing
the same method again and again:
25
.NET BEGINNERS GUIDE
Namespace
namespace School
{
public class Student
{
}
26
C# CLASSES IN DEPTH
using School;
Interfaces
27
.NET BEGINNERS GUIDE
28
III
31
.NET BEGINNERS GUIDE
Now, to run the site hit F5 on the keyboard or select Debug ->
Start Debugging. Notice it will ask you to select an environment
so select .NET Core and also it might tell you it needs to add
configuration to help debug the project. Select add configuration
32
ASP.NET WEBSITE BASICS
as well. Now when you run the site, it will open a new tab inside
the browser and notice that the address of the site refers to
“localhost” which is the current locally debugging environment.
The result should be something similar to this:
In the next Section, we will learn more about MVC and how
can edit the pages and create new pages.
MVC
33
.NET BEGINNERS GUIDE
Models
Models represent the data holders inside the MVC site. Each
model represent an entity that relate to a unit of data that we
want to store and retrieve such as Employee information. We
use the models to represent this entity as class so we can work
with it to save data and do operations on the same data.
Models are usually placed inside the Models folder. And can
be grouped in a sub folder. Let’s add a new folder inside the
models folder and call it Employees. Inside this folder we will
add an EmployeeInfo.cs file so we can store the information
34
ASP.NET WEBSITE BASICS
for the employee. Also, we will add properties for this class
(Name, DateOfBirth, Email) so we can store information about
the employee. We will be using this model in the next part of
this chapter:
using System;
public class EmployeeInfo
{
public string Name { get; set; }
public DateTime MyProperty { get; set; }
public string Email { get; set; }
}
Views
Views are the visual part of our application. It’s where we place
our HTML code to render the controls for the pages so the user
can interact with our website. Also, inside the views we can
use Razor to apply application logic to the visual part of the
application for example to hide a certain section of the page if
the user does not has a certain permissions. Razor is the syntax
we can use inside the Views to write C# code inside the view file.
The views file are ending with cshtml which represent Razor
views file.
Inside the Views folder, we have some views already declared
for us. Such as the Home and the Shared views. Views can be
shared and reused again. If we open the Index view inside the
home folder we will see a lot of html created by default. Let’s
Delete all of it so we can work with a clean page. Now, let’s Add
35
.NET BEGINNERS GUIDE
What if we want to display the current date and time inside the
home page? How is that possible? We can do it via Razor. To
write the Razor code we add the @ sign in front of the code that
we want to place which is DateTime.Now to show the current
date time:
@DateTime.Now
Now when we run the site we will see the following result:
36
ASP.NET WEBSITE BASICS
So now we have the date time printed out with C# code inside
the view file. We can also do more C# code inside the Razor view.
Let’s add an if statement inside the code so we check a value of
x variable if it is greater than 5 we will write on the page that x
is greater than 5 otherwise we will print that it’s less than 5:
@{
int x = 6;
if(x > 5)
{
<span style=“color:red;“>X is Greater than 5</span>
}
else
{
<span style=“color:red;“>X is less than 5</span>
}
37
.NET BEGINNERS GUIDE
Notice that we can add a block of code using the @ sign along
with the curly brace so we add a code block inside the Razor view.
And we can also write html elements to the Razor page via the
C# code.
Controller
38
ASP.NET WEBSITE BASICS
39
.NET BEGINNERS GUIDE
40
ASP.NET WEBSITE BASICS
Now, how are we going to tell our view about the EmployeInfo
model so it render it’s data? To do so, we need to define our
model inside the view file:
@model EmployeeInfo;
This way we are telling the view that the model we will be using
of type Employee info. Inside the view we will be referencing.
And now we can access it’s properties via Model variable inside
the view. So, let’s add the data from the model inside our view
as follow:
41
.NET BEGINNERS GUIDE
And now inside the razor view we will make it as a list and do
42
ASP.NET WEBSITE BASICS
@model IEnumerable<EmployeeInfo>
@{
ViewData[“Title”] = “Employee List”;
}
<h1>Employee List</h1>
<br/>
<br/>
@foreach(var emp in Model)
{
<span>Employee Name:@emp.Name</span> <br/>
<span>Employee Email:@emp.Email</span> <br/>
<span>Employee Date Of Birth:@emp.DateOfBirth</s-
pan>
}
43
.NET BEGINNERS GUIDE
44