Transcripts
1. Introduction: Welcome to the course, C-sharp and dotnet six
for absolute beginners. I'm your instructor
for Vaughan Williams. I've been teaching programming
for the past ten years. In this course, you
will learn to use our programming for
desktop applications. These lessons assume
no prior knowledge of programming or the
C-sharp language, and we'll have you up
and running in no time. We will learn C-sharp syntax, variables, loops, and
this is on statements, collections like this
sender raise loops and decisions statements and
object oriented programming. By the end of this course, you
will also learn how to add your solution to
a source bunch of Management System like GitHub. Developers with knowledge
of the dotnet framework and the C-sharp language are in very high demand in this field. This course is chock-full of development tips and it
is very beginner friendly and will equip you to be unemployable dotnet developer
in any organization. Without further ado. I am happy to have you
on this journey and I can't wait for us to
start having so fun.
2. Install Visual Studio 2022 Community Edition: Hey guys. In this lesson we'll be
installing Visual Studio 2022, which ships with dotnet six, which is the fastest dotnet yet. So to get started, we just need to
go over to Visual Studio.Microsoft.com
slash downloads. And then we will
go ahead and hit the free download on
the community edition, which will then give
us an installer. Once you run that installer, we will be able to select
the workloads that we want. No workloads really referred
to the libraries or to supporting libraries
for the type of app that you're
interested in creating. In this case, we
definitely want to get that web development ESP, dotnet and web
development workload. But then if you are interested
in other technologies, you can go ahead
and select them. So you can see I have
several workloads selected with a 0 development, NodeJS and desktop development. You can go ahead and
split those also. I think that they will
come in handy with other projects that you might
end up building all in all, once you have done that, you can go ahead
and hit Install, which would be done by the
bottom right-hand corner. Of course, the
more workloads you select is the bigger
the download size. So once you've completed that, then you can continue. No one that download
is finished, you may get certain
options that I am not going to get because
I already have it installed. So I'll just walk you
through what do we expect? You may get an option where
you get to choose your theme. As you can see, I'm
using the dark theme, but then you do
have the option of a light theme, a blue theme. And the thing is
that Visual Studio 22 has many more themes
than its predecessors. So even if you don't want to
light the initial themes, you can get other themes
if you need them. Also, you may be prompted to
sign in if you already have 2019 installed on your machine and you are just
installed in 2022, you already signed in 2019, then it probably won't
prompt you to sign in again. However, if this is
your first installation of Visual Studio, you may be prompted to sign
in or create a live account, which point you would
want to just use your Microsoft
issued live at all. Look at hotmail.com at cones, or you can just go
ahead and create one. And if my memory serves regard, you can use an another non-Microsoft email address
secretes a level college. If I go ahead and create a new project and do
a new console app, C Sharp console up. Then this is just going
to be VS 2222 demo. All right, go ahead, hit next. Cc is the same thing, but here I can choose which framework and
I can now choose the latest dotnet six.net five was released last year and
it's according to 1.93.1, sit as long-term support. So it's still quite capable and each one is actually
backwards compatible. So five, most of what
you're doing, three most, if not all of what you do in 3.1 is still compatible with five. Most, if not all of
what you would've done with these two
can be forward, forward it to dotnet 6.1. It's sort of supports. I would suggest if you
have any UPS stealing 2.1 to start looking to
upgrade or update them. Or at least if you're
just learning.net Core, don't start with 2.1. When we go ahead and hit Create. Another thing I take note
of, I Pause the video. It wasn't really that quick, but I paused the video. One thing I took note of is the fact that
it is much fossa, that is probably because it
is not a 64-bit application, so it's making better use of your 64 bit machines resources than previous
versions would have. No, you're going to see
FASTA search times, faster project load times. You have solutions
with 109 projects. It shouldn't take as
long to load them up. So another thing
that I went to point out is that this
is a console app. And if you have any
experience writing typically C-sharp
obligation and you wouldn't know that there
is a standard template where you have a bunch of
using statements at the top. And then you have
int main Java class, and then you have int main, and then you have
the code in there. But here all I'm
seeing is the code. This is dotnet 66 actually got rid of
that whole template. So let me just add dotnet F5
console app project quickly. Let me just call this one demo. I'm at five so we know Next. And then this one
is done at five. So that is what we're probably accustomed to when we look at a dotnet five project are done addFive console
project or code file. You have your class, you'll
have your static void main, and then you have your code. However, in dotnet six, they got rid of the rappers, our own olive that the
just start writing code. This isn't, this is not
a C-sharp tutorial. I'm just pointing
out the two nuances. My C-sharp tutorial, I will
definitely go through all of that and the differences
between the dotnet, regular dotnet C-sharp and
what dotnet six brings to the table. But it's really cool
As far as I see it. Now another cool
feature to me is it's increased intelligence when it comes to code hinting, hinting what you might be getting its while you're
writing your code. Again, see this as
a huge productivity when they adopt the
feature in tele code. And you'll see that little
icon down here that's allows you to kind of
toggle some of the options. So if I type console, you'll see that
it's kind of auto completing dot write line. It's not waiting for me to go into the list
and then select it. I can disperse tab and then tub, as long as they are agreed. Characters to the
right of the cursor, I can press tab and then
it will just fill them in. I am here. Let me try something else. If I do something like varname, var num one is equal to and then see it's suggesting that I probably want to
initialize it to 0. Let me see if it's really
that the intelligent, okay, It's suggesting num2. What if I wanted var name? What would it suggest var name? And then a space and then okay, So I guess I broke it. But you can see what
it's getting at maybe. All right. So I guess var name is vague. As, you know, you need to
type for it to know what exactly walk ten of variable it's going to be
before you give the value. So if I said string name one, Let's see what it would suggest. Stringy and one. And also just showing, alright, so that's a feature I guess that overtime it will increase in its offering
because this is really based on cold completions, based on other code samples from various repositories
all over the place. So I guess random variable names that I desired rarely used, which is probably
why it's having such a hard time understanding
what I'm I'm getting at. But then he can't see
with the num one, num two, if figured it out. I've just press Enter. It's suggesting console
dot write line, tab. And then it's going
to suggest in the one tab right there, I just printed num
one without writing a code line of code
and look at this, I press stop once and
I'm printing num2. So I think you use it, it will seem easier, it will make better suggestions. It's clearly contextual
because he knows that num1 and num2 are in the
documents are in the program, knows that he can
suggest it later. Don't for my operations.
3. Create Your First C# Console Project: Welcome back guys.
In this lesson, we're going to be
taking a quick tour off Visual Studio 2022
while we're setting up our first project,
Visual Studio 2022. Once it launches, you will
get this little window. I am running dark mode, which is why my looks so cool, but you might be
running light or blue, which is something you may
have been able to select while you were doing the installation
and the initial steps. However, whichever
theme you're running, the layout is going to
look very similar to this. On the left, we have
recent projects. You see here that I have
collapsed my recent projects, what I have quite
a few projects. I just don't want to be
struck to with anything. I'll make you feel as
though yours is not running as smoothly as well as mine is. But once you start your
development activities, visual Studio will kind of
archive them to the left to allow you to quickly
launch them if you need to. However, We both have the opportunity to
get started so we can fill in a repository from an online repo like
GitHub or as your DevOps. We can open an existing
project or solution. We can open a local folder, and then we can go ahead
and create a new project. We can also continue
without code. I'm going to go ahead and
create a new project. You have different templates
and these templates are relative to the
workloads that you would've selected
during installation. To the left, you have
recently used templates. If you add guanine and
experimented already, then you would have
these kinds of templates listed to your left, whichever one they
experimented with. If not, it may be
empty decimal problem. The right over is where you have all of the potential templates that you could possibly have
based on your workloads. Once again, you can
also search and filter. So you see I have a lot here. I don't know where some of
them following in the list. I can always type in the one that I'm
interested in October, and it will filter
this list accordingly. When selecting your template, you have a number of options. You have the language that you want the template to be in. This is a C-sharp poor, so we're going to be focusing
on C-sharp B as templates. Then we have all platforms
or Android or as zeros. You can filter our
according to that. You can also filter according
to the project types. So weirdly, in console programming in
this particular module, I could easily see a
C-Sharp on console, and then it would
only produce me the templates that are
conducive to these two options. Otherwise, with these filters, of course I can scroll through. If you don't find the template that you're looking
for, let's see here. You get this far and you're trying to find C Sharp
console and it's not there. That means you probably
didn't install the workload. So you can always
go to the end of the list and you can
install more tools and features which would
actually just relaunch the installer that allowed
you to select your workloads. You select the missing one, which would be for
desktop development. Hello Visual Studio
to modify itself. And then the next
time you come here, you should be fine. Let us get started with
a C Sharp console app. So notice you have
C Sharp console up, you also have VB console. Absolutely very
careful to note off the language that is being
used were not using Vizio OBC. If you'll want to be
sure you choose C sharp. Then when I did console, you would've seen two
different options, C14 dotnet framework
versus the dotnet Core. So we're actually going to
be focusing on dotnet Core because.net Framework
is older technology. That's when it was Windows
only since dotnet Core, not in it has
become open source. So we wanted to focus on the more recent
advancements with NOT IN IT development and
we've using dotnet Core. We'll choose the console
app that can run on dotnet Core that can run
across all the platforms. Remember, I didn't
mention that this is an old cross platform, a slit that just
double-click it. And then we will be faced
with the menu, sorry, the wizard option to put
in our project name, choose a location and
select a solution. I'm going to choose for the
project name. Very simple. Hello World. That is usually the
first program that anybody ever writes in
any programming language. Helloworld. Notice that by changing
the project name, it's automatically change
the solution name, but I don't necessarily want the solution to have the
same name as a project. So I'm going to choose this one. I'm going to name this
console programming. The relevance of this is that
the solution to a project, sorry, a solution to a problem. When you're developing
an application, you are developing a
solution to a problem. Solution. The solution may have a name, but it has different projects, which is something
that we will be doing. So we're going to
have one solution that has many projects. So that is why I'm naming
them differently even though by default it would have tried to give them
the same name. I'm showing you that
you don't necessarily have to because
there are times when you have a solution
and then you have different projects
with different names on the same solution. Project name. Helloworld. If you want
to change your location, you may do that,
but I'm going to leave my default location. And then the solution name
is console programming. Then I'm going to hit Next. It is optional to please a solution on the project
in the same directory. I'll leave that
until we hit Next. Then we have to choose the framework version
that we're interested in. So we have dotnet
Core two-point one, which it is very clearly
telling you is auto support. So my suggestion is
don't start there. If you're already doing some
development and you have a quart 2.1 obligation out. So just say try to upgrade
it as soon as possible. But that is auto support.net Core three-point
one that will be in support until I think December 2020 to this
dotnet five was like an intermediary
between 3.16 and it is going out of support in a
bowl me off the same year. But dotnet six houses
long-term support and that will be in use until the 2024
or thereabout. It will be in use
for a few years. I would suggest you
just start with Dutton at six as that has the longest bit of longevity out of all of these versions. That being said, however, with just a few tweaks between each version off the
dotnet frameworks, each version of C-sharp has
incrementally improved. And most of what you can do in 3.1 is also possible
in dotnet six. But then dotnet seeks has also a refined certain processes and the code layouts that are not visible in
previous versions. So we're going to do
a quick comparison just to make sure that you can appreciate the difference under nuances between the versions. I went to start off
with dotnet six. Then we can go ahead
and hit Create. That is the end result of Oscar editing our project files. One file really in the
Solution Explorer, and a file called
program.cs with one line of code or comment
on one line of code. I'm going to pause from, From this point we just looked at all
we create a project, it's somebody we're going to do a lot more going
ahead, going forward. So don't worry if you didn't complete all of
the steps to memory. That was a lot of information, but we'd be doing it again
for normal, went to pause. And when we come back,
we're going to take a tour of visual Studio, this IDE, what it is capable of, and how it will help us with
our development pursuits.
4. Visual Studio 2022 Community Edition Tour: All right, In this lesson
we're taking a look at Visual Studio 2020 to
the user interface, how everything is connected, how it works, so we
use it in general. First of all, you'll notice that my Solution
Explorer is to the left. Yours by default is
probably to the right. You can actually take
these what should I say, toolbars and move
them all around. If you have a different vision for whole Visual Studio
is laid out for you. Personally, I liked is the
Solution Explorer to the left. So I actually can just
drag it over and docket. You'll notice that these
are the squares up here. When you drop it into square, it will show you
where it will appear. Should you release
your pointing device. If you drop it in the center, then it becomes another tub. I don't necessarily
want to be writing code and enough to tab over here to change a file and
then come back. I don't like that, so
I like to have it to the side and in my case
to the left-hand side. And then I don't want it so
wide either I can always make it smaller or
larger accordingly. Of course, it affects so much real estate you have for your code editor section. You can experiment with that. You'll also see other toolbars on my screen that you may
not necessarily have. Any toolbar that
you're interested in. You can always go to View
and you can see it's if you accidentally close your
solution explorer, then you can always go to view and you'll see your
Solution Explorer, you click it, it reappears. You can pin it on
peanut or hide it. So if I wanted hidden, but then when I hover
or click one time it appears and then I use it and then I continue doing
what I wanted to do. That's another way to keep it
on the screen, so to speak. But have more real estate
for writing your code. I'm going to have it docked permanently because of a beginner screen
that I can use it, but you may not
necessarily have that, which is why I'm showing you
how you can alter hide also. I'm just going to focus on the
things that are absolutely necessary for us to get
through console programming, at least for now as we go along, we will see more
and more things. Visual Studio has
different themes like I would've mentioned. You'll see here I'm
using the dark theme. You can always go to Tools, theme and choose the theme that you like the most or you
can get more themes. And when you get to
a certain level, you can even design
your own themes. So if you started off
with a light theme, then your Visual
Studio is probably going to look more like this. I don't like that. That is harsh on my eyes. If you chose blue, then I think it just changes
the minute items, right? So this is the classic
Visual Studio look from Visual Studio 20052008. Is it kinda looks like this
or more like 2012 actually. And then you can also, like I said, you can
choose other ones. So I've got one off the
marketplace called Nordic that kind of has this
grayish, bluish hue. I'm just showing you options. Once again, you can always get more themes if you
use system sitting, my system is set to dark
theme in my windows. So it's just inheriting that
from my Windows machine. Once you're comfortable with whole year Visual Studio looks, then you can proceed. In terms of color coding. This is our editor, and it's really
just a text editor. If you've ever done
any code before, then it's a text editor if
you've never written code before and this is your
first coding course, that's no problem. That is not a problem. This is just a text editor. Think of it like Notepad. So the only difference though, is that we're going
to be writing specific syntax to
accomplish specific things. So the color-coding are, you'll notice that
certain itself code have different
colors and they really signify their role in the whole experience
writing code. In the dark theme, at least you'll notice that console has that
kind of teal look. I'm colorblind, so forgive me. My clothes are off. But that console
has that T-loop. So Console is what
we'll call a class that is always going to color code
anything that is a class. Then you'll notice
right line is white. Functions. This is a function or a method
that is going to be white. And methods are usually
characterized by open and close parentheses. And then every line, or 90% of the lines that we will write at least
and with a semicolon. So that semicolon is
very, very important. No, notice if I missed off
that semicolon or I misspell something and Visual
Studio cannot figure out what I am referring to. It will give you that
red squiggly line. And if use Microsoft word, then that is something
you are familiar with. This is going to
indicate that there is some error with the
code that I've written because the syntax as the meat very specific rules in order to be accepted by our editor. Once the editor detects that
there's something wrong with the syntax or
what we have written. It is going to give us
those that are red lines. Sometimes these lines
are the arrows. If you hover over the line, it will tell you
what the error is. Sometimes it cannot tell
you what the error is Alt, rightly right here again, to me that the semicolon
is expected, okay? If I put the semicolon, that one goes away. But in this one it doesn't
know what we're right. It is it knows right. But it might not be
able to determine that lie is just a misspelled
version of variety. That is where it may failure. So you can always hover. And then you can click
on this light bulb, which will try to give
you some suggestions. Here, Visual Studio,
especially in 2022, has become so
intelligent that it can actually contextually see this time that it is probably
just a typo change right? To right. And I knew
if I click that, it will meet the
courage on for me. And then that line goes away. That this all Visual Studio is actively helping you
to write better code. If I made a bigger
mistake than that though, something like this. No, I don't know
what it can do in this situation to
help me because it's just going to say
this does not exist, it doesn't know what this is. It's suggesting an error. This one is still seeing. This is a variable
that was never used. There is no variable there
with that name, etc. I'm just showing
you that there are some areas that can help you with and sometimes it's just not going to
know what to do. So from time to time, you mean need to
end up googling. You know what your error is. Are you just erase everything and tried
to write it again from scratch and see whole visa
aesthetic and help you. Now another thing that I did
not highlight was the fact that we're getting
these cold hidden team. By going to the next line, Visual Studio is making an assumption that maybe I
wanted to do another line. So by pressing Tab, I can actually accept that
grade old block of text. What I intend to do if
I wanted to console. But this time, you see I
made a mistake console dot. And then you'll see
here when you do a dot, you'll actually get this
thing called intelligence, suggesting potential methods or properties that you can use to complete
what you're doing. All right, So sometimes, I mean, you're not going to memorize every single method
to just write. It's their time zone,
you just won't. So once you do the dots, Visual Studio is going
to make suggestions and see maybe you
want buffer width, maybe you want to read
line and he started typing and it's going to filter them as it Yes, I want read line. And then you can use that. Another thing to notice on this point is that if you
didn't choose that suggestion, something happened,
you clicked away. And then the
suggestions when till we get the MAC is control and space from the full stop are if you're typing it
will suggest it to you. But let's say you press the
Escape or you clicked away and you don't even have that autocomplete line
available to you. You want to know what
your options are. You can actually
do control space and then you will
see your options. And I'm going to be honest, I've been doing this for
years and dear times when I don't know what
needs to come next, there are times when I Control
Space and I scroll through onto something looks like
what I'm looking for. And I'm sure a lot of experience C-sharp developers will tell you the same thing. So I'm just saying that
to say there's no shame in getting this far and not
necessarily being sure, what do you want to do next. That's why Visual Studio is
designed this way to give you that IntelliSense to assist
you so you can be scrolling. And then he said, I
really want to write something or I wanted
to take inputs. I want to read
something, AAC read, read, read, read light. Maybe let's try read line. And then he can use your tab and an autocomplete that, right? I mean, I'm not able to show
you every single nuance and everything about Visual
Studio in 15 minute video. There are things
that you would just speak up as equal along. And as we write code together, I will be pointing
certain things old. I will always show your
keyboard shortcuts. So, you know, like writing a comment in C-Sharp
would be slash, slash or slash asterisk
and an asterisk slash. And this one is, allows you to do multiple lines of comments. All right, so if you need
a threat to a paragraph, you would use this one. If you wanted just one line of comments, you'd use that one. Because the next line
wouldn't be commented. Keyboard Sharp code for it. That would be Control key and see if you have line of code
that you no longer are. You don't want it to execute. You want to come in to told
you hold on, uncontrol, you press key and then you press C while still holding
down on control. If you want to uncomment it, it will be Control key. You, you're holding down Control and impressive
letters K and C to comment, and then to uncomment. Holding down Control. You press key and you, those are little things that I'm going to be
showing you if you want to duplicate this and
you do Control and D. All right, our control CV, which is really a
copy and paste, but you notice I didn't
highlight the line and say Copy then
piece I just had the cursor there and then holding down
Control C and then v, and then it just duplicated it. Control D may work for you based on your
development settings. It mean not to
have had burst and see those not work for them, but control CV generally
works in that way. For the final bit of the store, I'm going to show you how you
execute your application. So at this point we have some
code that is supposed to print to console or where
doing console programming. Console programming because
at the most basic level, in most languages that
are designed for desktop, you're going to have a console. So when we say console, we mean like command line. If you've ever done come
on line on your machine, what it's Windows, Linux or Mac. It would look
something like this. This is your console. So when we say console
dot write and I, and we're actually calling the underlying
functionality that uses this command prompt window. And seeing that I
wanted to print whatever text I
have outlined here. This window. When
you're going to be testing your application
in Visual Studio, you have the option
to either run it, and this is using debugging. Run it without debugging the keyboard sharp
bullets would be F5 to mimic debugging and control, plus F5 to mimic no debugging. Later on, we'll look at
what debugging means. The general net
effect is that when you press these either one
of these play buttons, it should execute the code
that you have written. And it should pop up
on screen and showing you what the alt come
off your code is, whether or not
it's desirable now is where you
programming comes in, you make your
modifications and you can get it to the point
you want it to be. But to run it, you're just going to press the Start button. And then it starts
debugging CDC, the output window is coming up and is give me all
sorts of stats. And then you get
that console window that looks just like
a command prompt. And then you get your
line HelloWorld. And he's telling
you that the code executed from this location. That's an executable file. All right, so any installer, any application you run is
usually an executable file. And then it says it will
automatically, well, they're giving you the option
to automatically close it, but we don't
necessarily want that. We can just press any key
to close this window. So if I press enter, it will just close the window and return us to Visual Studio. And that's really it. So that was a very simple
Hello World application given to us by Visual Studio. So yes, the project was
called Hello World. But the code was generated for us not because
the project was called that. Before I leave though, let me give you a little tour
of the Solution Explorer, even though I'm going to
expand on this a bit more, here is the name
of the solution. So remember I said
that we had a solution different from
projects or projects. So we have the solution
called console programming. And then we have the first
project called helloworld, which one-click will reveal an XML file that allows us
to change certain things. We're not getting
into that right now. Then we have dependencies. You don't have to worry
about that, right? No. And then we have one
file called program.cs, which is our class
file hosing over cold. We cannot multiple
files, of course, in our project, which we will be looking at as we go along. So that's really
it for the tour, the general tour
of visual Studio, what it brings to the table
and will help you to write clean and wonderfully
executable code. When we come back, we're going to look at the
difference between a dotnet six project and the
previous versions of dotnet.
5. Understand the Difference between .NET 6 and Previous Versions: Alright guys, so wonder we
were here the last time we created our first console
programming project, we call it helloworld. And we explored some code. We also explored Visual
Studio on a whole. And what I wanted to
do is just explore the difference between this
console programming template and the ones that we would have gotten from dotnet five or 3.1 or even dotnet framework
if we were to use it. Simple, simple way to get a new project is
directly core solution. Alright, so remember
Solution Explorer. We have the solution up
top and it's characterized bed at purple ribbon
inside that window. And then the project has
the badge that gives us the language that
this project was written eaten or is
supposed to be written in. So we can right-click
the solution. Then we go to Add. Then we say new project. Then this screen should
look very familiar to you. This is the same screen that we use to add the first project. We can choose console up. And because I have
on my filters, I'm seeing it up
close and personal, but if I just clear, then I will get bumped medalist, I'm going to go
with a console app. Then this one I'm going
to call it hello world. No one suggestion when
naming your projects. Don't use spaces. I suggest that you
don't use spaces. That's one. All right. Don't use spaces in the
name of the project and camelCase your words. You'd notice that my Hello
has capital H and the world as capital W. For
readability purposes, it's one word, yes, but at least with
the capital letters, you can see where one word
stops and another one starts in the contiguous
block off characters. I would also suggest
that you stay away from special characters. That actually is the
only one I really use is an underscore in
naming projects. And these are more suggestions because later on
you can end up with some issues that other platforms don't like, dashes and stuff. You can use an underscore and sometimes you'll see
people use a dot. Not just perfectly
fine off course. So I can say helloworld dot
and I'll call this net five, that we know that this is the Hello World application and its relative
to dotnet files. So that's why I named
me is very important. There's no indication
that this one is done at six really. But because the whole course is built our own dotnet seeks not going to be specific about all the projects
being done at six. I'm only highlighting this one because this is the outlier. Helloworld.net five. And then we click Next. And then this one, I'm
going to make sure it knows that I'm using dotnet F5. Then I'm going to hit Create. Alright, so now our
new project is, and if you look at it, you'll see that we have two projects. If I collapse them, you'll see them individually. You'll see the HelloWorld and you'll see helloworld.net F5. If I explained Hello World, you see the HelloWorld files. If I expand HelloWorld, done it five, you
see the five files. Notice that the tool
projects at least at the base of our identical
in terms of what you get. You get the program.cs file. Let us compare these
program.cs files. We already established that
this is what the dotnet six program.cs file looks like. Nice, clean, literally one line, we added that additional land. Well, let me go back
to the defaults. That's what we got
to hold off the box. Now look at the dotnet F5 file. There's a lot more
going on here. Well, what do you call
a using statement? So I using statement
is a statement that says I need to use this
library on your system. So every Windows machine, every Markov or Linux machine, they have certain libraries, are certain compiled bits of code that are available
at the system level. That when you're
writing an application, you can actually write
an application to access them on any machine. When we say using system, it means that any machine that it's going
to be running on, please try and access
that library calls. This system library is what is important for our
console up to run, which is why I said it
uses the command prompt. When we include that, then we have access
to certain functions, are certain bits of code that lowest access by
itself, the system. You'll see this one
called namespace. No, namespace is the
code representation of the folder that the
file is located in. So simply put, every project
that appears on the disk, Project Explorer is really a
folder in our file system. All right, literally
it's a folder. So if I right-click
this and I see open, sorry I missed there. If I right-click and I say
open folder in File Explorer, it will literally
launch a file explorer showing that sense of files, all the files involved
in the program. If I go up, That's
console programming. Console programming,
that is one, that is the name of the
solution, That's one folder. Then you have that folder, okay, nothing is in there. We also have hello
world. Remember we. Screw it to that project. In the previous lesson, we have hello world, dotnet F5. That's another project. I'm just showing you that. That's all the
namespace really is. It's just the name
of the folder. So if you have subfolders and input files in those
sub-folders and the namespace would
automatically be fuller dot, dot, dot, dot for the whole
hierarchy of folders window. Next up, you'll
notice that it has class declaration called
internal class program. And it says open and
close curly braces. And we have a static
void main function, which then it goes down to print our helloworld
line of code. So the only real thing that these two have
in common is that they're both using console
dot read line to print. Know what the engineers at Microsoft did for
dotnet six was reduced a lot of the
repetition because in all the versions of.net
Framework up onto the net five, this is what the code
would look like. At the very least, by default, it's always starts off
with that glass and it always starts off with
that mean function. What they did was the
Echo Dot repetition. So everybody knows that these two are required
at the very minimum. So they said, Well, let's
just make it implied. We don't want that
console needs to use system so we can meet
that's implied also. And we just get straight to the coding non-points
in the soul because based on the version
that you are able to use goes down and it's six is limited to Visual Studio 2022. Well, if you're using twin
to 19 for whatever reason, and you have to use done it
five instead of done it six, then this is what your
code will look like. But everything that
we're going to be doing for dotnet seeks, it is very possible
for a dominant fire. Just remembered that all of your code is being
written inside off the mean function and everything operates
her own main function. If you have to go to
another function, then you do that
inside of the class. If you'd have to go
to another class, you have to do it
inside the namespace. I don't want to
confuse you guys. I just wanted to show you what the older C-sharp
templates look like versus the newer C-sharp templates
and help you to appreciate why one looks the way it does and why the other
looks another way? No, earlier we had clicked to, with one click on the project, were able to see this XML file. Here it's that the unit at
the output type is dot EXE. That target framework is dotnet six and implicit new
usings are enabled. We'll look at that later on, and it is enabling no level. We'll also look
at that later on. If you look at the dotnet five, you'll see that it has fewer
configuration parameters, but it is just saying that
the target framework is 5. Still the same output tag. If you do dotnet Core
three-point one project, it would look just like
this project template, code template, except
I think this would probably be public
instead of internal. We look at the difference
between those later on and the XML file, the project configuration file would look slightly
different also. However, like I said, we will
be focusing on dotnet six. So I went to leave the dotnet five project
there for reference. But we will be focusing on
dotnet six going forward.
6. Development Using Visual Studio Code: Hey guys, In this lesson
we're going to be looking at an alternative to
Visual Studio 2022. So you may need an alternative
for a number of reasons, Visual Studio is
really created for Windows and Mac OS machines. And your machine in
general just might not be doing a good job
off running Visual Studio, which is okay because it is
a very huge application that requires certain resources that not every machine can
regularly provide. That's fine. So
Microsoft has given us a nice lightweight alternative
that is cross-platform, meaning it runs on every single operating system and it is very easy
to set up and use, and this is Visual Studio Code. Know the first thing, there are two things
that you need. One Visual Studio Code, but you also need to manually install the dotnet six runtime. So you can get to that by doing a simple
Google search.net six Runtime or ASP.net
Core six ruined time, whichever one, then that
will allow you to install the SDK on any one of the operating
systems that you have. Once you do this, it will install the binaries
in your system and you'll be able to use dotnet six. Now if you have Visual
Studio installed already, it came packaged with it. So you don't necessarily
have to do this step again. Alright? Once you have the
runtime, of course, you go ahead and download
Visual Studio Code. You go ahead and
install whichever one is suitable for your
operating system. No-one's Visual Studio
Code is installed. You'll get a simple
enough window that you can increase or decrease display off by using Control Plus or Control
minus on your keyboard. No, from here, you'd want to
set up your project space. What I like to do is create a new folder and then
use that folder, my operation spot, so to speak. So I went to open folder. And by opening folder it will ask me which
folder I want open, navigate to where
I store projects. Then in this projects folder, I'm going to create
a new folder. I'm going to call the
console programming just to stay in line with all that
we've discussed before. Console programming, and
then select that folder. Once I have this folder, I'm going to want to save
it as a workspace also. So I can just see a file
and Save Workspace As. And then I'll just leave this as maybe console, console
programming workspace. That I know that once
I open that file, it will open up this folder with all the projects and
everything in there. I can see if that outside
of having the workspace, I would want to have
folders per project, just the same way
that Visual Studio, each with each project
that we created, give us a new folder
and then you wait to IRange the related files
to a certain project. I'm going to create a new folder instead of this workspace. And I'm going to call it, let's say helloworld
dot VS Code. Spelled incorrectly. I can always
right-click and rename. There we go. Hello world. Yes, quote, know, to create
the project in there, unlike Visual Studio, gives us a nice wizard and a
visual user interface. I have to use commands. I can go to determiner, bringing up a new terminal. And then inside of this
terminal after make sure that I am pointing to one, the card folder and then
to run certain commands. So the change folder, what you want to do is say cd and then start typing the
name of the folder you want. So I want hello, world, I can press Tab and it will
autocomplete that for me. Press Enter. No, I am looking
at the folder helloworld. That path. No much is the folder
that I'm targeting. Mixed. I want to actually
create the project. So I can say dotnet
just by writing.net, you can see if you have the runtime or the SDK
installed properly. If you don't get something
looking like this, then you probably need to go and try and install it again. But I can say
dotnet, new console. So I'm seeing dotnet, the cape. Please give me a new
console project. And just by pressing
Enter is going to fill out this folder that I targeted with files that are conducive to doing
console programming. When I click program.cs, which is a C-Sharp file, I get my one-liner, just like all we saw, that program.cs
in Visual Studio. You may also get a pop-up saying that you need
additional assets. You can say yes. Now another thing that
is very important for your experience here
in Visual Studio Code, extensions for
Caesar programming, you may have gotten
a pop-up saying. They suggest that extension if you didn't get
to, that's fine. If you go to Extensions, then you search for a C sharp. You will see at the top, C-sharp for Visual Studio
Code poured by Omni Sharp. Okay, that's nice to know. But the relevant
part is that you want to definitely
install this so they get some code hinting on some intelligence
to help you with your C-Sharp programming in
diverse in this interface. So you would see on
Install button like this when you click
on that extension. So you can go ahead
and install it. And once you have it
installed, you can proceed. Now to execute your application, what you'd want to
see is dotnet run. Say dotnet run. It was start compiling
and then it will show you the output or the results
instead of the terminals. So the cool thing about
determinant is giving you direct access to the
same kind of console that would pop up every time
we run an application in Visual Studio without going externally in launching
a whole other console, you can do everything right
here in this interface. You can execute,
you can go and run. You can start to
debugging if you wish, which would give you a
slightly different experience if you start at the debugging. But the point is that you would actually see
everything inside of Visual Studio code without actually needing to launch
an external window. Of course, every detail
has its pros and cons. This one is nice and lightweight
and you can see that it's brings us slightly
different flavor to the whole
development experience. But that being said, everything that we're
going to be doing in Visual Studio is
actually repartee, replicatable, replicable,
whatever that word would be. But it can be mimicked in Visual Studio Code
to help you to have a very similar experience. The last thing I'm going to
show you guys is how you can manage multiple projects
inside of this workspace. So let us think of our console programming
workspace as our solution. And then we have
different folders that represent each project. Inside of this console
programming folder, I would want to
create a new project. Let's call this one hello world, just like what we did
in Visual Studio. Hello world, dotnet f5. Let me get my spelling right. There we go. Then if I wanted to create a project instead
of this folder, I have to make sure I'm
pointing to that folder. So I have to say
something like CB. And then I went to
see a dot slash, dot slash sawed
off the Goldbach. Because look at it. The path is HelloWorld VS Code to see cd dot dot to go back
into console programming. So the NCI CD and then start
typing Hello World and press Tab until I see the
folder that I'm interested in, which is.net five, press Enter. And then I can say
dotnet new console. And just by using the
up and down arrow keys, I can go through recently or
previously typed commands. So I have to say dotnet new, let me increase the system, makes sure that you are seeing clearly dotnet new console. But when I went to
specify the version, I have to say dash F net F5. So this would only work
if you have the dotnet F5 SDK already installed, which you probably do if
you install Visual Studio. If you install the SDK, manually the nail
after, go back and get that particular version. But when I press Enter, notice inside of the donut five, I'm getting that
new project as well as the project templates, the code template for
HelloWorld and done at five. That is essentially whole
Visual Studio code would work. What you can do is
for each budget that we will be creating
in Visual Studio, just create a new folder, run that command, and then you can start coding accordingly.
7. Section Review: All right guys, we've
come to the end of setting up our
development environment. At the very least, four
are in diverse a hit, so forth, C-sharp
console programming. We have two tools
at our disposal. We have Visual Studio 2022. We're using the
Community Edition, and we see here that it is a verbal full tool
that Microsoft has invested a lot in to make sure
that as dotnet developers, we have a fair, comfortable workspace to use. And it is going to help us to write some clean
and Paul full code. We looked at how we
can rearrange it for your own comfort by moving
these Toolbar sections, the different parts, looking at the different
themes whole weekend, it executes a program. We can add multiple projects to a single solution and
everything like that. Outside of that, we also looked at using
Visual Studio Code. Visual Studio is not an option for you
whether it's because of your OS or your system
specifications. Visual Studio Code is a
very, very lightweight, capable, and perfect alternative
to using Visual Studio. There are other
editors out there, but those generally cost money. So I'm just showing you all
of your best options without having to spend much or
if any money upfront. Visual Studio Code is a nice lightweight editor
that allows us to, it gets our projects created with a few simple commands in a built-in terminal and get to writing some
code with all of that, sit and don't stick around. We're going to start
focusing more on the C-sharp language
as we go forward. And we will be learning how to build some beautiful
applications.
8. Understand Basic C# Syntax: All right guys, so in this
lesson we're going to take a step back and look at how we can continue our
development in diverse by launching an existing project and the proceeding
with programming? No, I hadn't mentioned
from when we're doing the setup that to the left
you'll see recent projects. So here are a bunch of
my recent projects. But here is the one that
I wanted to focus on, which is the one that we
have created together. If you shut down your Visual Studio,
shut down your machine, you went to have dinner,
whatever it is, familiar buck, you want to jump
back into a project that you're working on or a
solution you're working on, then you would see
it on the left. Most recent projects know
there might be a time where you work on so many that you don't
see listed there, you can always go to open a local folder or open a
project or solution rather. So you'd want to open
our project or solution. So let us state that
route because this one, it says easy as clicking
it and it will bring up Visual Studio
with the solution. If it's not there, then you want to go to open
a project or solution, which will then
allow you to burrows to where the project might be. So you can actually just
browse through until you see the project or the
solution folder name. And then you will
see this file here, which is a solution file. I'm Solution files. Any FCS approach for us, we don't see in the
CS project file from Visual Studio Code. It's a CFI that one we
click it in Visual Studio brought up that
XML configuration. The solution file would launch
Visual Studio with all of the projects and files
associated with the solution. Generally speaking, the name of that file will be the same name. They would see as
the folder name. If you double-click that, it will actually do
the same thing and launch Visual Studio
with that solution. Now that seems solution file can be found from
your file system if you were to navigate
there and you can, I can easily navigate
by right-click and say Open File, Open Folder. So if I was to navigate here
manually, here's that file. If I double-click that file, it launches Visual
Studio with my projects. I'm just showing
you how easy it is to resume where you
would have lift off. Knowing this part of the
lessons we're going to be focusing on C-sharp
language, the grammar, vocabulary, and general beats off syntax that you
would use to write code. We're not going to learn
the entire language here and I'm not
going to be div, being too deeply
into certain things. I'm just giving you
a broad overview of what C-sharp looks like. C-sharp is the premier programming language
given to us by Microsoft for the.net platform. It has a close resemblance
with C plus plus RC language. So it's a part of the C
language is kind of botch. It also takes a lot of
its constructs from other object oriented
programming languages like Java. If you're alert, if
you've learned C, and if you've learned Java, than a lot of what
C-sharp is doing will seem familiar if you've
learned neither. And this is your first
time doing programming. Once again, no problem. I'm going from the very basics, so you'll be able to catch on. Now C-Sharp on this
point is at version ten, he does gone through
many versions with each iteration of the.net
framework into dotnet Core. And I can just delete, keeps on getting
better and better. But one thing that
I must come in Microsoft with is
their ability to update the language while
keeping certain older regimes, arrangements or paradigms alive. So even though it may introduce a new way to
do something there, all the way off writing it in older versions of C Sharp
won't get deprecated. It's not like it's
going to say, oh, that can't work anymore,
you have to change it. So they do an excellent
job of upgrading. Destroying older code. C sharp is also open
source and you can go to their project repository on GitHub by going to github.com slash.net slash C-sharp lungs. Open source means
that everything that you are doing the have
made it available. All of the underlying code that they have written represent our owned that you will be using. They give you the shoe, the implementations
and everything you can go in and you see
exactly how it works. You can even make
contributions here. You see here that it
does 125 contributors. Sure, not all of those are
direct Microsoft employees. So as you get more
and more comfortable, we can start looking
at this library, looking at how certain
things are done and seeing if you can improve them. Also. Know earlier we were looking at certain things that are illegal. There are certain
things that you just cannot do in the syntax. There are certain Tolkien's that server specific purposes. So like a hashtag or a
number sign or pound sign has the very specific rule of opening one-off these
I kind of just say pond. I can say Poland error. Sure. When I can say something
that I version, notice that I'm getting this
red line on the version. It does because it
is not something that is in the vocabulary. So just like with English or any other language
that you would speak, there are certain words that are just not in the vocabulary. Words that you don't use because they're not
apart of the language, is the same way when we
are writing C Sharp or any other programming
language for that matter. If the language does not detect what you are writing
as part of its syntax, then you will get that
kind of red line. Let's us start off
with what a comment. A comment is a good way to document what is happening
in your cold read. So we can write a comment
by using slash slash. And then we can see this
line prints hello world. So this is a good way
of informing what won, reminding yourself
what the code does or to informing somebody else
what it's supposed to do. If you have a whole paragraph
that you want to write. So you can see
slash and asterisk. Notice that everything after the slash and asterisk
became green. So that's just the color coding to show you that it's a comment. But then you can close it with another asterisk
and another slash. I didn't want you to do that. Anything that you read
in-between the asterisks would be seen as a comment. This is a whole park off
comment in multiple lines. So this is how you can write
like a whole paragraph describing what is happening
in a whole block of code. So this is for one
line at a time, this supports multiple lines. We also have what you
call a statements. A statement would be an expression that
ends with a semicolon. So in English or most
other languages, statement is usually the
sentence also on comprises of words that usually has a
specific order or grumbles. So in English you would say the adjective before the known. In Spanish, you see the
known then the adjective. But the fact is
that That's hold. A statement is written in
that particular language. In C-Sharp, this is
whole statement is written and you should
always end with a semicolon, even though in another language you'd end with a full stop. If you have a statement
declaring a variable, for example, and I'll
explain what a variable is. Well, let's say
var name is equal to variable is a temporary
storage space for detail. And then I can see my name. Is this. That's a statement that
red line is saying, Well, you need to end your
statement appropriately, so I need a semicolon. So one way to know when
you need a semicolon is that if you
write the statement and you see that red line, chances are you need a semicolon at the end of it if
everything else looks okay, but you still get the red line. Check and see if you
need a semicolon. Now there's another
construct called a block. A block would be similar
to what we saw in the C-sharp five template, and it is denoted by open
and close curly braces. So these are blocks. A block would be
like a paragraph in our written human language. Blocks can be namespace, it can be for our class. You can also be for method. I didn't mean it as you go in, you get blocks for
if statements and repetition statements outside of the ones they examples
here with Aeneas's, the class and the method. If I was to do like
an if statement, I would say if name. Don't worry about this. I'm going to go
through all of this. I'm just showing
syntax once again. So here's a comparison. If name is equivalent to. And then here it's showing me
that I'm missing something. So this is one of those times
it may suggest a semicolon, but this is not a statement. A semicolon is not
what is missing. So I need to write
the cart syntax. There are times
when you may say, why do I have to
do it like that. I generally say don't ask why if the language says this is how you do it, just
do it that way. Sometimes when we get
fixated on the why it blocks all the fact that
that's just the way it is. I'm going to say if name
is equal to something, obviously this is not true. But I'm going to just
started up a new block. This is saying if this is true, then I want to execute
this block of code that is denoted once again
by our curly braces. No. Do you remember that we have to import what
we call namespaces. So I may have said libraries, but this is a library,
it's a namespace. A namespace is really a
collection of classes. C, namespace has one class
equal to multiple classes. That is a namespace and the ones who compile
that namespace, it becomes multi
color libraries. The different libraries of different classes that
you're taking advantage of. Soul console is a
class that is found in the system namespace or library. No one, C-sharp six, you can always add, you're using steam and
soap top using system. You can always do that. And as you go along, you may need to know
it's grayed out because it's already
implied it's unnecessary. But I have System.out and then you have
different things here. So you could say
System.out detail, you could see this
and that whatever. The fact is that if
you're not actively using our class are coded reference
phoned in the namespace. Then Visual Studio kind of gray told actually that
it's not relevant. Now there are times
when you have certain namespaces that you want to use across
multiple files. Now in the older
versions of C-sharp, you'd have to actually repeat
that every single time. So for every class that I'm
going to use in console in, I would have to have
that using system, using system, using a system. In C-Sharp six, we are afforded the opportunity to add what
we call a global usings file. So I can actually just
create a new file. And I went to call
it global usings. So right-click on the project. Notice multiple projects,
but I wanted to inside of our C-sharp
seeks HelloWorld project. Right-click that
project, go to Add. And then I can say new item. And then new item
allows me to add a new class file or
other types of files. So this time I'm focusing on
our new C-Sharp class file. I'm calling it global
using this dot cs, and then I can add it. And then from here, I don't need all of these are I don't need
all of that stuff. I can clean it off that. But no, I can add all the using statements
that I know will be globally used throughout
my application to make it. So I will just say
global in front of it. So just say global using global, using global using no, I don't have to manually add these using statements
in 51015 files. I just have one please. And then I can just
keep my code file clean and dedicated to my code. Now, let's bring him back a
little closer to home and look at some of
the best practices when we were just
writing our code. One thing is indentation. When we have blocks of code, we want to make sure we're doing indentation because
you don't want to be having everything on one line and then you
don't know where one curly brace opens
and closes, right? So it will be always good. Anytime we have a block of code, make sure you indent. Another thing is
that applications in every programming
language goal by three control structures. We have sequence. Then we have decisions not necessarily in
that particular order, but we have sequence,
we have decisions. And then we have repetition. Sequence refers to
the fact that it will always go in the order that
you've written the code. Just know you saw me
take these two lines of code and put them
inside each Stevens and then we got an error. Why? Because I am checking for
a variable called name, which does not yet exist. The only way I can write an
if statement against this is if I already made it exist. So that's the sequence I
have to make sure it exists, then I can do
processes against it. Alright, so that's a sequence. It will always run
from the first line. So the last line in
that exact order is good when you're working
on your algorithm or the flow of your application, the logic that you will see, I need this to happen then that, then that, then that. And then you write the
code in that exact order. Step one, step two,
step three, etcetera. No decision is also called
a conditional statement. It means that I'm going to take this action should this
condition be true? So if this condition holds true and we'll be looking at
the different conditions and how we can write them. But if this condition is true, then I wanted to
get this auction. Whatever code is inside of this block would
be like the thin. Because if we were
seeing that in English, we would say if it is cloudy, then I wanted to do
with this action. Then he would say something
like else, do another action. That's all we think
as human beings. And that is exactly a whole. We have to write
our applications. So after say, if
that holds true, then inside of that code block, I defend what I'm going to do. And then I can see else,
do something else. We'll be looking at that later. And I'm just once again
giving you an idea of what we mean by decision or
conditional statements. The next one would
be repetition. So if you have something
to do multiple times that maybe print hello
world 50 times, you do have the option to
just write that 50 times, which of course is
not very sustainable. Or you can do a repetition. So you could do
something like loop. And I'll just do
a quick for-loop here to explain the syntax. Yet, I'm just showing
you what you would say for some number of iterations. All of these just define only to see a whole many
times you want it to happen. Do this action once again, you can see it's in a block. The reuptake, the inside of
that block would be repeated for whatever number of
times you've defined here. Alright? So those are some key components to writing programs in general. But then C-sharp syntax, of course, it makes
it very specific. Wholly go through
using Visual Studio. I'm not going to get too
much into all of the syntax. What I will do is leave some comments just so we can remember what
was discussed here. And I'm just going to
erase anything that is not completely important to this
Hello World application. And I'm just going
to say there are three control structures
in C-Sharp programming. Sequence. And I think sequence
is the most important. So if there was an order of
precedence sequence called his first decisions
and repetition, those are more
based on your need. Input sequence definitely
governs everything because it always goes into artery that you've
written the code. That's really it for just
a broad understanding, broad look at C Sharp syntax. When we come back, we're going
to create a new project. We're going to look
at how we can write an actual program that
interacts with the user.
9. Input and Output Programs: All right guys, So we've
been learning C-sharp. We've been getting familiar
with Visual Studio. Let's write a real
program, shall we? So I'm going to create a new project for
this new program, I'm going to right-click
or solution, go to Add New Project. And then we want another C Sharp console
application project. I'm going to call this
one input, output. Input output Clinton. Next, we know that
we need to work with dotnet six and then we create. Now of course, if you are
using Visual Studio Code, that means you'd
create a new folder, do a new dotnet console program, and then you will be
at the same stage as we are, so forth. This application on
what we're going to do is I will just
write comments. One, declare what
we call a variable. We're going to low user input
and store in a variable. Then we're going to
print contents of a variable or the
user's inputs. Alright? So it doesn't visualize it anytime you're using
an application, whether it's whiteboard desktop, you usually, as the user
would put in detail, press, Save or something. And then later point, you'd need to see
represented to you by the program or saved
onto milliliter point. We're not quite
at the part where we're going to see if
it's until tomorrow. But for null, we can at
least see how it is that we receive input from the user
and then send it back. So typically, computers
do three things. They accept input,
the dual process, and then the produce an output. Those are the three
processes of any computer. Tick inputs, process it, and then send back
output process, of course, could be as complicated
as a math equation on, it could be something else. But whatever we do inbetween, taking the input and producing an opiate is called a process. Alright, let us look at
declaring a variable. Now C-Sharp has
different datatypes. A datatype is pretty much like an adjective
that describes the variable and it defines or tells the variable what kind
of data it can store. Data types in. C Sharp can be. Let me just this data types can be and are not
necessarily limited to. I'm just showing the basic ones. That will be string. That would be like
words and numbers, like a license plates, like a name or a
license split number. As I said, Names and Numbers. Anything that wants
it contains letters. It can contain numbers, it can contain
special characters. Well, that's what we
call it a stream. We also have numerical
data types like integer, which would be more
like whole numbers, which would be more
like decimals, then you actually have
double slash float. Did more recent
C-sharp versions, you actually have
decimal as a datatype, but I'll just stick to the
traditional ones just in case where on different versions
they have integers, you have double float. You also have like char. Char represents
one chiropractor. Alright, so it
could be a letter. It could be, I mean,
this is just one. So that's the example of it. The letter E or
the number seven. It's just one thing at a time. It could be a special character. Those are all examples of chars. All right, let me get
my spelling rights. Strings would be my blocks off, takes those blocks of texts, we'll use the double
quotation mark. Notice the char uses a
single quotation mark. In an earlier example,
you would have seen me use my name. And in the default
example you would see Hello World, that is a string. All right? You have different datatypes and we'll be going
through others. I'm just trying not to give
you an information overload, but these are typical examples
of different datatypes. So like I said, it's
like an adjective describing what the
variable can store. The variable is the
actual storage point. So to declare a variable
after the datatype, then I give it a name. So if I'm going to want the
user to enter their name, then I would call
the variable name. So the thing is that you want to always be as descriptive with your variable name as possible. You don't want to
say string aches. What is x? Alright, when
you have to describe to somebody who is looking
at your code or even try to remember what
x was used for. Two weeks later, you will not remember when you
see something like name. It is clear if you
want the edge, you would probably want
to use integer and C, H. That is clear. If it is something
that is more than, more than one word
like maybe full name, you'd want to see
something like full name. No notice how I wrote this one. There are no species. Species are illegal
in variable names. You can use special characters
as far as an underscore, that's the only special
cartilage that is allowed. You cannot use a hyphen, you cannot use anything else with an underscore,
will do that way. Some people like to use
underscores personally, I don't I don't
like to use them. They are illegal. They're nothing wrong with them. So if you want to use
them, no problem. I however, rely on what
we'll call camel casing, which is when you make the first letter come on and then you capitalize
every other words. You see here is an
example of Kamil in case like when we
named our projects, we would have used pasco case. If I said Hello World as a variable and I can
fertilize the H and the w, then this will be Pascal casing. Alright, so we capitalize the first letter as
well as every ADL. First, that's off
every word in there. Generally speaking, when
you name your variables, you want to use
camel casing because it improves readability and
anybody who looks at it, you can tell that this is
a variable designed to store the full name of whatever. All right, so that's why naming
is very, very important. So that is our verse simple
variable. All right? No, That was number one,
declare a variable. We looked at a different
datatype options based on the information
that we want to get. String is the best datatype
for that information. Right? Now we need to allow them
to input that variable. So, so far we know
how to output, because console dot write line is what allows us to output. However we need to input. So where do onto
right, we want tool. If you said read the
near underwrite truck, we want console dot read, line. See you that I always say
console.log deadline. The console will
actually pause and wait for inputs because it's
not reading something. As opposed to write
land where it's going to print out something. Alright? No ofcourse when we
allow the user to insert whatever the read line we
need to store it somewhere. Because when they press Enter after the type within
emperors enter, the inflammation is sent
back to the program. But whereas the
information going, that is why we
have the variable. The variable here with the green line is complaining
that it is declared, but never, you never used because we're not using it to do anything. We
just have it there. So the presence of it
is important. Sure. But it's not doing anything. So we wanted to see when you receive input from the console, please store it inside
of this variable. Went to see a full name. You will get your value from whatever comes in
through the console. All right, so there are number of ways to write this code. All right, I'm just
trying to show you the simplest way you can get on your feet
and start working. One makes sure the
variable exists to assign the value
to the variable. This is called an
assignment where whatever is under right goes into whatever
is on the left. So an example of an assignment, we did an example earlier, I could see a string full name. I'm assigning you
the default value of maybe an empty string, or you should have the
default value of my name. All I'm doing is seeing
create a variable and assign this value
to set variable. If I do not put the assignment, then the variable will
exist with nothing in it. So it's what we're
going to call null, which is just like a receiver. There was nothing
inside there until we do an assignment like this. All right, after we assign the value that the user
enters into our full name, then I can print console,
dot, write line. Then Visual Studio figures that I would like to print
what is in full name. So you've seen console dot
write and then lots of wonderful weekend
print a literal value like we did Hello World. In this case, I don't want
to print the static string because the user didn't
enter helloworld. I don't know what
the user entered, whatever to enter the
stored in full name. That is what I wanted to print. So instead of writing out all these quotation
marks and those. That block off string. I'm going to just
print the contents of my string variable
back to the console. At the end of this application, once again, we are creating
a space in memory. A verbally is basically
a space in memory, a storage space in memory for the time
application is running. As soon as the application
stops running, that variable no longer exists when you start to
top it creates it. Then it waits what input? Then it does science and
input to set variable, and then we print it. So let us state that for a spin. Know, you'll notice that the play button up top
students is Hello World. That means if we press
bleeds still going to run the program.cs
in HelloWorld, we don't want it to
run Hello World. We wanted to run our
input output program. We can actually change
this by changing this drop-down list and
choosing the program that we want or to project
that we want to run, which would be input-output. You'll also notice that
when you change the one, the project that is being
focused on at that point, he's the one that
gets the bold letters in the name, right? So if I change it
back to Hello World, hello World is bold. If I changed the input output, input, output is both. All the way to do that is
directly the project itself. And click Set as
startup project. That does the same thing that a drop-down list up top here. If I want input output, right-click Set a
startup project. And it will not focus
on input output and the Play button will
show the same thing. When I press the Play button, notice that the console
is on the screen and it's just blinking. Okay. Why is it just blinking? It's not printing
anything is not proceeding wanted created
or variable shore, but it's also waiting on input. Output, my name. Then press enter. It will proceed to print to the console and
then you notice it stopped. God, that's all I told it to do. Weird for you input
print and linear done. This second line is
what it is printing. This is what I entered. Now this is not very intuitive. I'm sure you're seeing
that it's not very intuitive because you
just paused for input. I wrote their applications. I know what the
input is that it is pausing on waiting for you. If you were to pick up
this application for yourself without my guidance, would you actually know that
this is what it wanted? You wouldn't, and that wouldn't expect any user really know that saw what you would want to do for this
kind of application. Just like any other application, if you're spending
a lot of form, they usually tell you
what they want in the text box or for that particular input so that the user know
what you're doing. So you always want your user to know
what they need to do. You can always print, holds your own
messages to screen. So here I went to say
console dot write line. I went to give you
the instruction, enter your full name. And I'll just put a
little colon under, of course, this is
what is being printed. So this is a literal string being printed to the screen
and say you wanted to look nice and presentable,
Nice itself instructions. And then it will
pause for input. Then what is being printed. I don't know what's
being printed. So I could say something
like console dot write line. Your full name is, and then we print to the screen whatever
the user entered. All right, so these
are the things that enhance the way your obligation. You do have. Console dot write. First was console dot write. Console dot write is going
to print it in one line, whereas console dot
write line is going to move it to the next
line automatically. Let us try this one. So go ahead and run. You see here nowhere seeing
enter your full name. And because we did a write line, it automatically went to the
next line to wait for input. I went to see my name. Then when I press enter, your full name is printed it in one line
because I said right. So if I wanted a
prompt in one line, I would just say right
instead of one, right? And we'll write line automatically brings
to the next slide. And that's pretty much it. Do you see? It's not
that hard at all. Once again, sequence matters. Create the variable, print and ask the
personal for input, then weighed four inputs, and then you can print
out whatever afterwards. All right, so that's a
simple input output. No one quick refactor that I would like to
show you to this is that when you are asking for data to go
into the variable, it is perfectly fine
to actually just define the variable and
assign it at the same time. Of course, you can't have two verbal assignments
on the screen. If you, if you create a variable before you
do this a second time. But here what I'm seeing is creates a variable called full name and give it whatever
comes in from the user. So that's actually
fewer lines of code. So sometimes it's worth creating a program that
uses fewer lines of code. Sometimes they'll readability,
you want it as explicit. So I'm going to
leave it like that for, for your consumption. Create the variable,
allow the user to enter storage into variable and then print the contents
of the inputs.
10. C# Data Types and Conversion: All right guys, so in
our last application, we took a quick look at how
we can declare variables. We took a brief overview of the different datatypes that
these variables can have. And then we looked to accept and print out information
from the user. Now we're going to
build on both of these concepts as we looked at variables and
their data types. Once again, if you close Visual Studio and you
need to relaunch it, you can always just open, sorry if you close
Visual Studio, I need to relaunch the project. You can always just relaunch
Visual Studio 2022. You can look for the recent obligation or
project that you've been working on or go to
open a project or solution. Browse to where you would offset the project
location to be. Find it, then launch
that solution file. You can also do this step
from your Explorer itself. So let us proceed. We're going to create
a new project. So I'm just clicking
on the solution, go into new project, console up. And then this one is
going to be variables and data types bearing in mind
or very strict naming, suggestions, null spaces and special
characters and Pascal casing. So we do all of that. Next we're using dotnet
six and then we can create know in our brand
new program.cs file. I want us to look at the different variables
that we can use. And we're using
practical examples, someone to be seeing variable
declarations and types. In this application,
we're going to create an actual kind of form data
input form for our user. We want name, age, maybe salary. There are gender and if
they're working or not. All right, let's
do all of those. And in doing so,
we'll explore hall. We convert datatypes between
what is input on what is needed based on the data
type that we selected for the variable and how we can print everything all
today, end of it. We already did an example
with the full name. So here I can see a string
full name is equal to. You generally want to stay
away from, from Knowles. Null, meaning that I just
say string full name. There's a variable
called full name, leave it be, it is generally wanted to
stay away from that. It will work. But when you build a
bigger applications, you want to stay away from that. So you generally want to
give it a default value. So here is a built-in default
called string dot empty. Sometimes you may end up
seeing persons do this, but this is what we
call a magic string. And it's usually discourse because you could
make a simple mistake and not realize and it would be very hard to figure
it out later on. So we need to stream dot empty. You will let see sharps
at the value for you. And it will be much easier to maintain
and on track, don't. Let's see age. So I wanted to edge, I'm going to use an integer because people generally
would be 2021, not necessarily 21.8 years old. I would want an integer
instead of a double or a float because I'm not
expecting a decimal for this. You can also assign this
one a default value. I'll just set 0. There
are defaults here, but you would end
up being int dot, max or Min, which would be
on either scale of billions. All right, So Min is minus 2 billion or 2 trillion
or something like that. I mean that mux is
positive, that same number. So here I'm just going to
set that default is to 0. Maybe one, maybe a default ease off one makes more sense, right? But hey, next stop, salary. No salary. You could expect
that decimal points because salary represents money. You could use double. Then if you wanted to
make a default equal to 0, that's your default. You could probably
also see float does that also takes decimal numbers, except the default value has
to look a bit different. So you'd have to see
something like that. 0 f. Just to show you,
I'm just showing you the nuances with a
different datatypes, but this is also a decimal place is just saying that
you have to put on the F to represent
that it's a float and not just a regular double. You could also use decimal. And if I'm not mistaken decimal, you would just see like 0. You'd have to put on
suffix m. There we go. Once again, just showing you the different options,
different nuances. Alright, so I'm going
to keep it simple. Door double is good enough. And I can just say that WE
will know that it's a decimal, so 0 is the same as 0. Gender. No, I would probably want the user to enter M
for me, F for female. And then you'd want to
use a character for that. So I would say char. Notice I just accidentally
wrote a capital C. Capital C char is
different from common CHR. Similar but different purposes. So you always want them
to be lowercase when you're defining your
variable types. Char and gender. I can initialize this one tool. Don't think that there's
a char dot empty. We do have Min and max value, and you have a number
of other operations. They're not going to go through all these
operations just yet. All right, so here I'll just say give me the
mean value for the char. Then it's asking for birthday. I don't want birthday
this time I want Boolean. And are you working? So Boolean here
represents true or false. You either are
already, aren't you? You're either on or
off binary one or 0. Bool represents that
binary decision, that binary value one
or 0 true or false. The working or not working. Here, I'm just going
to default to false. So you'll notice that false
untrue are actually keywords. You can tell that from their color coding
and the fact that results through it is
actually suggesting those values in
the IntelliSense. For me. Even with a bullet, if I don't assign a value, it will default to false. So that's something
that you can bear in mind if you don't
give it a value, it's default is false. If you don't give integers
and doubles values, their defaults will be 0. Char that will default to null just the same way that the string will
default to null. As you practice and you
get more comfortable. You will know these things. I'm pointing them all because I've been doing
this for years so I can exude this wisdom
because of experience. Both I'm shearing the wisdom
so that you can have it in the back of your mind when you're going through
an experience. So those are the variables that we want to work with
for this exercise. Know what I wanted
to do is prompt the user to enter
each one of these, and then I wanted to store
it in the variables. Let's start off with name
because we've done that before. So I went to see
console dot write line. And I'm going to say
Please enter your name. I'm going to do
console dot write. I wanted to talk to print in one line and then it's going to pause for input
afterwards, semicolon. And then I'm going to see that the full name is going to be equal to whatever
the user inputs, which would be console
dot read line. So remember that last time
we looked at the fact that you just have to
assign it there. No. I didn't mention you
could defined a variable here and do everything one time. For readability purposes, sometimes it's good
to just get all of your variables defined up top and then you
use them later on. So at least you
know what you need. Then you can write code our own what you've already worked old. It's not necessarily one
is better than the other. It's just, I'm just suggesting this kind of workflow
so you can get your mind in order and have
your strategy for your application ready
for when you need it. This section, I'm just
prompting the user for input. Know, I'm going
to basically just copy and paste this
and I'm going to say please enter your age. Sometimes you have to
work smarter, not harder. You already have the
console dot write line. You know that you need to assign the variable that I'm going
to do is copy and paste. It takes its end needs to change and then use the
appropriate variable. So here I'm saying
give me the age. Wow, no average. What is this red line thing? It says it represents sorry, let me hover over this again. Okay, So sometimes when
you hover over it, you're not going
to see the error. Oh dear, it is done at end. Cannot implicitly convert
type string to int. So please you can go to see the error details
would be error list, which is usually done
at the bottom part of your interface cannot implicitly
convert string to int. And if you don't see it there,
you can always go to view. And you'll see error list and you click it
and it will appear. Here. It is saying that I have
an integer called age. But console dot read
line is actually accepting a string
from the user, meaning no matter what I
type in through the console, it will always see
it as a string. So remember earlier when I discussed the
different datatypes, I had said that the string is actually capable
of storing words, numbers, special
characters, anything, anything that is on your
keyboard is actually going to be processed as just
one big block of text. When we put it in
the console input, it is not assuming that because
you put the numeral one, that you want an integer r, you put one letter
you want a character, or year-old award
truly meant a Boolean. It will always be assuming that you are
entering a string. So if you want this string to be stored as something else, we have to do a
manual conversion. To convert from string to int. You have the option
to use converts, which is just another
built-in class. And I can say convert dots. And I can see two in 32. I wanted to convert in 32. This is a method, so I have to open my parenthesis
and then notice it's seeing after parsing
that string value that I intend to convert, what am I converting that
string input from the user. When we just say
convert to in 32, whatever input came
in from the user. And then every time I open a
brief so r bar and the SCC, I need to make sure I close it. So this one, this one
is open-ended loss for, for the red line, but I
need to close the 32. There we go. That gets rid off all the arrows. I
hope that was clear. If it wasn't clear, doubled
to get much clearer. Now we move on to salary. So I'm just copying
and pasting again. And then I wanted to say
Please enter your celery. Then I'm going to be
assigning salary. This is converting to 32. I don't necessarily want to 32 in salary because
salary is a double. It is not an integer. So I don't want the person putting that they made $210.05. I don't want just $210. I want the five-sevenths
also say I need the decimal place.
Well, guess what? I can convert from string
to double our two decimal, we're using double, So
I'll say to double. There we go. All right, so string
input from user, convert it to double
storage instead of salary. Coming along nicely. I'm just going to go ahead
and duplicate these lines for gender and working and
we can figure them out. Gender these into the gender. And then please enter if
you are working or not. Notice red lines again. This one says Gender. This one says working. Let me see it. Working status. Let me rephrase that.
Are you working? Are you working? All right. So forth, agender,
we're using a char. I know needs to tell the user watts
legitimate inputs hour or what the inputs are
that I'm looking for. So I'm going to see inside of
the prompt message M or F, enter either one
of those letters. Then went up to convert
the string into character. Goes around about
character is different from a string because
this can have multiple characters in it's contiguous block versus a char, which is literally just
one character at a time. So I went off to see
converts, Dots, tool. And then I pause in
that string inputs that hopefully only has one of the choices that I've presented. And then I'll store
that in gender. Likewise, are you working? What do I expect the
user to put two in? I expect them to put
in true or false. Why? Because I just wanted to add direct conversion from the word true into the actual datatype. That is a bool, which
is the keyword true. For our visual representation. True in quotation marks. That's what's coming in through the console dot read line. But I need true. The keyword, which is what
I would say until Boolean. We're going to do with that
convert tool. Boolean. I'm sure Visual
Studio is giving you some good suggestions
because it's contextually looking at what you're trying to
assign this value to. So it would suggest to them, because clearly you're
working with a Boolean. This is it for
prompting our user and for behind the scenes
converting whatever the type from the
default string into the actual data type that we need for our
background processes. Using the correct data type
is very important because. If you need it to do
math with salary, you cannot do that kind
of math with a string. You have to have it
as a numerical value to see if this is what
you earn per month, then this is your ear,
the salary, right? So if we ask them to
enter your yearly salary, I wanted to show them what
they earn per month would have to get it as a number
in order to do the math. All right? So those are the things
that you have to consider. At the end of this, we want to print the information
back to the user. So we wanted to
print out this is what you entered, here's
what we have for you. So I'm just going to see
console dot write line. We know that we have to do that. No, at this point, I can do what we'll
call concatenation. So I went to see your name, is. I have two choices. I can't see your name
is and then have another console dot write
line were printed full name. This is what we did last time. Then that's going to
end up doubling up in every print statement when
I can do it in one line. So your name is, and then I'm going to do what
we'll call concatenation. So I'm going to say plus and then give it the
variable. What this does. It says, please add
this block of string. This is a literal because I, the programmer typed is so
this will never change. Plus whatever came in through
the variable because I don't know what the user
is going to be writing in. That is what we're doing there. Another way to write this or to do this kind
of concatenation, let's say with EJW, if I wanted to see
your ij is all right, I can actually put
a placeholder here. So I went to see open curly
brace 0, close curly brace. And then I'm going to put in
the variable that matches. So I can see each know what this is doing is it's actually creating
what we call args. So our arguments that will
be passed into the function. So the arguments will be boned to the variables that
go into see him order. So if I had multiple arguments are multiple variables
that needed to print, I could actually just do 12. And then I can see some text here because I'm
writing with a whole sentence, but parts of it
needs to be dynamic. I didn't for each of these, I'm going to call
them placeholders. I would provide a variable
in the same order. 0 always goes first. The first variable
matches the 0. The second variable, much is still the other one,
etcetera, etcetera. All right, so I will
just list out as many variables as I
have placeholders. So that's some other
ones. I'm going to keep this one simple. And we're just going
to see your age is then we're using
placeholder and printing each. Another way to
print what we call concatenate string would
be to see your salary is. And then I would put
the variable here. Instead of seeing please holder, I would see curly
brace and then put in the variables, so celery, but notice that everything
is still Orange already in the same color as whatever your string literal
representation is. What's missing here
is the dollar sign. I need a dollar sign
before I write the string. Then that allows me to use curly braces and super impose the variable anywhere in
that stream that I want. This is called interpolation. So this is concatenation. This is called inter
pull leash on. The thing with interpolation
though is that inside of this string it's very
similar to using the args. But I can actually just sprint the variables where we'll say
if I write something takes, I didn't, I wanted another
variable and then more text. So I'm just writing all to
one block gloves string, but anywhere I need
it to be dynamic, I just write in that
variable accordingly. Prints that string.
It will just putting those values wherever I stated
the variables should be. All right. I'm leaving a little comments. They are aware of what's
happening at each point. So these are all of your
options when it comes to printing and interacting with a string on the variables. C-sharp does a good job of changing everything
to strings. So you have to be
deliberate to change these into whatever datatype because
it's going from string, you have to be deliberate with the datatype you're
converting tool. However, you would
notice that I'm not converting anything
else to string here. This is a string
and I'm putting in a double inside of
a block of string. The same thing here with edge will full name is
already a string. So here it will always convert whatever data type it is into string without you having
to do anything else. But if you needed to, you could always say
variable dot toString, which will also
convert it to strings. So that works, but that's kind of redundant
because it's not needed. Because C-sharp is that blue. I'm just showing
you your options. So let me just duplicate
this and see your gender is, and then I can just
put in gender. Then I can see you are Floyd Are you employed
or you are employed? Sorry, I'm just getting caught
up in altar write this, you are employed and then
I'll just see working. Alright, so it will
automatically convert it from Bool string for me. A lot of the times you'll
see me doing this, but later on we'll look at string manipulation in
a bit more details. So I'm just showing
you some options here for us to get
through this activity. And later on we'll
delve more into what is possible when it comes to strings and different
data types. From here, if I wanted to run this application,
once again, I have to change it to that project that
we're working with, variables and data types. And then let me execute. Then let's start
filling this out. So this is going
to be test name. Enter into your ear
edge, let's say 23. What is your salary? That's a C. I am earning that 0.35. What is my gender? I went to state the ones that
I was suggested to stay it. Then on my working
I am working true. So at the end of it, you're getting your name, your age, your salary, your gender, and
you are employed. No notice there
is no space here. Why is there no space there? Because I have a dipole as you can just fix that typo and that would fix that printout. But that is really all that I want us to go through
in this exercise. We looked at the
different variables, at a different variable
types and whole weekend give them the default values
versus non-default values. We also looked at the fact
that we can or we need to, when accepting input from the console converts to our specific datatype
based on what we need. And we also looked at the different ways
that we can handle printing old static text combined with Takes coming
in from our variable.
11. Application Debugging Techniques: All right guys, so in
this lesson I want to discuss application
debugging techniques. No, debugging has to do with
finding errors in your code. At this point, I have a bunch of other projects that
we will be going through one-by-one later on. So don't get caught up
on the fun that I have many projects and
you don't let us focus on the project
that we just completed, which was looking at
variables and data types. And I wanted to show
you how you can try to find errors in the
code as we go along. All right, So we
have different kinds of errors that will
appear in our code. The easiest one to spot
is a syntax error. Syntax error could be
as easy a type or you would have seen that they
wrote a few wrong lines. Sometimes, if I wrote
console instead of console, then the syntax error would be apparent through
the prison itself, the red line, we've
seen that before. You'll also notice
that Visual Studio would kind of highlight
the lion to your right. And you'll see that
little red square up here showing that there
is an error on that line. You'd also see that you
have that little x down here and error list
will be populated. So there are so
many indicators as to the idea of an error
in your code when it is a syntax error that
it's hard to not notice. Now let us say a
prayer adventure. You didn't notice it. And you tried to execute someone to change my
starter project here. Then I tried to execute this. I didn't notice that that syntax error and I tried to run, then I'm going to end up
with an error like this. So when you run, you will have two options. You have run with debugging
and run without debugging. Running without debugging means that it's just going to build, make sure everything is okay. And then tried to
launch a program written with the ballgame
means that it's going to run, but be mindful of the fact that you might want to track
what's happening. So we looked at that in a few. However, neither
option will work when, if you have a syntax error. So when you get that
kind of error message saying that there was an
error with your build. Do you want to continue with
the last successful build? You don't want to continue with the last successful build. It means that you're
going to go back to the good version of the code, which would defeat
the purpose of writing new code that
you're able to test. You want to see NOR. And then he wanted to
give some attention to your error list so it can show you where
your errors are. So just by double-clicking here in the area of
the city will actually jump to that line
with the errors. Whether you're going to fix it, commented or whatever
you're going to do, just remove the syntax errors. So those are the easiest
errors to detect. Know the more difficult type of error to detect is
a logical error. In logic error can be
because you didn't write the coordinate
particular way you did something before he should
have done something else. Remember that we have sequence, we have her position and
then we have decisions. If you're a sequence
was incorrect, then you might not get
the desired results. If you made a bad decision, meaning you wrote a statement
condition incorrectly, then you may get a
bad results there also your for-loop, etc. There are a number
of things that can throw all the logic. Those are logical errors which really manifest themselves
during runtime. One of the ways that you can use the truck values that are being assigned is by sitting
in what we call breakpoints. Breakpoint is let's say a marker line of code that tells the compiler that while you're executing all the code, when you hit this line, I want you to pause. Let dot-dot-dot register for a bit to get that break point. If you look to the left
of the line numbers, so you have the editor, then you have your line numbers, which when you
click it highlights the entire lines you'll
see me doing That's a lot. To just get the entire lining just clicked that line number. But into the farthest
left you'll see that the cursor inverts again. So here it occurs, so it looks normal,
just a regular pointer. And then when I go over
to the line numbers, it becomes kind of pointing inverted to
the next direction, and then it goes back
to normal over here. And then you'll see
those little white dots appear on the lines of code. When you click where that dots
up here is, it turns red. The red dot signifies a breakpoint and that
entire line turns red. So you can have
multiple breakpoints. Throw it to your code. The activate the breakpoints,
you just click it again. Click to turn on or turn off. When you are running. In debug mode, meaning you
press this bigger play button or you hit F5 on your
keyboard, it will run. And be mindful of the fact that there might be a breakpoint. So when it hits that break
point is going to pause. When it pause it allows
you to interact with the variables and other bits of code that would
have preceded it. If you run without debugging, then it will just run. It won't curable at
the brief points. Let us run with debugging
and look at that experience. Here you see it. Seeing enter name so that we
know what that we're at 912, That's where it's waiting
underneath to go in. I went to put in my
name, press Enter. Then it goes over and asks what they eat so you
know what that is? No outline. 15. It's waiting on the edge. So putting an edge and
then look at that. So it printed the
prompt for a line 17. Body can't go any
further because of the break point that we
had put that line 18. Know the obligation is actually stopped no
matter what I do, I'm actually pressing keys are, and no matter what I do, I move forward because the
compiler won't allow me to. Alright, at this point, if I hover over these
previous variables, I can actually see what
values went in and I can see that full name currently
has a value of Truvada. William's age currently
has a value of 25 and it's waiting on
salary to get its value. And salary currently
has a value of 0. Gender has the
value of 0 slash 0. That was the mean value. And working by default
has a value of false. See that? Just by hovering over
them, I can see that. I can also go down
here where the some of the additional toolbars are and I can bring them up. I'll watch, watch, that's tiny. The watch section
allows me to put in a variable second and
watch the gender variable. And it was showing me
what its value is. I can watch full name. And it was showing me
what its value is. And I can also watch salaries. So let me watch salary. No salary has a value of 0. But if I press F9 on my
keyboard or if you live in, and I have to make
sure to press F 11 because f ten will actually
stop the recording for me, the difference between f
ten and if we live in, is that if ten we'll
skip over a method, whereas FLM will bring
you into the method. Later on when we're
writing code together, we will see that working. But for null, if
ten or 11 is fine. So if I press F 11, it will move on from
that breakpoint. And all the numbers, these are all the numbers
I was putting in earlier. But no, it's actually
at line 18 waiting for me to enter something
and press Enter. So if I put 800, let me erase this and just put sensible number and
then press enter. It will move on
to the next line. So you see when
it hits the line, it changes it to yellow. When it hit the breakpoint, it was yellow when he
moved to the next line. It's not showing me That's
how about the next line? Right. If I go to the watch
to watch his nose showing me that the
salary is no longer 0, it's not 8 thousand
because that's what I entered. At this point. I have a few options because it's still waiting
for me to go line by line so I can continue to
do F11 or f1 until the end. Which I mean for a small program like this
wouldn't be a big deal. But sometimes you
really just wanted to devaluate what happens
at a particular line. After that particular line, you can let it continue.
To let it continue. You just press F5 or
click Continue up top. And once you do that, it will just continue
with the execution of the code as usual. So here I can put in m and
you see it's not pausing and asking me for anything and it continues to the
end of the execution. Once again, this
breakpoints allows me to pause the application
in its trucks. And I can take a look
at the variables may be whatever it was being
set was incorrect, maybe the salary value was wrong because the
calculation is wrong. This helps you to pause your application and take a look at the values
that are coming across. And then you can
determine that, okay, maybe I need to tweet this or modified that as you go along. And then it helps
you to move forward. Here with celery, I just
disabled the checkpoints are at the breakpoint rather by clicking the red dot
and clicking it off. You cannot have multiple. The point is that each
time you hit Continue, it'll go as far as the next breakpoint in
the line in that order. So if I enter the salary and it went here and in that
pressed continue when he teaches line 20, he would continue,
continue, continue. And then it would do
everything up until the next break point where
it pauses and waits for me, the developer to
either evaluate what's happening and then continue
or go line by line. So that's some guidance as to debugging in
your application. This will come in handy. When you end up with errors. Maybe your logic is not as well thought of it as you
would like and that happens, we're all human, right? Sometimes you don't get
it right the first time. Which is why these
tools exist to help us to refine
what we're doing.
12. Arithmetic and Assignment Operators: In this lesson, we
are going to be looking at math operators, lets us create a new project. And we're going to be calling this one math operators
using dotnet six. And then we can get
this show started. No. We want to look at
maybe three categories. We have basic operators, are basic assignment operator. Rather, we have arithmetic
operators, operations. Then we have maybe compound
assignment operators. We looked at the assignment
operator before. This one has to do with
assigning a value int num, we have a variable. Cool. I want to give it a value. I'm going to say num is
equal to maybe five. That is called an assignment. In regular English or
irregular math, a human being, when you see the equal sign, it means that whatever is on the left has the same value
as whatever is on the right. However, in programming, when
you see one equals sign, it means whatever is
on the right is being assigned to what is on the left. So this is our variable. We're assigning the numeral
five to the variable. That means the reverse won't be true if I write this in code, I'm just going to get an error
because there is no way. I'm seeing that these
two are equivalent. In regular English or
regular human language. That would probably
be acceptable, but not in programming. Already explored whole, we
assign values, our variables. And if we wanted to
do it in one line, we can easily just say int
num is equal to that value. By default, once the
variable is created, it's created with that value. Now let's look at
arithmetic operations. Arithmetic operations
would have to do with us doing math. I'm going to do norm1, give it a value of something
random 11 and num2, Let's give it a value of 12. Thank you. Visual Studio. You see here, he's just a
gift that keeps on giving. However, that's not
what I want this time. When we wanted to do math, we have the opportunity
to do the same kind of math that we're used to in our regular
daily lives, right? So if I wanted to do addition, remember I said that you can
do different operations. We can easily add
num1 plus num2. Simple math. If I ask somebody if I wrote a program for calculate and that's a nice activity for you. Write an application
that accepts two numbers and
performs addition. You would ask them
to enter into num one and turn it into num2. And then you would
see num1 plus num2. Then proprio have
another variable, int sum is equal to num
plus num1 plus num2. Math is that simple? So I'm just going to
write them inside of these Console.WriteLine line. So we actually
print them back to screen and useful lines of code. That's a nice activity if
you wanted to go on right, and a calculator
after this activity, then that would be a
good practice for you. Writing input, output, and
processing applications. We can do math, we
can do addition, we can do subtraction. Instead of a plus sign. We have a minus sign. We can do multiplication. So remember that I'm just
duplicating the lines using Control D on my
keyboard or Control CV. All right, So multiplication is represented by the
asterisk, not by the x. Alright, so we write an
x when we're doing math, but it's an asterisk. In programming division,
you don't want to have a division symbol on your
keyboard if you wanted. That's in Microsoft Word,
That's a special symbols, so we use the slash. So that's a division sense. Norm one is being
divided by dome tool. If we want to do modulus, we use the percentage sign. Modulus is basically
the division or the remainder after division. So let's see. Two divided by three. Divided by three is going
to give you something like I'm sorry, let me see. Five divided by two. Let me do something more easy. Five divided by two would leave, would go into two times, remainder one because it
would get a superinfection. Well, you'd get two
times remainder one, tool times remainder one. All right. Now, if you wanted that
remainder one only, then you would say
five modulus two. That will return
just the remainder. That's what modulus dot. So if you've never interacted
with more details before, that's what it does
see if you ever get to an application
where you need to find all of the odd numbers, are putting a number and
find out if it's odd, then pretty much if the number, let's say I'm sorry. I think that would
be a nice activity for us to do after this. I'm going to leave
that until later on. But if you ever have to find out if something is an odd number, all you have to do is
do number modulus two. And if whatever the result is, is one, then it is an
odd number, right? That's all modulus works. So let us, in a bid just
to drive the point home, I'm going to modify
the value of num one. Num one. Your new value is
norm one plus four. All right, so that's me
increasing the value of num one. And then if we were to print out all of these and
I'll just add one here to see a new value of num one is whatever
norm OneNote has. Afterwards then
you'll see that these were the original answers. I modified the variable's
value and then we get these new
values afterwards. That's a simple math operations. Then you have different
libraries that allow you to do more complex operations. Tangent and sine
and cosine, etc. But I'm not getting into
those in the B6 right now. If I run this program once
again changed the car starts or project to math operations are operators
started the project, it should just spit out
all of these calculations. So the console screen. There we go. So sometimes
the console will pause. You just have to click. If you click in it
will kind of pause. So when you press enter
it will continue. So here we have the
addition and subtraction, multiplication,
division and modulus. You see here it said that
12 could not go into 11, right? Or what was that? That was normal one does
he live in divided by 12, so that's 0 times over the modulus is 11 caused
the remainder would be 11. Then we go onto new
value of norm one is 15, and then we get the addition, we get the subtraction will
go into multiplication. Know, 12 into 15 goes one time. If the remainder is three, modulus gives us the remainder. Alright? That's really it for basic
arithmetic operations. Node compound
assignment operators would be like a
shortened version of doing something
like if I wanted to modify the value of a variable, but I wanted to add or subtract
from the original value. In this case, I
increased it by four. So I said whatever the
value of num one is, right? No, give it back to me and add four and then assign
that new value. Normally that's installed,
I would increase it. If I wanted to decrease it, I would just do the same
with a minus sign, etc. If I wanted to
make it four times its original value,
I'd say multiply. Now when we talk about
component assignments, it would be a
shortened version of doing this kind of operation. So if I wanted to see
increased num one by four, I can see norm one plus equal. Then give it the value. So this would no see accumulate. So we have this, this is called an accumulator. Norm. One increases by four. That's pretty much
the same thing as writing what we
did up top here. I'll just come in adults. Same dynamic. Alright? And then based on
what you want to do, like I said, if you wanted to decrease by four, it
will be to see him. I say num one minus four. If you wanted to multiply it, make it four times
its original valid. And it will be times. If you wanted to make it four times less than
its original value, then it will be divide, etc. So when we talk about
compound operations, That's what we are doing. Compound assignment operations. The thing is that with
each of these operations, we're actually changing
the value of num one. So it's not a matter
of just printing out, this is the result
of the operation, it is actually changing. So here I'm seeing odd for me
to remember what sequence. So num one starts off as 11. We go through all of
this or the sprinting, all the result of the operation. We're not assigning anything. We're not making new variables are assigning any
value to any variable, then I see increased
normal one by four meet that the new value, num one at this point is no
longer living, it's now 15. So all of these math
operations are relative to normal one being 15 and
num2 still only being 12, which is why we saw
the different sets of results at this point
normal and is still 15. When I say plus equals four, I have no increasing it from 1519 causes at this
point it is already 15, so I'm adding four to 15. And then here I'm
going to say make it back 15, right here. I went to see if 15 times
four is equal to 60. Know no one has a value of 60. And then here I'm going
to say divided by four, I'm putting it back to 15. And then I'm going
to say modulus four. So that means 15. Modulus four would be if my
math serves me right three. So what we're going to do, just to test if my math
is still on point. I am going to do our
console dot write line. And I'm going to be
printing the value of num one with each operation. Then we can compare to see if my mouth that I have in the comments
there would be correct. Alright, let us run
this application again. Alright, so now
it's your time to mark my math assignment. So I did say that the first
one to Ockham will be 19. There we go. The second one to
o con would be 15, then 60, then back to 50, and then the modulus
would be three. So no, each time we're
changing the value of num one. That's his whole, you can go
ahead and modify variables. If I said increase something, increased all the prices
by five, then you know, you can just see price
times z equal five. It would automatically do that.
13. Conditional Statements: All right guys, In this
lesson we're going to be looking at conditional
statements. I've already created
that project. By now. You should be familiar with how to create a project. So you can just go ahead, hit pause and go ahead and
create that project. When you've done that,
we'll continue and we're looking at
conditional statements. Once again, with
conditional statements, it's all about
making a decision. We already discussed
that we have sequence, we have decisions,
however, repetition. So decisions on conditional
statements seem. So we have basically
three main ways of making decisions in most
higher-level languages. So when I say high
level languages, I mean C-Sharp, Java, C, C plus plus, and languages
like Python, Ruby, etc. So we have if statements, we have switch statements, then we have Turner operators. The syntax off
course would differ, but generally speaking,
you statements, switch statements, and
ternary operators. They all look the same way. The same way really in CC
plus plus C-Sharp Java. They may have slightly
different interpretations in Ruby and Python on
certain other languages. But once you understand
the general idea, adopting to another language is really just a
Google search away. Even though I'm doing
a C-sharp course, I just wanted to bring you to the points of
realization at once. You appreciate these
concepts within C-sharp, they can be applied to any other language
as you may need. If statements generally make use of what we call
conditional operators. So we just looked at arithmetic
and assignment operators. We looked at
conditional operators. Conditional operator
we've seen one before, is in the form of
a w equals sign. That one means equivalents. So like I said, the, the single equal sign doesn't mean that the two
sides are equal. It means I'm assigning what's under right to
what's on the left. However, when we see
the double equals sign, it means that whatever
is on the left is equivalent to what
is underwrite. That's an equivalence operator. They also have less than, we have greater than. Sometimes I get those mixed
up, so just work with me. We have less than an equal to. We have greater
than and equal to. We have not equal to, which would be not the
equal sign symbol. Those are generally
the operators. And once again,
those are in most of the languages that I
had listed out earlier. So once you understand
how to use these, you can apply them
almost anywhere else. So let's get started with a simple program to
see how this works. I'm going to say int num. I'm just going to prompt
the user for input. So I'm just going
to say console dot, write line, enter, first number. And then we just see int
num is equal to converts. And I'm just going
to write everything sharp because we know
what we're doing. And it's even Visual
Studio is helping me out. So whereas seeing into
your first number. And then we're going
to convert to in 32, whatever comes into the console. And we're missing a
semicolon on that line, which is why I had
that red line there. Alright, let me see. Visual Studio is going
to have to again. No, that's not the hip
I was hoping for us. So let me just copy this line. I went to call this num one. I don't want to call this num2. And I'm just going to say
Enter your second number. We're asking the user
for a tool numbers. Now, let us look at some
conditional statements that would be sensible. So what if I wanted
to know if numb one is greater than num tool? I would start off and say
if which is a keyword. But notice it's not a statement because I don't need a
semicolon on the end. So I'm going to say it's suggesting num one is equivalent to the norm to
what I'm not there yet. So I wanted to see if normal
one is greater than num2. Then I went to start
up a code block. What would I like to do? Maybe I would like to see
console dot read line. You have, you know, let me, let me make this a
bit more context or enter number of apples. Num one represents uploads. I didn't. Here's an example of me
violating my own rule. Num one does not
tell me that this is storing upwards normal
one here says number one, what I'm asking for uploads. So there's a disconnect here, so it's better, I would
name it number of apples. So it's clear. Now when I name of rename a variable and I have to
rename it in several places. Of course, I can just go
through and copy and paste or I can just do Control and dots. And then you'll see
the option Rename. From old name to new name. When I click that, it
will just go through and refactor all of
the parts for me. That's a nice
productivity tip for you. I want number of apples,
number of oranges. I went to call this one
number off oranges. And then I went to the same
control dot and renamed NOW number of apples,
number of oranges. If the number of apples is greater than a
number of oranges, then I went to see you
have more couples. Cool, nice and simple. All right, That's
a new statement. But what happens if I have something else I
wanted to check for? Because this is only
checking for one-off the conditions and it's
taking one action, one-to-five more orangey
is then I've upwards. Then I can say else. This would be like if this
is the case, then do that. Otherwise, do this. I'm going to say you
have more oranges. If the number of apples is greater than the
number of oranges, then you have more apples, else you have more oranges. But that might not necessarily be the case
because what if they're equal? Because right know,
I'm only checking for one condition, alright? And explicitly, I'm only
seeing if the number of apples is greater
than this, then this, that's not a fair
assumption because I could have the same number on either side would
have to introduce more conditions to this hole. If check, I can actually have
another construct called L, which allows me to check
for another condition. I can see if this is
the case, that else, if this is the case, so number of oranges is greater
than, number of apples. Or I could see less than, so I use a greater than already. So let me see. Number off. Oppose is less than
the number of oranges. I'm just showing you how to use up the conditional statements. Operators, LLC. If the number of apples is less than the number of oranges, then we print you
have more oranges. What if they're equal? I can do another one else. If number of Uploads is equivalent to the
number of oranges, then we want to write
some more code. I notice that it's not
illegal for you to put the else if on the same
line as a curly brace. I'm, I tend to speak the
model because I want that clear distinction as the weird one starts
on one stops. But it's not the LIGO to
write your code like this. Else-if that is the case, then I'm going to console that. You have the let me just see the same number of
apples and oranges. That's what I went to
print to the console. It's one big decision. So you see you can have
a simple decision. You can have complex decisions to make based on two
different variables and different things that
are being thrown onto two different considerations
and really enjoy it. That's all big obligations. Are there You statements? If you do this, then you get that result. That's all that they are. Now wants a whole company created the application may seem whole beautiful user interface is and how high ticket looks. At the end of the day, it all comes down to a
bunch of if statements. If you press this
button, what do I do? If you put in this
value, what do I do? That's all it is. All right? So at the end of all of this, if none of these
conditions holds true, then you can finally make one big else statement
that would see invalid input or could not
compute our direct action. You don't want something like
that. When you get those, when you put in a certain
volume and an application, I get something to say
that's an invalid value. It means that all the
if statements that were written did not account for what you actually ended up putting in, it'll sit else. I don't know what to do. I can't I can't help you. Alright. What's in this situation? We probably don't
need to go that far, so I'm just going to remove it anyway because I
think we're covering all the potential outcomes based on what we're asking here. The number is either going to be greater or less or equivalent. Granted, you could
put in all the ones I created an r equal or
less than an equal, etc. But we're not going to
go into all of those. Just know that
those are options. And it obviously,
if it's not equal. Then it's going to be
either greater than r, less than that. So you can make decisions
when you are at in the code that you can make a decision
as to what kind of decision. Our conditional
logic you need to put in to match the situation
you're dealing with. You can go ahead and test
that one if you want. You can put in the same number, you can put in two
different numbers. And based on the
numbers you put in, make sure that
conditional statement is sprinting what you're expecting. I'm going to move on
to switch statements. You can hit Pause, go ahead
and test and experiment, but I'm moving on to
switch statements. So let us see that we have, um, we need to evaluate
watts is in a variable, and then based on what
is in the variable, we need to take on oxygen. Alright? So I'm going to give you another example that
I think is practical. I'm going to ask for agreed. Alright. I went to see enter final grade and we're going to determine if
you passed or not. Someone to see
integrate is equal to whatever is passed in
through the console. So a switch statement literally starts off
with the keyword switch. Then we put in the variable that we're going to
be evaluating grade. This is not limited to integers. This can be with stringy, can be with charge,
can be with anything. Because even when we're accepting the form
and we said MRF, I could have easily put in
a switch statement R&D if statement to see if
m then print meal, if F then print
female or switch, similar to what
we're able to do. So I'm just showing
you that it's not limited to integers. I'm just doing this room. So I can say something
like the switch, I can specify a case CC or Visual Studio is
waiting for me to pick up. And continuous I can say case 0. What do I want to do? Console dot write
line, you failed. Nice and simple message. Then I need to end
each case with outbreak. Pretty much that's it. All right. Whatever you do
between that colon and the keyword break
is what will be executed if that is
indeed the case, where we're looking at
the value of your grid. Grid probably wasn't
such a good example because the case has
to be very specific. So unlike the if statement
where I could see like this greater than that or the scripted and that's that I have to be
specific with a value. So I can only see keys 0. In that case, I can
say keys one or tin. And the ofcourse,
whatever value I use is relative to what
I'm checking for. Here I would say
Console.WriteLine, you got ten marks. If the value is ten, then it will go here. If the value is 0,
then it will go there. But if it's not ten, it's not 0 and it's
a valid that is not accounted for in the cases, then we would want to
give you that default. So default means this
is what I'm going to do when all else fields. All right, so break,
Console.WriteLine, invalid grades. So if we entered 1 thousand I, we did not go on for dot 1 thousand inside
of all of this, then we would just go to the
default that would be like the default else
statement, the catch-all. No other condition was met, so just do this by default. That's what the keyword is here. If I wanted our range inside
of my switch statements, which would meet
more sense here, considering that degreed
is usually between 0 to 60 for the failure and then
6060 to 100 for the pause. And then default would represent anything outside of 0 to a 100. Then you'd probably want to
do something like keys, int. Just choose a variable when. Then you can see here is, let's see, great today. Sorry, less than or equal
to the value of 60. All right. This point that just
escalated, right? I'm sure you already
understand how you do keys against a single value. If you wanted to
check 102030406998, you just have keys ten, right? What do you want to write? Break, case 20,
etcetera, etcetera. And you'd go along,
if you wanted them to share the same block, then you would actually just
say like keys 101112 and All of these cases would just
use that one block of code. So yes, it's unblocked, but there is no open
and close parentheses like you have to switch. It's just the colon's. So all of these
would actually share whatever happens here
before that break. That is one way to make different conditions
for different values share the same thing. Of course, that will be
tedious if you are checking between 0 to 60 because it would mean you have to have 60 cases. Are 61 case is 0 to 60
to check for failure. That wouldn't be practical. What I'm showing you now
is how you can make like our range for a
number of values. Right here I'm
going to see case. And then int n. So n is like in in-process representation of the value that we're evaluating. And then you'll see
when I actually think these braces are optional, if they are int n, when this n, whatever n is, whatever that value
is and represents that value is less
than or equal to 60, then you print, you
failed whatever value. And once it is less than or
equal to 60, then you field. So that I can now
check case int n. When n is greater than 60. Then we can say you passed. That makes sense. So
60 is the pass mark, you feel are 61 is the password, let's say greater
than or equal to 59. Yeah. All right. See if it's
greater than or equal to 59. Less than or equal to 59, sorry, That means you've failed. However, if it is
greater than or equal to 60, then you pause. So 60 is the pass mark upwards. However, if I put
enough thousand, that is not a valid value. So there is another
situation where sometimes you need
multiple conditions on to seem multiple conditions thrown in the same decision. All right, here we have to use what we call
the and operator. And operators would be, and they also have our
operators which would be RR. So that's, these are pipes
and these are ampersands. Alright? So anytime you want to specify two or more conditions
within the same decision, like for this case, I
wanted to make sure that it's greater than
or equal to 60. It's also less than
or equal to a 100. So I can see when n is
greater than or equal to 60 is less than or
equal to a 100. I didn't Standard English. This would be like
saying between 6100. That's what the Andes, alright, so when it is, as it's over 60 but
also less than a 100, inclusive of these two values, then we can bring to
you passed anything outside of that is
an invalid grade. So my challenge to you would
be to modify this to make sure that it's
creates a non-zero. It's not negative. Negative would be invited
over a thousand is invalid. So that's my little
challenge for you based on this example, modify this case
statement to make sure that it's not less than a 100. Like I said, you can have as many statements in this
section as it needs to. So if you wanted to print
you pass multiple times, as long as you do that
before you hit the break, then you have no problem. That's another way
to make a decision inside of our
C-Sharp application. The final one is a
ternary operator, which is like a one-line version of a simple if statement. Let us say that I
wanted to determine the message to print based
on the condition here. I could easily say
something like message. Let's say var
message is equal to. And then I check the condition. My condition would be if number of apples is greater
than number of oranges, then I want to
make my message b. You have more apples. Otherwise my message will
be you have more oranges. There are so many ways
that you can do this. This turner operator can come
in handy in so many ways. I need to just print
the message instead of having the whole
if statement here. If and else if you
could just have a turnoff burrito, no downside. So I'll turn our operates
as simple as it is, is that it's generally
designed to just have one condition so it wouldn't
get complex as a whole. If block that can give you multiple variations of
different old columns, etc. He would just be like, is
this condition true or false? If it's true, denoted
by the question mark, then this is the
value that I want to place inside
of this variable. If it is false
denoted by the colon, then that is the value
that I'm working with. And that pretty much
works anywhere. If I wanted a Boolean, what I would need a ternary
operator for Boolean. That's an activity
for Leto time. Now I gave you a challenge to modify this to show the range. So what I would've done here, we'll say int n, when n is greater
than or equal to 0, is less than or equal to 59. And that's it Really. This is between 059, you failed. Between 100, you pause. And then like I said, you can have the keys would
assemble. So let us see. I will just show you, leave you with an example
of a simple case. Let us say k is 101, that sees the value. And then I'm just going to
say console dot write line. Single case example. If you did cases 101, that's a single case example. That red line is there
because I need a break. All right, So every case it needs to be
followed by a break. You can go ahead and
run this application. We're going to be prompted to enter two values or I'm sorry, I'm still under the wrong one. So let me change
it to conditional statements and try that again. Then we have to enter
the number of apples, someone to say ten, and then a number of oranges
on what you see at 20. And then I'm getting
the message. You have more oranges
because the number of oranges was indeed greater
than the number of art was, the number of apples was indeed less than the number of oranges. So I have more oranges right. Now, if I enter the grade, I went to enter 50, then you failed and didn't know. Well, that's the message
from the ternary operator. Let me just comment that
ought to avoid any confusion. But let us try this again. If I enter ten for both
apples and oranges, 1010, you have the same
number of apples and oranges. Thank you. And then if I enter 101, it is going to say
single case example. One more test. I'm just going to comment
out the if statement operations run
without debugging, so it loads a little more
quickly into final grade. If I enter one falls
under something greater than then it's
going to see invalid grid. Couldn't meet that case. It couldn't meet that case. It didn't need this case, so it went to the default. That's pretty much
how conditional statements work in C-Sharp.
14. Repetition Statements: Welcome back guys.
In this lesson, we'll be looking at repetition
statements in C Sharp. No repetition statements
generally come in a few forms. We have a for-loop, we have a while loop. You have do while loop. You have others that I'm not willing to explore in
this particular lesson, but I will make
honorable mentions off. And that's namely,
well, before each loop. Lets us focus on
the ones that are generally prevalent in
general programming. Once again, I'm
always trying to show you how to briefly give off a whole to apply what
you're learning here in C-Sharp in any other language. For loops, while
loops do while loops, those are available in
every other language. The for-loop is what we call
a counter controlled loop. Basically means I am going
to loop for this many times. So there's a counter that striking how many
times you did already run and how many times
it needs to run. Then we have what we call the
condition-controlled loop. This one is a pre-check, meaning it will
check the condition. So we just looked at conditions resolved or at
conditional statements. So it's going to
check up on Deshawn. And then if that condition
does not hold true, then it will go ahead
and do the loop. And it will keep on checking the condition until it's
true and then it will stop. And then we have the do-while, which is just an
inverted while loop, really where the check
is done at the end. So it will do it at
least once and then check afterwards to see if the condition is
met, then continue. For each like I said,
honorable mention. We will just look at a quick
example of that later on. Let us look at a four-loop
syntax for a for loop is four, and then open and
close parentheses. And then we say int. And generally speaking, in most examples that
you'll see on the Internet, you see the variable being used has no relevance using the letter i has no
relevance to the court. I could've used norm, I could have used counter. All it is is a point of reference for the
variable that is going to be keeping track of how many times this
is going to execute. I guess is most
popularly used letter because it represents
the word increment. Increments means at each time. So I can be sure
it for increments. But it could also be increment
equal to also be a right. I'm going to proceed with I only because it's the most
commonly used variable, but once again, it has no
real syntactical relevance. So we define a variable, generally an integer,
give it a name, and then we assign it the
value of 0 at the very start. Notice you see Visual Studio is auto completing it for me, but I want us to understand each component of
this code properly. So I'm not going to
autocomplete on with the type and explain
as I go along. So that first block defines the variable which
we need as an incrementer. Notice a semicolon ends it. The second block on
mobile to type represents the value that I wanted to
be at when we exit the loop. So I can say I less
than five or ten, whatever value I put there. If I wanted to run five times, I see I less than five. No, Why would it ruin five
times even when I'm saying I less than five because
I'm starting my contact 0. So if you were to do
it on your fingers, I'll just read
here so we can see it as clearly as possible. 012345, that is 123456 times. You see that if I
start counting from 0 and then I go up
to the number five, that is actually six. So that is why I say it
must be less than five. So the moment it hits
five means that I am exceeding my number of runs. So 010 is one, this is two, this is three, this is four, this is five. When the increments
that changes the five, it's going to realize that five is no longer
less than five. You see what I'm seeing? At that point I wanted to exit. So this is the exit
condition for the loop. I wanted to run from 0 to four, which would be 12345 times. So if I wanted it
to run ten times, it will be between 0
and i less than ten. Just so you can appreciate
why we're starting at 0 and that we're seeing less
than the desired number. There are different
ways to write this, but generally speaking, this is the way you'll
see it written. So I'm explaining it easily. Understand why it starts at 0, our white arrow instead
of less than the number. Alright? No, the next block. Is our increment where all we
have to say is I plus plus. So earlier we looked at the compound
assignment operators. Those are accumulators,
meaning you're actually adding whatever value to the origin of value and
putting back in a variable. This is an incrementer which by default just sees plus one. That is almost the
same as seeing, is supposed to be
equal to I plus one, which if we're using our accumulator technique would be I is plus equal to one, which could also be written
plus equal one equals sign, which could also be
written as a plus plus. That's all we're doing. We're just increasing
it by one each time. So that's why I said it's
a cones are controlled. Now this could be I plus two. I'm sorry, if I wanted, it's increased by two. I would have to say
I plus equals two. It would increase by two. So if you wanted to jump in twos as a counted from 0 to ten, then it would ruin 0, then two, then four, then six, then it, the moment it would be
quantitative, it would jump out. So I'm just showing you
the different iterations, but at a very basic
level, I plus, plus, once again, I is
not that important. This could be norm,
this could be counter. And if I said colon
transit variable here, that means that
everything that I had as iodixanol relative
to the word counter. That so a for loop looks in
most high level languages. All right, I'm going
to proceed with I. If I do console.log
line inside of this, I can print that, you know, i in a loop. So I'm just sprinting
the line I'm in. Expectation is that
this is going to print for as many times
as I have defined, which would be between
0 to ten or 00 to nine, which would be ten times whatever I put inside
of this block, would be repeated for as many times as we
have defined here. That means if I want to do
something after the loop, I have to put it outside
off that loops block. Here I can see console
dot read line and we'll just print a blank line by
not putting anything there, it's just going to
print a blank line. And then in this line,
I'm just going to say a for loop finished. I wanted to print
this line ten times. And then at the end
of it we're going to say a for loop is finished. So change your projects
of course, and let's run. And then when it takes accuse, you see here 12345678910 times, and then the loop is finished. So that's what
repetition brings, because if I wanted to
print this ten times, I would have had to write the
console landline ten times, which is not practical. If I wanted to increase it, I would have to go
back and write it that many more times in a for-loop, I can just change this to 20. It ruined 20 times 25 to
make it run 25 times, five times to make it run
and fewer times, right? So just for some more
contexts though, I'm going to show you
what the value is. So I'm going to load the console to print the
value of the counter. I wanted to say
counter value colon. And then I'm just going to
interpolate or I variable, which is our counter. So remember that
each time this has the increase and then it's
going to come back and check. Has the increased made
me exceed the number? If not, then run any comments, Buck, have a exceeded a number. Null comes box. So each time it ruins
it increments and checks, increments and shakes. So you're going to
see that it run between the starting value of 0 and the value of four, because at five it
would violate that. Let's try that. All right, so I'm running from 0 to five now you see
the first row needs 0, then it went ahead. The second run is 0, I'm sorry, 1D and
2D and 3D and 4D. And the moment you'd have
incremented to five, it jumped out and then went
to the rest of the code. And that's pretty much
all our for-loop does. Anytime you have
something you want to repeat for a specific
number of times, the for loop is your best bit. Alright, someone to comment
this out just for now. And then we will jump
down to the while loop. The while loop is going to have a slightly different syntax. Number one, I went to want to give it some form of counter. So there are number of there are a number of
applications for the while loop. Let me, let me start
off with the syntax. Firstly, you see awhile, then you give it a condition. So we already looked
at a condition, that same kind of condition
statement with less than, greater than etcetera that
you would use in an if statement is the same condition you'd use inside of the while. I can see while some variable
called n is less than five. Here's why I was saying I would give you the counter
because there are times when you wanted
to do something for a fixed number of times, you will need to truck than
the number of times you have to manually go
and create a variable. That's you're using a 0 counter. Then you're going to
give it to a condition. It's almost like what we did
between these two parts. And then we would have
to do this part outside the little section of the loop. So let us go ahead. I'm going to show
you the two ways. I'm going to just show
you one when he meets the condition based on
certain elements versus one, we have to do it in a
counter controlled way. Let us say I want a
console dot write line. And I wanted to see, give me a number, input number. All right, then I'm
going to let n gets that value coming from
the console read line. Alright. I'm asking the user for a value. I'm taking the input. And then I'm going to print at the end of
it that you entered. You entered whatever
number you entered, which is n, put on
the dollar sign. So we get an input
interpolation working. This is not a counter
controlled situation. This is a situation
where it's checking if n is less than five. While this condition holds true, please run the loop. Egn starts off at 0. So when it does this check,
it's going to say, well, okay, I went to run this code. If n still meets the condition after
printing told the screen, then I went to run
it again and it will continue running it until
that condition is met. So that's not a counter
controlled situation. Because if I enter a 100 numbers that are all less than five, then this loop would
continuously run until I enter something
that is greater than five. So that's, these are
condition-controlled loop. There might be times
when you need it to be counter control. And at that point to
doctor actually put in your manual step to increase
your counter accordingly. This is where I will be increasing the colon
trial accordingly. Just to make sure, of course I wouldn't be taking user input. So let me just keep it that the condition control
situation and let us test. Here we're seeing
inputs are number. Remember what that is
going to run while in this E15 and
started off at 0. And if I put you in one, it's going to say
you entered one, but it checked again
and sees that, okay, I'm still in a loop. Enter another number two. And then for as
many numbers as I enter that are less than five, it will always keep
looping and sit up. It will never let me
do something that violates the condition
that was coded in. The moment I enter
ten, it will exit. Here. I can see while loop finished because I finally entered something that
brought the condition. All right, that's pretty
much it for the while loop. So the while loop would actually keep in
an infinite loop. I don't know if you've heard
that expression before. An infinite loop, you've probably heard it
in a movie before. But this is what it is. Poorly constructed
while loop can end up in an infinite loop where that loop would never
stop and could even have serious consequences
on your system. In the long run. You always want
to make sure that you meet that condition. Or rather you write the condition
in a way that it can be met to let the loop exit. This is an exit condition. Now let us look at the do-while, which I didn't mention is just an inverted version
of the while loop, but it does the check
at the end, right? So I'm going to take in, take our variable called n, reinitialize it to 0. And then I'm going to see, do they open up a block? Alright, so this is where
the syntax is different. We say do open up the block
and then we say while i, then we have the condition
n is less than five. That's pretty much hold on. Let me comment this out for now. So we have a variable and just the same way we had to get the variable and get
the conditioning. We have to do the
same thing here, except the condition
is at the end, which means that it will
always do this part first, regardless of what the
condition if it is METAR knots, it will do this at least once. So that's what we'll
call it post check. It's checking afterwards. If we use the same
code as the do-while, while, sorry, inside the
do-while is going to do this. Then afterwards it's
going to check if n was still less than five. So let's try it out. One. You see here, it's jumping up. It's asking me for a number. If I enter ten, which is greater than five, it's going to print
and then it's going to check and see
that I exceeded its. So don't do anymore. That's pretty much all there is. Say if I had set this
value to ten initially, which obviously would violate this condition and an arrow. And it's still going
to ask me for a value for n. Because as far
as it's concerned, it needs to do this
at least once. I didn't. If I change this to two, it's still going
to be in the loop because now n is less than five. If I change it to four and he's going to
keep on looping me until I exceed the condition. That's a that's a do-while? No, we've looked
at the do-while, the while and the for loop. The for each loop would
look something like this, but this is usually
used for lists are collections which we're
going to look at later on. But basically saying
for each item or each installment or each object inside of our collection,
this is what I wanted to do. So this is useful when you don't necessarily know how many times you want to do it and you
don't necessarily have a condition upon which to stop. You can just use the for
each to say just go through each one instead of the
collection and do this, but we will look
at that later on. So I'm not as I said, it was really just an
honorable mention. Know, I brought up that for
each loop very quickly, just know using Visual Studios, code hinting and suggestions. The thing is with these, you can actually quickly just start typing for and
then press Tab twice. It will generate that
block of code for you. The same thing for do, the same thing for a while. I'm pretty much it
works for every block. If I said if Tab twice, it brings up that IF statement, if I say switch tap
twice, it does that. So I'm showing you
that after the fact because I really wanted to
get the practice of typing, he told from scratch, you can appreciate it. And some of these
shortcuts don't think they work as well
in Visual Studio Code. So it's still good to understand all the type or the
syntax regardless. Alright. At this point, I'm just going to put on that last console rather than after the
do-while like I did for the while and the four. And that's it for learning
about repetition statements.
15. Methods - Void: Hey guys, welcome back.
In this lesson we'll be looking at methods or functions. So I'll be using both words
interchangeably because they represent the same kind of construct that we
have available to us in C-Sharp and many other
programming languages. This point, I've already created the new project so
you can hit Pause, go ahead and add the
new console project, and we're calling it methods. So once you've done
that weekend, continue. So we have two types
of functions in most, if not all programming
languages, and they are void functions and value returning functions. So avoid form shown
basically complete a task and then moves
along our exits. Then the value return
completes a task. Yes, What's it also makes sure that's it returns a result. You can always go into
on getting something back from the value
returning function. The void function
doesn't return anything, you just do something
and moves along. In defining functions,
you can end up with three parts and BSN new contexts you may not
necessarily need one. You would end up
with a prototype, which basically
defines the function is a function header
to see the type, whether it's void
or value returning, and the name and
parameters to be expected. All right, that's what
the prototype has. Then you have the definition, which usually has the code. It contains the code
block for the foam shown. What is the function doing? It's executing something. Then inside of the code, we would have the function call. This is the actual code that
makes a function execute. Because our function
will sit there and onto the quality
won't do anything. So that's really what
the function colleagues. So we have the
prototype which is like a definition, which, which is kind of
optional one you may not necessarily always
have to do a prototype, but you always have a definition and you always have
a function call. And this is the key part. Now we have been
interacting with a few functions upon
to know in our, in our code where we wrote
Hello World and everything, our write line,
that is a function. All right, So when
you say console dot write line that is a function. The name of the
function is right line. And I think by definition
it would actually be a void function because it's not returning anything towards
console dot write line. And then functions are usually characterized by the open
and close parentheses. At least that's a function call. So this is what we
call that function. Its definition is somewhere
inside of this code file. And if we look at it, you'll see that it is
actually a void function. Alright, so you see all
of that comes together. So WriteLine is actually a void function as
opposed to read line, which is another function in the code file, which
takes parameters. Sure, but this one is
actually a value returning, which is returning a string. When we talk about the type, it's either void or
it has a data type. As scientists we
know the data types. It can return a value of a specific datatype after it has done its function
like what the red line does. Or it can be void where
he just does something, doesn't send back any value. You'd be wondering, okay, so
why do we need functions? Well, functions are good way to package repeatable code or
code that we need to repeat. All right, so one of the foundations of
programming is dry. That's a principle. When we talk about
the DRY principle. It means don't repeat yourself. In a bid to not repeat yourself when you
find that you have to write the same block of code to do a particular thing
more than once, then you would end up
creating a function. So instead of writing
the four or five lines over and over and over at different parts of
the application. You just put it
inside of a function and the call where necessary. So that's exactly what they did with console dot write line. Console dot write line probably has a few lines of code in it. Instead of asking you to write those five lines of
code every single time, they just tell you,
call this function whenever you need
this functionality. Let us start off with an
example of a void function. What if I wanted a foam
shown that basically just bringing my knee to
define the function one. Once again, I need that type. In this case I'm just
printing the name, so I just need void. Void is the type. And
then I'm going to give it the name, print name. I didn't, I need to tell it
which parameters to expect. So parameters are listed out. Inside of the parentheses. If I don't have any parameters
that I'm passing in, then I don't need to put anything inside of
the parentheses, but apart and disease
are not optional. So by seeing void print name open-end towards
it knows that this is a function and then I open
and close my curly braces, which gives me the ability to
start writing, watch it up. And instead of this function, let us say I wanted to keep it simple and I'm printing my name, so I'm printing
Trivago Williams. That is all that I want
this function to do. So imagine I had to print true or Williams
multiple places. Fine, Fair enough. But then what if
I wanted to print all my full name or
just my first name? That means in each of those several places
I would have to go and change this one line
of code multiple times. Or I can know, say print name I can call this function will anywhere
in my application. And then if I needed to
change the way it operates, I just go to the function and change the code one, please. Repeat yourself. If I call this ten times,
it doesn't matter. All it's going to do is go to the function
on Meet the call. If you tried to make
a function call for a function that
does not exist, you of course will get an error. All right, Well, you can
always have a function that exists and it's never called. Then they'll share
that green line, which is just saying that it's
there but it's never used. Are you sure you need it? All right. There's another principle. You ain't gonna need it, or YAGNI for short. You aren't going to need it. In this principle. Basically, say, don't write
code that you don't need. If you have no purpose
for this function, don't write it,
doesn't have it there. Clogging up your code file because you're going to use it. All right, So let's get back
to the film Shaun costs. So we have the function here. The function call will be
just calling the name of the function alongside
the parentheses with any parameters as needed. In this case, I have
no parameters defined, so there are non required
when I made that call. Alright, I don't need to
plot passing any value here, just like Console.WriteLine,
there are times when new console dot write line with
no value being passed in. And there tends when we console that red line with a
value being passed in. Alright, so this is one
that t experimental. This is one that does
not take parameters. We look at that in a few. So if I execute
this application, it should just go
right ahead and call my print name function. There we go. So this is the main code file, whatever we write in here, yes, but whatever method
we put here that is only going to get this block of code once
it's enough function in R, Once it's a definition
of a function, only gets executed when
there is a function call. So that is why we're seeing
shrimp or print it out. However, if I was to remove this function call and try
again, nothing would happen. Literally nothing would happen because there's
nothing to happen. There is a function
that was never called. There isn't a lot of code
to be executed anywhere. That's his whole you call or interrupt with your function. All right, so I'm going
to leave the examples of void methods are
void functions here. Really and truly. Once again, it's just doing something. It's not returning a value. So if you wanted, say, a void function to do some math, like odd numbers, so you have situation where you'd want
to pass in a parameter. Alright, so let us say you
would want to see audition. We were talking
about the potential of doing a calculator
up earlier. What if you add void addition
and then you wanted to add two numbers so you could put in parameters
and relaunch. We'll go Allah parameter
is a variable that you are defining inside of
the parentheses in the function definition. And the value that is
passed in will get assigned to that value in the
same order that's defined. Let me do that in
a code example. Let's say we have to
take two numbers. I'm taking int num1, int num2. Then I want to print to
the screen the sum off. Then look, oh, we can just make it look nice and
professional nodes. So I went to use interpolation
and put in normal one. And norm tool is, then I can just put in
the math num1 plus num2. So we're not returning a value. I'm just going to
call the function, give it two values, and then it's going to do the process and
print the screen, but it's not returning
anything to call that, No, I would say addition. And passing two values. And notice now I'm getting
that into the hint, putting a value
for num one side, do that and press comma. And then seeing putting
a value for num tool, I do that and continue. And then D2L I would call it if we were to
bolster this a bit, no antique user input
for num1 and num2. And what I'm going to do is
jump back over to one of our previous examples where
we asked for two inputs. I believe you did that in
our OK. What do you think conditional statements asked for number of oranges and
number of apples? I'm just trying to work
smarter, not harder, right? So enter number, I'll
just keep it simple. Number one and number two. Then we are calling this
number one, number two. So i'm I'm doing all of
this deliberate with my naming because I'm
taking variables, I'm taking values from the user. I'm starting to number one. Number two, I have defined in my function
that is taking num1, num2. However, all I have to
do is pass the value for number one as the first verb and devoted for normal
to the second verbal. So that just goes to
show that the naming here has no correlation
with what is here, because I can pass in the
literal values one for num 12, for num2, I can pass
in my variable. That's I got from
the user for number one and the variable
for number two. The fact is that by the time it makes this call and goes over, whatever was first in the
list goes by instead of firsts into this
relative or a second in the list bands with what
sick on in the list. Then the void method is going
to go ahead and execute. Right now we're
learning about one, making void methods to defining
them and calling them, and then three, writing
in our parameters. So if I run this application, yes, it's going to print for VOR because we have
the print name. Sure. So it does that. They'd notice it continues
with the rest of the program. So it went, it
called the function, the function and said
then came back and then continued node to see
Enter number one. I'm just going to say ten
and then enter number to 20. Then it's going to go and
meet the Photoshop cough, where I addition passing
into an end-to-end t. So the sum of tin and
twin T is the math. Then he claimed buck
realize there's nothing more and then
they can fix it. That's really how
functions work. So imagine that you had to write this kind of logic
multiple places. You don't want to have to be writing this every single time. They asked one num1, num2, num1, num2, num1 num2. You write the method. Yes, you can prompt the user
and all you do is call. If you're writing
a calculator up, you can create a method per operation that would do
something very similar to this. And you called
Operation accordingly.
16. Methods - Value Returning: All right guys, So
we're continuing our adventure into methods
and hold their work. We already looked
at void function, so it'll taught one that takes no parameters and we looked at another one that
takes parameters. The key thing about void
functions is that they just weren't operation
and then they end. So if we wanted to know
the end of that operation, we would simply maybe write Console.WriteLine and C
and off void function. So when we press Start, it is going to the IIT
being the compiler, is going to go in the order
that the code was written. This is going to see that this is the definition of a method. Don't do anything in here. This is the definition of a method don't do
anything in there. However, the first
line of code I need to execute is a function call, which means I need to
go find this function. I would have seen its
definition already. Do what it says I should do. And then that's the
end of this function. Then we go on to get
two numbers as input. Once the user has
provided those will go ahead and we see that this
is another function call. It's asking for
these two numbers. So it pauses, it goes, it finds that function
pauses into two numbers. Those what it's supposed to do. And then that would be the end of that
function call also. Alright, so that's this
all void functions work. No, we're going to take a look at value returning functions. No bother returning functions do exactly what the name suggests. They return a value, something gets
returned at the end. Often operation. Let's say we wanted to find
the largest of three numbers. So the largest off three. So I'm going to introduce one other prompt here
for a third number. So we take number
one, number two, then we find out what
the sum of number 12 are or is rather what
does sum of them is. But we're going to have a third variable
here that's going to take our third
number at the end of this operation or by
the time it hits this. Now we should have
three numbers. Now I want to know what
is the largest number. I'm going to create a
method that's going to take those three numbers and
return the largest one. When we talk about
value returning, you have to start off
with the data type. What type of value
am I returning? Well, in this case, if I'm comparing three integers, then the expectation
is that I went to return the largest of
the three integers. So my return type is
going to be an integer. If I was returning the word or a name,
it would be strings. So the same way
that you consider what datatype you should use for your variable
is the same. We consider what data
type you use for your return type
from your method. I'm going to say int. And then I went to call
this one largest number. And very similar naming
rules apply with methods, except we tend to use the pasco case for methods
as opposed to CamelCase. So remember plus Co
case means that you capitalize the first
letter as well as every other first off
the other words in that block of text,
that's your typing. Alright? So int largest number. Then I'm going to
tell it that it needs to take three parameters here, Visual Studio suggesting
that IT num1, num2, both int button went to
introduce a third one, int num three, because
I wanted to take three numbers and as many numbers as you need you
list the most of course. No, you'll see that
I've already line here. I need to see in that not all
code paths return a value. So I like explaining what
these different errors mean because sometimes it's not
very obvious, right here, ETC. And that we have
a method that it has seen is a value returning, but it doesn't see
anything being returned. All right, so that means we need a keyword and that
keyword is returned. After return, we need to stay
at what we're returning. So here I'm just going to
show you if I say return 0, it is satisfied because
0 is an integer value, an integer value, and it has the return statement
for the integer. So it's expecting me
to return an integer. I have this statement seeing
I'm returning an integer. If I put you in a
string like my name, then I would get an error
because this is not an integer. So it's good to see you
cannot implicitly convert type string into int y. This is a string, but it's expecting an integer
to be returned. Those are little things
that you have to look out for when dealing with value
returning functions. So let's just go through
a quick exercise here. I'm going to show you another
thing called nesting, which is where we have
one control structure inside of another. So R1 type off construct
inside of another meaning. Here we have a method, but I'm going to
put an if statement because the only
way to know what the largest number
here is is to use an if statement or some
form of decision logic. I went to start
off by seeing that the largest number
is equal to num one. This is just an assumption because one of them
must be the largest. So I'm going to assume that
no one is the largest, but then I need to test if the largest number is actually
greater than num two, num three, then if either one
of these is greater than, that will be the
new largest number. So I went to say, if just the same way I said it, if largest, let us see if largest
is less than num2, that obviously largest
needs to be equal to m2 because the largest number
cannot be less than name2. If it is less than, then I need to reassign. Know num2 assumes the
role of largest number. Then I can say if again, if the largest number
is less than num three, then Visual Studio is going to assist me again and
let me know that the coordinator at is
that the largest number is not equal to num three. Notice I have two statements. I didn't use if else, if or whatever because I
needed to do this comparison. And then these comparison, if I did an if else, the only way it would go to this comparison is if
this one wasn't true, you'll want to be
very careful and very deliberate with those
kinds of things. If else means that I'm
going to do either or. So if this one is true, then I don't need
to look at this because this one was true. However, if I do completely different and individual
if statements, it will check this. If it is true, then it
will go into the body. Then it will check this. And if it is true, it
will go into the body. So this is saying that if
normal one starts off as five, and then it tests to
see if the dot five, which is the largest, that's the value five, is less than num2. And let us say num2 was 105 is no less than 105 is less
than ten, that is true. Then no largest gets
the value of ten. Then it will move on to say, check if ten is less
than, let's say three. If it is not less than three, then this will not get
executed as this was not true. Then we need to return. If you said largest, then you are definitely correct. We need to return that value. So this is returning an integer which is deemed
to be the largest number. So the method you name it according to what you
know it's supposed to do. Yes. Well, you always have
to make sure that whatever logic you're
outlining in there matches the intended
operation and whatever value is returned
matches the expected icon. So you can imagine that you
wrote a method to calculate somebody's tax and
then you've got the calculation bit of it wrong. So every time you call
that calculate tax method, it was returning the wrong value and you are taking too
much tax from this person, that would not be nice. So it's always
good to understand what logic here putting in relative to the
operation at hand. And yes, I can sit here and show you examples where
you have to practice. That is how you get
better at this. So you get better at
making these decisions. We have determined what the largest number is and
then we return it null. I can do a function call
for largest number. After getting all three numbers. I cannot just say largest number and then pass in number one. Number two, number three. All right? No, when I pass this
in, what happens? I'm just going to
call this method. It returns largest. And then what happens? We know that without
a Console.WriteLine, nothing is getting printed to the screen though the
console read line, nothing is being
taken from the screen as it's tons when it
hits this method, It's good to come up periods, go into the numbers
shores going to do this. It is going to return
largest water wheel. We have to bolster this
a little bit more. I have to actually put
this inside of a variable. So I can know C creates
a variable called, in this case result, and it will be equal to the results of this
function call. Just the same way
we've been doing the same thing here
when we ask for input. We say console dot read line, or we said String name is equal
to console dot read line. It's calling a method, and then it is assigning
it to this side. In the case of software,
we have to do conversion, convert to in 32, that is a value
returning function. All it's doing is returning
an integer, right? Same thing for it to convert
the char and to bool, etc. It's just returning whatever datatype it said it will return. That is why the assignment or the variability it's being
assigned to is very important. So int number two is
equal to convert in 32, That's method call one. But in where passing in
another method here that will. Give us the value that will be converted and then our science. So it's the same
thing here except where to want to write
your own method. This time, I'm seeing creates a variable of type
integer called result and assigned to it whatever is returned from this method
called largest number, which is returning an integer. Alright? So when we pass in
those three numbers, come up, meet the comparison, return whichever
one is the largest. And then when we return, that gets stored inside result. So no, I can see that
the largest number is. I'll just use interpolation
here and see results. Interpolation means I put the dollar sign at the
front of the stream. There we go. Alright, so nobody's
going to print the largest number is result. No. I'm going to show
you another way you could have done this. To use fewer lines. You could actually
just print that. Alright, let us see. We could say console
dot, write line. This block of string is, and then put in
that function call inside of the
interpolated section. That means it will go and
meet the function call. The value gets returned
and that value is what will be printed
inside of that section. So I'm just showing you, I'm always going to show
you your options. You could do it in
two lines like this, or you could do it in
one line like this. I tend to prefer using two lines because that form
MY increases the readability. I can clearly see that
this is a variable. This is how it's being set. And then this is
always being used as opposed to a longer bit
of text where I have to put on good lenses
to see that this is a function called happening inside of an
interpolated string, inside of this, inside of that, why it was still work. You also have to consider
the readability of your code because you might
be writing it for yourself. Nobody looks cool. Maybe take a break and come
back and you'd be like, what, what did I write here? Why did I write it like this? Or somebody else may have
to look at your code. And they weren't thinking the way you were thinking
when you wrote the code. So it's hard for
them to catch on. So you always have to consider those possibilities when
you are writing your code. So sometimes things could
be done in one line, but I split them all just
to see him because it makes it easier to
digest and read. All right, so let
us test this out. I'm going to run
without debugging. Then let us put in
over three numbers. So I went to put in 1056 and it's going to say
the largest number is ten. Hold it no. Because largest norm. Largest got the number
ten initially, right? That was my normal one. Then it said, if ten is less
than five, which it is not. So it skipped over that. And he said, if ten
is less than six, I think up within it is not. So it skipped over that. So it returned ten and then stored tin
instead of results, which is what got
printed to the screen. That's his whole value
returning methods work.
17. String Manipulation Functions: All right guys, So we're
kind of coming off the beaten path of
exploring the B6 of C-sharp development and going into a few more
advanced concepts. And one of those is a string manipulations
with a string on D8 time manipulations
because those two datatypes, as seamless deme look, can cause a lot of problems
if not handled properly. We're going to be
looking at some of the ways that we can
manipulate strings. We've been doing it for us
to some extent upon to know, but we'll be looking
at some others. And we'll also take a look at dy dt time values and hold those can be
manipulated as well. Let us start off quickly. We know how to print, so we know how to
print the string. I should say print to
screen here, sorry, that is console dot write line. If I'm to print one thing that I just see, the one variable, in this case, Visual Studio is actually helping me
with concatenation. So that's actually the
code for the second one. If I just wanted to
print first theme that we know what we just
say, console.log line, please in the string
or the variable, or it is a static
stream that we pass in. Whichever one works once it's a string variable,
well, frankly, it can handle almost
every variables as far as we've seen, all the simple primitive
types at least. So those are your
options to Print Screen. Now when it comes
to concatenation, we know that we can
use our plus sign. So here I'm seeing
Console.WriteLine, the value that is in firstName, space in-between, and
then another variable. We also know that we, if we wanted a static
string plus the variable, we would see my full name is. And then we would be able to
concatenates the variable. And then as many other
variables as we would need, we just flows with
the empty space. So the relevance of the
species that prints, it's going to print
exactly what it sees. Its printing my
space, full space, namespace is space,
then lastName, then space again,
then first name. So that's the importance of having that little
space in there. Alright? No, Although way as we
have looked at doing concatenation would be using interpolation where instead
of using the plus signs, we can use our curly braces. We just sprint one
block of string. But then of course
you notice that I using variables here, but they are still orange. I need the dollar
sign at the front. I'm gonna take note that
it's an interpolated string so that it will know that
anyway sees the curly braces, that should be a verb in there. All right, so this is
another way because now it's sprinting my space. Full name space is then
the last name space, the FirstName,
lastname, firstname. Usually you'd have like a comma, so it'd be williams
coma for VOR. See that even though I am typing in-between
the two variables here and the
interpolated string. It's going to see it as one
being contiguous block, just going to super impose the variables that it knows
it needs, need to be there. Generally speaking, these are the most common ones that you'll find in water and C-sharp. But then the
alternative, of course, like we had looked at
was using placeholders. So we're not using
any interpolation. Instead we're using 12 and as many other police
folders as needed. And then we just put in the variables that
match in that order. So first name, then last
name, last name here, would much please holder 0, firstname much as
placeholder one. Those are ways
that we can handle concatenation in strings. No, there are times when you
may want to know a whole long is how many letters
are in the first theme, for instance, if I
wanted to do that, I would want to find the
length of the string. So I could say int, let's say length is equal to. And then I can
call the variable. And then look at that
Visual Studio is contextually autofill
in that formula. So it's going to say,
get me the variable, look at the value in it
and telling me the length. My name has seven letters in it, this would just return seven. This is a property on that
string called length. When we get into
classes and objects, Yolanda, similar time
you buy a property. So don't worry about that. The use of that
keyword just yet. That's it. So you would see
the length of a string. If you wanted to print
that total tough course, you would say something
like console dot write line I used just say length or let me just make
it a bit clearer. Someone to use the oldest style all the way off concatenation. This would be seeing
your name is. Okay. Let me make this into
a whole sentence. So this would be interpolated. Let me make it into polluted. Your name is, and I can put
in that many letters long. Close the stream, and that's it. So it's printing out
a whole sentence with a variable smack dab in the
middle of a literal string. Now, let us move on
to something else. What if we wanted
to replace letters? So let us say I wanted a new name or I wanted to see what my name
would look like. If I replaced all of the
t's with the letter key. I can see a first name, dots. Then if you do the dot, you see a number of what
we'll call methods. We looked at methods already. Just like we can
say, console dot, write line is our method. You can actually on
any string variable c, variable dot, and then you'll
see a bunch of methods. Some of them are
useful, some are not. Just to see him week
and have this property. So if I say Replace, then you will actually
see that there is a method they're
called her please. And it wants two things. It wants the old
char or character, wants a new character. I could see the character t. I remember characters use
single quotation marks and only one value can go instead of a single
quotation mark. Alright? So that's
his character. And I want to replace it
with a new character of k. This is going to automatically evaluate this string
and go through and find all the T's and replace
them with K. Alright, I went to pause here
and do some testing. So let us change over our our project is
through manipulation. I hope you had
already done that. I'm sorry, I didn't points
he told earlier but create your project clustering
manipulation. Or maybe you already created
it with a different name. That's not really
the big points. Just make sure you're
running in a new method, in a new project rather. Then let us run. So here are the printout. So we have traversed string
being printed. There it is. I know we have all of the different ways that
we did darkened cutting concatenations and it will have your name is seven letters long. And then we did the
replace with new name what I did not do a sprint the end results someone
to see your new name is, and then I'm going
to see new name. So that's Australia.
That again, right. So here we have your
new name is Chris. Oh, I don't I don't like
the sound of that one. Right. But now you see
what it looks like. So even if I wanted
to remove litter, let us see the eye. So my name is quite unique
because it's really trivial. What's an eyes in there? And it's Greek and it means
reservoir of strength. Both a lot of people, when they call them by name,
they just call me trouble. I don't really make a big deal out of
it, but that's fine. Let us say I said new
name, one is firstName, replace the letter I, the character i with nothing. I could see it with a space. Because really and truly a character can't
be empty like that. That's why you're getting
that green, that red line. You can't have an empty
literal character. And there's no car
dot empty like older is a stream dot mt, is there? No, there is not turret. So let us just see, I'm going to replace
it with a space, so a species,
actually a character. Alright, and then I went to see your new name is new name one. So let us see what that
would bring up. There it is. So no, it has that
space where that I was told that replace
operation works. Alright, so let us move along. The next one would
be to append to other string variable
really and truly that would look just like this concatenation here
or any one of these. Because we see that you can actually just
combine them, right? So I could actually
just see var, sorry, string
listening gets so var, string full name is equal to one variable combined
with another variable. And that's it. Or if I just wanted to
combine them without anything in-between yourself
to C plus, alright, so once you put a
plus sign between a string or between two strings, would that air literal
strings are string variables or a verb that
under literal string, however, the combination,
you just use that plus sign. So that wouldn't give
me my full name, which I can print. Your full name, is an employee
until the full name there. Now the next option would
be to split the stream. And you can see
here that I started writing the code
would have an error. And the error is saying that
it cannot implicitly convert type string with those boxes, with those are two square
brackets tool regular strings. So in other words,
this would actually give me a collection of strings. So here I'm seeing
the full name. That'll be traversed space
Williams and split it. Wherever there is a v. As many visas may appear in
the one block of string, it's going to split
it accordingly. So if there are three V's, then he's going to split it to the left of one of the V's, to the riots off that V and into the
write-off the next V, That's pretty much
what it's going to do. So in Trevarthen Williams, There's one VC would
end up with that side. And this side as two strings
or two blocks of string. So I can't be storing two
blocks of string in one string, which is why I said it
needs the square brackets, which means we're not
quite ready for areas yet. We're not quite ready. We're going to explore
it in more detail. But I went to show you what the outcome of this
operation is regardless, the best way to iterate through an array is to use a for-loop. So usually say like four. And then we already went
through a for loop. For int i, which is my
counter, is equal to 0. I is less than the length, meaning how many times do
you want it to go through? And then I plus, plus. In this case, I don't know how many blocks of
string I'm going to get. I don't really know
because I don't know what the full name
is going to be. The full name could
have been user input where the person Internet
stream with no v's in it. So there's nothing to split
equal to 053 V's in it. So R5, I don't know. For the length variable, I wouldn't want to put ten
because then I went to go ten times and maybe only
needed to do two times. And then that is actually
going to cause a problem. Again, I'm not
getting too much into detail with Ari's just yet. However, an easy way to
navigate this would be to take this array of strings c. Instead of giving
us specific number, I went to see the
IRI dot length, whatever length, whatever
number of elements are blocked. So Stream come out
of that operation will get stored here. And each one is going
to call Windows one. Each block is going
to be one block inside of this
collection of strings. I'm saying get me the number of blocks in the
collection of strings, and that's the number of
times I want you to loop. We're going to go more
detailed into that later on, so don't worry about it. But right now I
just wanted to show you the console right line. All I would have to do is say, give me the split name value
per subscript that is in I. Right? I'm just going to
print that out. And then when I looked
at it in debug mode, I'm going to see tray
and then, or William, that's really printing
out here because that v would be the
splitting points. So what's the lift of the V? What is to the right of the v? More, V's more splits. So that's what I
said. We don't know how many splits we would get, which is why we tried
to keep it dynamic. Once again, we'll
be looking at that. The Concept of
Irony is later on. Lets us move on to the
string comparisons. A string comparison me occur
when you want to compare something that's a user
entered to a potential option, or you want to compare
two values to variables. There are number of reasons
you need to compare strings. So a very common comparison
that you may end up doing is checking if something
is empty or default. Because like I said from the very beginning when
I defined the string, Let's say variable,
test variable. Remember, I defined the string and I didn't give you
the value at this point, it is what you call null. All right? That's another point. You may have given it. This. It's not null, but it
doesn't have a value. So it's not, no,
it's just empty. Just isn't the same way. That will be the same thing
as seeing string dot empty. All right, so these
two would actually equate to the same theme. Let me just put
that as a comment there to show that that
means stream dot empty. All right? And in other, in some situations
you don't know if the user actually put
in something valid or so-and-so you have
to check if it is true, if the null, sorry, if the string is null
or if it is empty. There's a built-in method
where when you type the data type string and
then you see a full stop, you actually get a bunch of
methods also looked at that. You have is molar empty and you have
Ismail, our whitespace. Whitespace would
be more like this, where you just put
a space in there. There's nothing there. I wanted to say null string. That's on those string. This is an empty. String, then this white space, straight, just so we're clear
what each one represents. Instead, I'll see you
in test variable. You can see is null or empty, which will either say yes or no. That's going to return
a Boolean value returning function
that's returning true. It is not empty or null, it is not knowledge
is not empty. So they need parsing the string. So you could see no string
here if it is null or empty. And that's where you would say, well, I have an ISI here. I cannot proceed. And actually, when
you look at that, so in C-Sharp, in later
versions of C Sharp, it's actually preventing
me from using this without first
assigning a value c. I would have to explicitly
see here that it is null in order
for it to proceed, that you could just assign it
as null, not assigning it. The compiler is not going to
take any guesses and say, Oh, it's no, it's
not limited to that. So you have to
explicitly say it's not. Like I was saying, you could
actually, if it is smaller, empty and it shouldn't be null, you could take oxygen. Those are considerations
that you have to meet. Another easy you'd
probably want to. Let me just split in
our console right here. So they now see string is null, one citizen alert empty. Then we will just see it is not. The reason you would
want to compare strings is if you wanted
to compare two of them. So the same kind of equivalence
that you would probably apply to a math operation
can apply to two variables. So I could see if firstName
is equivalent to lastName. Then take some option. Names are equal. That's something you could do. You could also see if
they are not equal. I'm not equal would
be exclamation equals sign, not equal to c. Names are not equal. So those are other things
that you would want to do another week and compare two strings is to see if then string has a dot-com
peer method on it. Then he can policy and the strings that you
want to compare. So here it's showing you
that you have a few options. You have nine overloads. We be looking at overloads
in a few bullets. I can pass in a string
number one and number two, which is last name. And then it's going to return, if they are, sorry, actually this is
returning an integer. So let me try that again. So I would actually say
int comparison results is equal to the string computer. That's integer. Then it saying that
a value that's less than 0 shows that are
actually a valid, that is not 0, shows that they are different. Pretty much results or whatever variable value you
get from this comparison. If it is equivalent to 0, then that goes to show the names are equal and I can
just do an else. Names are not equal. So this is another way
that you can check that there are
several ways I cannot exhaust all of the methods here and I can assure you
as you go along in your professional or even
recreational pursuits, you're going to find
different ways, different needs for
string comparisons that cannot be covered
in a single course. So it's audible
practice and exposure. Next, let us look at
how we convert values. So strings. Typically speaking, whenever you want
something to be a string, you would give the
variable the datatype. Um, let's see, convert, converted value,
converted string, it's called a converted string, is equal to string dot empty. I'm just initialize
it to stream dot mt. Know if I have a number
that I want as a string. This number could be a specific number that I
wanted to type like this. Or it could be in a variable. Int number is equal
to this value. And I wanted that value to be stored inside
of this variable. I cannot just say convert this
string is equal to number. We've exhausted that. We can't just take some numerals on police
inside of a string. It does a word that we, instead we have to
convert it the same. We will have to convert
tool in 32 from strings and see what
we have to convert toString from any
other datatype. C-sharp makes this very easy
because there is a method on every variable that you will ever interacted
with called string. It's so cool that even the very numeral that you
would've typed up, you could see a dot to
string onto the end of it. So just by typing this numeral
I and then putting dots. So string, you would actually convert it to string for you. Any variable, whether
it's a Boolean, a number, a double, whatever it is, you can also convert
it to a string. But just remember that
when it is a string, the plus sign is going to work entirely different from how it would've worked if
it was a number, right? So remember that plus
signs when we're dealing with string
concatenation. Whereas plus signs when
you're dealing with math, will actually do actual
math and add the numbers. So be very careful and
cognizant of that. But for norm went to pause
with string manipulations. You can go through,
you can experiment, see what other
methods are there. Other cool ones like you
can see upper and lower, you can do joins. You can create. If you just write
string1, press dots, you can skip through and
see the other methods available to you on
the variable itself. You can actually let us
say fullname and I see it. I can actually see two upper if I wanted everything
to be uppercase. I could actually say to lower, if I wanted everything
to be lower keys. There are number of methods
that are there to help you manipulate a string without
too much work involved. So I went to leave you to
experiment and explore.
18. DateTime Manipulation Functions: All right guys, welcome
back for our own tools. So Gondwana was
all about strings. I intended to do both
strings MD time, but you'll see that
the activities to cross over into another lesson. So that's fine. We can catch up here. So I haven't created
the project has yet. Go ahead and do that.
You can follow along with me or hit pause
and do it on your own. So of course you have to
go create our new project. We're going to go with
date time manipulations. Then we hit Create. So now that we have
our new project, let us see what tasks are
there for data manipulation. Here are the tasks that
we're going to go through. Much like string
manipulation and data manipulation is very deep, very extensive, and
very situational. So there's certain things
you mean never end up doing in the foreseeable future. And there are certain
things that you may want to do that I'm not going to necessarily showy
in this time-space. Let's just work through it. I'm just going to vary
in some of these things. We can additional time. Sorry. We can just work through it and see how best we can on the sun. Firstly, a datetime represents a datatype in C-Sharp and it's sounds like
primitive datatype. So when we talk about
primitive datatypes, we were talking about
int double stream, the very basic date, time, or a more
complex data-type would be a combination of different datatypes that extends the functionality a bit more. When we talk about
dates and time, talking on boats, a datatype
literally called the time. Then we give it a name. So I went to call
it. Then usually you want to initialize this. And we initialize this is
by saying I knew the time, open and close parentheses. So this is the way you would initialize a datetime object, not variable, what object? We're going into the
realm of classes. So notice that it is
kind of that teal cyan. I'm not sure what color that is, what it's the same
color that console is. So Console is a class. Datetime is also a class, and we'll do string is blue, int char, all of those are blue, they're not the same color. Those are not classes. That's it. So you initialize
an empty date time. Now let us look at how
you would create one. So let us say I wanted to
create my date of birth. I could see a date, time, or date of birth is equal
to a new datetime object. And then I can actually pass
in values that I would like. So if you look through,
you'll see that you have a number of options.
Here's the one I want. I can pass in an integer
year, an integer month, integer d, the year I was born, Let's say I was born 1980. The month let's say
I was born December. That's the 12th, right? And then today Let's
was born on the 35th. So of course there's no 35th. But it's not going to give
me an error here until I try to run it and it
gives me a problem. So I went to do a
console dot write line. I'm trying to print out date of birth and notice I
can just pass it into the right line because
it does accept more complex objects also have the primitive types that
we have been working with. My D OB is I'll just
use my plus sign here. If I tried to run this, let me change it to date manipulations
and try to execute. Then, wow, we're seeing that
we have these exceptions. An exception is an error
that usually occurs in our program and kills
the program pretty much. If you're using
Google Chrome or even Visual Studio and you get one of those areas,
even Windows. And you have that
blue screen of death, all it's doing is
hitting an exception. An exception is one of those irreversible arrows that assist him did not account for. In this case, it is
saying that these are not representable bedtime
the values that I presented. So yes, the area is fine, the moment is fine,
but this is not fine. So if I put it to the
31st and try again, then I should get
that print adults. So notice just by passing
in that datetime object equals able to print out the
12th to the 31st and 1980. That's what's my date of birth. All right. Nice and simple. Now, what if I wanted the current timestamp
so I can see a dead time. Let's call it no. I wanted to know what
is the time, right? No. Then literally I
can see a date, time, dots null that will
go to your system, gets the timestamp as at the moment the
code was executed. And then that will be stored inside of this
object called null. So the time now is is, and then I'm going
to just print no, Let me try that one. As at the moment
that code executed, the time was the date, was that the time was that I'm using are my
time on my system. It's going to reflect
what the system time is, an even the layout
of the date format. All of that is affected by the system settings you have
when the school is executed. So if you're on British
Standards or four months, it may look slightly
differently in terms of Using De Montfort instead
of month, day, year. And if you're not
using armed time, then your time We'll different. Those are things
to be mindful off when it comes to printing
out your date-time. Know what if we wanted to create the DateTime
from a string. So to do that, it means
that I would've had that kind of string
representation that was just printed out. And I'm using that to engineer. I did time value. So I can see datetime, time and I'll say did
from string is equal to. I can do a datetime parse. Just like strings. There are
number of methods involved. But it wants the string. What is, what is the stream
that it's supposed apart? So what if I said one
slash 31 slash 2221? That should be
January 31st, 2021. That sees what I want to parts of other parameters
that you would add like parameters to let it
know that culture in full. Let me see if I remember how to type this in variant culture. So there are
different parameters you put in to let it know. Don't be specific
to the culture. Here. I just added that using
statement for this one. It's actually a static class
just like console or LN. So I'm saying culture in
full, in very uncultured. So it says the culture
is, it's an independent, it's culture independent
because like I said, the format sometimes will
differ based on your culture. So just by doing that, we can make it independence. No, I did something and
I didn't explain it while typing all its
culture in full. Udot seen the red line, which means that
I'm trying to use a method that's from a
library that is not included. Remember we have
to add are using statements whenever we
want to use methods from, from libraries that are
not already included. So I had to do Control dots. And the suggestion was I can
add the using statement. So by doing that,
it automatically went ahead and added it for me. I know I can access that method. I'm just showing you once
again different variables. I'll see the time. The deed from string is, I'll just print out
the ID from string. Let's take a look at that one. All right, so doing that
really and truly it took it, it parsed it into daytime and then it really just re-post
it back into a string. All right, so that's
just an example of how you can convert from the actual string
into that actual datetime. Datetime is an actual
datatype that you have to be mindful of when you're especially dealing
with a database. Because you'd be accepting
string input from your users if they were to enter it in through the console, we'll just store it
into a database. You would have to have
it as a date time. So that is one of the other reasons you'd
want to be careful. Now what if we wanted
to see what is an hour from the
current time, right? So I already know. What if I wanted to add an hour to see what time it would be? I can always say no dots. You have options to add hours. You can IDs, you can add
milliseconds, minutes, months. He can add almost any
construct of time. Existing datetime variable. So I can see no odd OA. So if I just wanted to
see one hour from now is one hour from null is, then I can just see no dot
add hours prune told No. It's already gotten up top here. He knows what time it is, no. And then it's going
to calculate, well, what is an hour from now? I could see one d from
NOW is no dot add days. Once again, exploration. So let's see what
that looks like. When we get that, we see
that null is 1241900 hours. One hour from node would be 20. $100.1 day from now will be the 25th with the same
timestamp as it was. No. Alright, so all of those
calculations are brought to us courtesy of these methods. Now the next one in the lineup
is dead time from ticks. But I think I went to invert
it and we'll just say ticks from the time I take. Or ticks basically
represents the number of clock ticks since
time was being, let's say calculated
as at this moment, if I say console dots, right. A time numeral, pretty much if you were to give a numeral
value at the time, he would be in the form
of Dijkstra is I could say no dot ticks. It gets the number
of things that represent the date and time off. This instance, this moment, there had been
this many ticks on o'clock since we started
calculating time. That's pretty much
what ticks would be. Before I test that takes, let's just jump into
data and time only. So dates only and time only our brand new constructs
and C-sharp tin where there are times
when you wouldn't necessarily want the time on it. Because here d itself, birth wouldn't
necessarily about time. Dates of birth is just
the year, the month. The right. Of course, you could
add on the seconds, minutes, and hours
if you wanted to. But we don't necessarily want to do that because you see
here if I keep on typing, you will start seeing he didn't hour into minutes and seconds. So that's when you talk
about date and time. That would actually be 1980s, the 12th of the
31st of December, 1988, hours one minute
and 35 seconds. There tends to be that precise. All right. I could actually see a date only is equal to date of birth. Let's say D, it's
only off birth. Just so we can how some distinguishing characteristics,
date of birth. And then I will take an
existing datetime object, that date of birth. And C, I want the D only. I see indeed, it would bring
me back just that actually. Actually no, date only
dots from date-time. There we go. I apologize. This is a fairly new construct, so forgive me. All right. So dot ds would really just give you about the dates, right? Let me, let me put
that one up here. Would actually just chop
off the zeros that would have been seen after the
whole datetime here. All right. So that should give me
about just the deed. Then the date only
construct is going to just give me back d It
doesn't care about at the time. But if I have a date time, I can say give me
the date only from date time and give you
that datetime value. In a similar manner, I can see time only. Let me get most spilling
right at time only. And I'm just going to say time. Time only is equal to, and I can do that time
only from the time. Let's try it out with no
cause I know that it will have an actual time attached to it given
that it's a timestamp. So let us try all
of those together. Here. I'm seeing
that even though I said it's still going to give me back the deet still
getting above the time component to the right. What we didn't do
a sprint these, so let me just add
the sprint only. Let me just add
these in quickly. And that will be d dt of birth. And then mix this one up. So let me fix this a bit. We're going to say time
only is time only. After putting in the console. Let's try that again. No. Once again, even though
I said dates of birth, give me the full datetime and then give me the
datetime dot d t. You can see that the time stamps are still trying to be added. Went through the Timestamp, went through adding an hour, and these are the ticks. So as at this moment, this whatever that number is, if you want to try and call it, you can let me know
what that value is, what that is the number
of ticks that I've gone through since we started
calculating time. Then the date only here
represents only the date. So this is literally
the date of birth, not the date, time of birth. All right. And then if I
wanted a time only, it is the time so that it took the timestamp and
give me just the time, not the date and the time. So those are some
ways that you can manipulate dates and
times d or advantage. I can assure you
once you're putting on functionality
that has to deal with Dietz and timestamps and particular things at
particular points in time, you will definitely
have to become familiar with some
of these functions. Once again, I cannot see potentially every
single one of them. There are things that
you have to explore, how to experiment with. They're also things like
returning the year. So what if I wanted to know
which day I was born on? I could say date of
birth dot d off week. So that will give me
back if it's 1234567, day of the week, he will give them the time off D it will give me the
day of the year, right? So there are 365 or
366 days in the year. Which day of the year was that? If I say give me
about just the D, it's the integer
of the D, right? So day off, day is different from the off week
because D is obvious. We know it's the 31st, however, the day of the week that
was the 31st in 19981980, we don't necessarily know that d of weak is
another construct. You have other things
like I just added them. So the off week, day of your time off the tick, the number of ticks. When I was born, and
the kind, right? So if I just one more execution just to see
what gets printed out for all of those values relative to date of birth. You
see that? I was born. I know it's there. It was the 366th
day of the year, so that was a leap year. So you can bear that
in mind when trying to calculate leap years. You can see the time off the, well, I didn't specify a time, so time of day would
actually give me something similar
to the time only. This was a number
of ticks, right? So if I did some math and I said This number of ticks,
this number of ticks, I would actually get
the number of ticks between the two times NO, and the date of birth. The kind here is on specified. So kind of would actually
refer to whether it's UTC or ESD or some local time, pretty much so I didn't make
any any specific locations as to watch timestamp
or time zone. This was so kind is unspecified. So those are some ideas when he comes to the manipulation.
19. Exception Handling: Hey guys, welcome back. In this lesson we're going to be looking at exception handling. We looked at exception
handling earlier. Are we encountered
an exception rather earlier when we were looking
at our datetime objects. And if you remember, when we create a
one-off those objects, we put in an invalid value. And then the program
gave us an error. It gave us a block
of text that I'm sure if I didn't read it
and explain it to you, it wouldn't have
made more sense. That's generally what
an exception is. An exception is an, a fatal error that would
occur in your program. Usually gives a
very nasty error. And it's not
necessarily something that you would want
your users to see. So just rock back in your
memories and think about every time you see Google Chrome crashing with a message
that makes no sense. Or that blue screen of death
on you're on Windows or some error that you encounter in an application where
that application and shows you an error. And then when you try
to navigate away, the application might crash. There are also times when that application does not crash, but it goes into more of an error state allowing you
to continue your operation. No, that is what we call
exception handling. The error itself
is an exception. Then the way it is dealt with is what we
call the handling. In C-Sharp and most
other languages, exception handling is done through what we'll call
a try-catch block. A try or the trip portion of the try-catch is basically
going to try the operation. All right, so it's
good to attempt to complete something,
complete a task. They catch section
off that whole block, is going to catch any fatal error or exception that may occur
while trying the operation. Then usually you will see, or sometimes you'll see
another block called Finally, meaning whether, whether
the trie or the, the catch was successful. Do this at the end of trying, maybe catching, do this. If you try it and do caught, then still do this. That's pretty much what
that find that it does. And then another keyword
that you usually see in exception
handling is true, which means the program
execution with the error. All right, we're going
to be going through a little exercise here
where we're going to explore under what circumstances
there might be errors. Watts we can do when there are errors and why we would want
to do those things, right? So I went to start off with
a very simple application. I'm just going to reuse one of these operations
that we have before. This wouldn't and
borrowed some code from one of our previous examples. You can just hit Pause
and replicate that, but I'm just saying
enter first number, storing it in num 1
second numbers during genome to hit pause
replicated that. No, generally speaking, you would want to do some
form of operation here. You'd want to say,
let's, for instance, say the quotient of num1 and num2 will usually be
num1 divided by num2. Simple math, num one
divided by num2. Then you'd want to
write that the quotient is whatever that
value is, results is. Then I'm just going to
concatenate that. Alright? So that is a typical
math example. I went to. Firstly, run this operation. And I already
created the project. Of course, as you can see, I call this one
exception handling. Exception handling. Let us run so it into the first number that's
ten divided by one. The result is ten. Okay, straightforward. Let's try again. If I do the same thing with 100. My look at this. In Visual Studio. I am getting this yellow arrow. Nobody went through
debugging already. We know that this yellow arrow usually indicates
which line you're at, especially after you've put in our breakpoint upon to know, I'm sure that you
haven't encoded this yellow arrow popping
up off its own volition. Then furthermore, you
wouldn't have seen this red X with the message
excepts on unhandled. So here it's saying
that we have a System.out divide by 0, exception, attempted
to divide by 0. So there's certain
things that cannot stop dividing by
0 is one of them. And the system, system
being your computer, being computers in general, cannot handle an
operation where you are asking it to divide
another number by 0. So this is what
causes an exception. And then this kind of
exception is what would crash your program because you see it just stopped running. And now I'm getting
this nasty error in the console saying
unhandled exception, then this means really, I mean, it probably means nothing
to you as a beginner. And that's fine. I've seen enough of these over my career to understand
what it is, seeing. What the point is that
you wouldn't want to display this block of text or anything here to our user because that makes no
sense to the user. So imagine how you
feel when you see Google Chrome or whichever application I keep on
using Chrome because Sita allows groom what
they handle it well, but they do sometimes
show some obscure errors. I think they've been
doing much better with that in more recent times. But sometimes you see
those obscure error. So those are the exceptions happening inside of
the application. And that is why
applications tend to say, do you want to do
that feedback program where when they
encounter an exception, you actually send the
message back to them. So they get this
kind of message in their email saying this
happened to a user, which is why you get updates. No application is perfect. They all have arrows
somewhere somehow. That is why you keep on getting updates because then they
would go into code and fix it. So let us fix this one. This is an exception that
we didn't account for. Let us fix it. So we can wrap
that inside of a try-catch. Like I said, the trie represents the operation
you're attempting. I can say try and just by writing trend
pressing Tab twice, it will generate this entire
try-catch block for me, this block is what we would try. I mean, couldn't be
as many lines as you need is the operation
that you're trained. Because sometimes you
have 235 steps in an operation and any one of those steps could
throw an exception. So then when we
throw an exception, for instance, or
division operation would be what we're trying. If this does throw an exception, then we have this catch block. Notice it says catch exception. Exception is the
general datatype used to identify when
an error has occurred. They're all just exceptions even though you'll
have specific ones. They're all at the
base of exceptions. You catch the exception. And then you can actually store this exception instead of
a variable or an object. So we can give it an
object II or III eggs or call it exception with all lowercase I went
to leave it as EX law. So basically we're saying
try this operation, catch any exception
or error and stored the details offset exception or error inside of this
variable called EAX. Then you will notice that you also have this
keyword called throw. Throwable is what actually
kills the program. So if I was to remove throw, you would actually
continue to execute. So when we try and it
catches the exception, if I don't through. So here's where I would
probably send an email it or write it to a log
somewhere where there yet. But I'm just giving
you the ideas because we use
applications every day. These are things that
we see anything there. But I'm just showing you
that it's really coming down to these little bits of code. Right here is where that application will show
you the dialog box and say, hey, you know, we sought out to you,
encountered an error. Would you like to send a
report to our developers? You say yes, then what they did catch showing
you the dialogue. If you say yes, then send off the error that was caught
inside of this object. That's all that's happening. Then at the end of the operation would print or whether
we can't print because this variable
exists within this scope. This is a good place
to talk about scope. If you define a variable
in-between two curly braces, that variable only lives for as long as when it hits that line and that curly brace ends.
That soak in a little. So that is why I cannot print quotient outside of
the try catch because this variable only exists
inside of this try block. So I can do anything I want
to caution inside of here. Like I can print it
inside of the tray. Once I cannot interact with
it wants to try is done. So that's why I cannot print outside off the tray lighthouse
trying to do initially. Those are things
to be mindful off. Let us continue now, if I say throw, then would end up
with the same kind of situation where it just ended
the execution altogether. You don't necessarily
want to throw, unless you are absolutely sure that that is
your desired effect. If you do want to
kill the program, when that particular
areas in concert, then you want to throw. So let us see that dividing by 0 is not the type of error
you want to throw on. However, if you
catch any other kind of exception and then
you do want to throw, you want different reactions to different types
of exceptions. Well, you can actually
have multiple catches. You can catch a specific one, let's say divided
by 0 exception. And then you can decide when it is a divide by 0 exception. I want to see illegal operation. Just say illegal operation. And then I could actually puts this exception message
inside its own object also. So let's call it EX. So if you catch this exception, I'm storing it didn't set off EAX within this catch block. And I'm going to print the
message that came with exceptions so I can see EX dots. Then we see message. When I do that for
semicolon at the end of it, I'll catch that
kind of exception. Otherwise, any other kind of exception through maybe
send an e-mail to the development team and
throw kill the program because I'm not prepared
to continue after that. All right? And then I'm going
to just say finally. Then in the finally block we
can just Console.WriteLine. I'll just say this is
the finally block. So that's bad. Let's try that again. This is the finally block. There we go. So what we wanted to
do at the end of this is whether we were successful or we call
it the division by 0. We want to save this
as the finally block. Or let's just say this is
the end of the program. Off the program. Let us try it out. One exception handling, run it. And then I'm going to
do a proper division. So I'm going to do 105
and then I get two. And you see this is
the end of program. So it tried, caught no exceptions and
then did the finally. All right, so let us
try that one more time. And this time I'm
going to try and get that divide by 0 exception. We're going to see that it
was still jumped down to the finally and continue
with the exception. So if I say ten divided by 0, it will say illegal
operation colon. That's my static
string that I typed. All right. And then the EX dot message would be the attempted
to divide by 0. So that entire misses that we would've seen
earlier when it would have thrown the exception would have been a number of
different bits of information all wrapped up in this object or
variable called EAX, which embodies the
entire exception and everything that went wrong. So a developer would want to see that
because then you would see the entire scope of the error and better
be able to fix it. However, generally
speaking, you'll get a nice message that is more human-readable
and human friendly, which is what you'd want
to maybe present to the user because then the user would get
an idea that okay, that is what I did wrong. However, they're not
getting the full shebang on the line number and
all sorts of details about the code that the property don't need our shouldn't
be seeing, right? Plus you don't
necessarily want users. You don't know who is
using your application. You don't want them to
necessarily know that that line number 20
are doing this, etc. So those are ways
that you can use to handle potential
errors in your code. Because we're not perfect. Sometimes you write
code and we don't realize that we're doing
things that might fail, especially when
you start writing bigger applications that have other dependencies that icon a database or on connecting
to the Internet. If you try to connect
to the Internet, the Internet isn't on, then you have to catch the exception and
handle it in a way that user doesn't feel like your program is not
written properly.
20. Arrays: Welcome back guys. In this lesson we'll
be looking at arrays. So you can start off by creating that project that we need. I call it arrays and then we
can get down to business. I have added some comments just serve as guides as to what we will be discussing
in this lesson. So an array represents
a collection of values. So we had a little
exposure to an array earlier when we were looking at our string manipulation
operations. And if you do remember
when I was showing you guys the split, there we go. Moodle seen those a square
braces or square brackets. And I said that
that meant it was a collection of string
blocks and we had to navigate through it and voltage at this point
because we would discuss it. Well, that time has come. So let me just close all of the tabs that are not
absolutely necessary. And just by middle clicking
the tab, it will close. So I don't know if I showed
you guys before nobody. If you have a tab, open our block of code open
and you wanted to close it. You can click the XES week and also middle click the
tab, it will close. Let's get started with our
definition of an array. An array is a
collection of values. For instance, if we
wanted to collect, see the grades or five students, then typically we
would have to end up seeing Edens green. One is equal to and then we have to do in grade
two and grade 345, etc. That is not very efficient. Then what if the
class goes to ten, then we have to come
back in our program and make ten again. The point is that it's not
very efficient when we have to have different variables like
this to store those values. So what happens is
that an array would be a collection of values
towards one goal. If I know I want grades, I can create a collection
type for grades. And then what we've
talked about, fixed size, it means that I know exactly
how many grids I want. Because if it's across all five, then I want a collection
of five grids. To define that
kind of construct. I would first defend the
datatype for this collection. And generally
speaking, you can use any datatype if you wanted
a collection of names, it would be a string
array, just like we saw, we have the string in
a string manipulation. Listen. It could be a double
collection of salaries, etc. So grades is just
merely an example, int, and then you use
your square braces, then you give it a name. So this is my
colleague, Norma Ari. I'm calling grids. And then I can see
z equal to new int. And then using square braces, I would say what value
I would give you C Visual Studio suggesting Maxwell it, That's
not what I want. I don't want the
maximum colleagues on because that could
crash your computer. But I did say I want five, so I'm going to put
five semicolon. This means that it's
going to go into memory and reserve
five spaces for me. All right, five space
addresses will be reserved in memory,
all called grids. And then what will happen
is that each one would have a number
associated with them. Since grids space,
one grid space too. But then the numbering for
RAs usually starts from 0. So remember World War
during the for-loop, we said we start from
0 and going up to one less than the exit number. So if the size of
the array is five, then the cone thing will be 0. Then 1234. And that's it because that
is 12345 species in memory. All right, so that's one part of arteries that
can come off as confusing. But once you get used to
understand, once again, the size is five, we're reserving phase species. But each one will
have an address from 0 up until one
less than that number. So if I see this like
a math equation, if n is the size of the array, then your array has addresses from 0, Tooele minus one. That's me trying to sound like
a math genius right there. All right. So five space addresses
would give you from 0 to n minus one, which is four. That's it for our ys. If I said ten, then it
would be 010 minus one. So 0 to nine would give
you the addresses. Now the thing with
this is that I have to cover and change the value here. It's quite easy. I mean, if I wanted it
bigger, make it 15. If I want it smaller,
make it five. Whatever. No more species I request here. When the program executes, it will reserve that
many for me in memory. Now let us, let us talk about
adding values to the IRI. So I didn't say that you would have subscripts are what
you call subscripts. Subscripts represent
these numbers which represent the addresses. If I wanted to add
the first grid, I'll have to say is
called the array by name. Then add my square
brackets and then call the address by value. So the first grade would be 0, which is the first
address reserved for me. And then I would give
it the grid value. Then I can do that again. Upgrades for one would be
two, etcetera, etcetera. So Visual Studio is just
making suggestions. These are not
necessarily the values because obviously
these are great. So maybe you scored 25, maybe students three squared 38, and maybe student
four scored 45. Notice this is Student
four because this is one, this is two, this is three, and then this is four. Even though the addresses are 0123 and I think we have
exhausted way up 12, no right to enter the
grades for all students. I would end up with lines, we can let this student 12345, all five students and
they'll have their grades. But I can see that
this is a bit tedious. If you think this is
tedious, I agree, because if I increase
this to 25 and you had to write 25 lines like this, it wouldn't be very efficient. Also, let's take a look
at what would happen if I added fifth grades. So if I said grids subscript
five or sixth grader other, I said you are 60. If I tried to execute this application so you can
switch over to areas as your startup project
hit your button and then look at that
unhandled exception we just went through
except on handling. Now I'm getting an
unhandled exception. And this exception type is System.out index out
of range exception. I need to see in
that the index was outside the bones of the array. So this is basically saying to me that you
have five species. You asked me for five species. Why are you trying to put in a sixth value in something
that only has space for five, that index out of bones array. Basically seeing that
you're trying to access a space that does not exist inside
the bones of the array. All right, so that
is why it's very important to appreciate that the numbers go from
0 to n minus one. If n is the size of the array, that is very much illegal. Know another way that
I could've assigned these values would be
in one line like this. If I said grids would
be equal to a new int. And then provided curly braces and then proceeded to
put in these values. So one mod 2538, **** 4554. And that will be a nice one
liner to assign values. So I wouldn't need six lines. I could have done it all in
one line because of course, just have to initialize
the array here. Let's just comment this all
just pretend I could have initialized to have
that number of species. No. Let us say that we don't know the grades of fronts
were writing an application, which is probably going to be
the situation where writing an application to allow users to provide us
with these grids. So I mean, typically
you wouldn't want as an IT person to write a program and then
until you use those all, come and tell me
what degrees are so I can enter them for you. You don't want to give
them the power to be able to enter the
grids themselves, someone to come in. Then we're going
to look at whole. We can write a program
that allows the user to go through and insert
each one of these. So we're going to use loops
in a more practical manner. No, Not just an example manner. So we actually did something
like that in once again, our string
manipulation exercise, where we had the IRI and you wanted to loop
through our array. Remember that? Wanted to
loop through the array. We can do something
very similar to that. So I'm going to see
for top tub int I, remember I is just my
counter is equal to 0. So here's a very
important point again. Why we start the cone to 0, because our array is
going to start at 0. As we consider relevance. The next. Is that you can always
see to whatever number. But then this by hard-coding
the number like this, you don't know if
tomorrow this is going to need 15 or 55. So when you hard code the number that you
have to remember that every time you might end up changing the
size of the array, you have to change the for-loop, which you might forget. We're all humans. So that
is why we would probably want to use the
array dot length, whatever length
that ends up being. We'll just get that length
during the runtime. And we know that we're going to N minus one of that length. And then we have
our incrementer. Know after we have
written all of that code, we're going to prompt the
user to enter the grades. So I went to say
console dot readline. Each time the loop goes around, I want to prompt or
users enter greed. And I'm just going
to do console dot write so it doesn't,
didn't want line. And then I can say grids with the subscript of
whatever value of I is. Alright, so let that
soak in a little. Remember that initially
I could say give me this exact address and I
can put in the number. All right. No, I want it to be
a bit more dynamic. I don't want to hard code it. I want it to be dynamic. So I already know that
loop is starting from 0 and it's going to add
minus one of whatever. However big this Ari's. So I can rely on as a, as a counter to go from 0
till the end of the array, one-by-one, just going to go 0, then one, then two, then three, then four. We saw that with
the for-loop works. That's the counter in
the for-loop works. Here. I'm just going to say, give me grids to the
subscript of I. Right? In that I would like to store, I don't want to say
convert to int 32 and get the value from
the user console. Console dot read line. I'm seeing each time you go
through for each subscript, go and get me the value
from the user's input, convert it into an int and
storage instead of my array. It seems simple and straightforward
enough, doesn't it? Let us see how that would work. So when I execute and
jump over to my console, you see here it's pausing. It's just waiting for
me to enter grid. So this would be
entering the grid into grades subscript 0, since it's the first run. And in the first grade
I had was one look at what happens when
I press Enter looped. It went to the tool, went to it incremented
the counter. I know it's at the
second subscript space waiting on that grid value. So this will be 2538 and
then 45 and then 54. And then look there, it asked
for was 001234 species. The IRI, if I
increase this still 150 up one to one less
than the iris space. Nice and simple. Know that we have
collected the values. Let us look at hold, present them back to the user. So I'm guessing, I'm
hoping that you realize by now that the easiest way to traverse an array is
to use a for loop. So I'm going to put some more printing
around these exercises. So enter all grids. That's just the
heating to see where entering all the grids. And then this one is going
to see the grades you have entered are the
grades you have entered, are you have entered. Then we're going to
print them out below. Once again, moving
forward FOR loop, just right for top tablet
it generated that stub. We're going to go for
the length of the array. So array name dot length. And then we're going to
see console dots, right? We're going to just
simply print grids. The subscript of I. One is going to be
filling it on one is just going to
be regurgitating it to the screen.
This is a rat lands. We will see them one-by-one. So let me test that
one old with you. We see Inter grid. I'll just put in some
random grid values. Then after entering
them it pulls. The United States agrees
you entered our 1020 to send the same order
that I entered them. This is the same order. They're printed out because
it's a structured collection. It knows what was first, what was sick on,
what was third. If I wanted to print
a specific one. I could easily just say
console dot write line, and C grades with
a subscript of C. Three. That will give me the fourth
elements no matter what, it will always be the fourth element
because that's how I can access it easily if I run
this application again, wouldn't prints all degrades. Yes, you will print the
first grid to enter, then it would print the
fourth-grade hit Enter, then the second grader Enter
then still the fourth grade. Because instead of being
dynamic with the address, I'm always looking at
address number three. All right, so that's
really how arrays work. So when we move over to a
variable size arrays, No, this is when we're talking about a situation
where you may not necessarily know how many
grids are to be entered. So I can actually define an
on-site sized artery as well. Let's start off with
giving it a default value. So if I said int grids, Let's say if I know agrees just so we can change the name. So int final grids
in square bracket, final grids or you know what? Let me try a different data type so we have some coverage. Let's try a string this time. So string student names
is equal to know. Remember, if I say new string array of five
and locking it into a size. All right? The thing with
these is that they always have to have a size. They're not dynamic. You have other collection
types that are dynamic. Button is not that dynamic. You always have to
tell it the size. So even though I
said variable size, it's not really a variable
size in the true sense. Instead, what would
happen here is that I would have to
see new string RE, not give you that defined size. You'll see that
I'm getting there. The line represents
most of us size or an array initializer. So I can use that initialization method
that I showed here. Now what happens is that
it will take its size based on what values are the number of
values being put in. So I can see a test,
student one, etc. So at this point, student names is an array
that only has a size 4123. So there might be times
when you're initializing an array and you might
be looking at the list, but you don't know
the number to see. Okay, I have five here. It might be 20, maybe
21, it might be 70. To write some number that you wouldn't be able to compute
just by looking at the list. Then this is where that kind of declaration would
actually come in handy. Then what would
happen is that you just go ahead and list
them out like this. And then when the program executes, it wouldn't know that. Okay, I have three
are 72 spaces here. I need 72 spaces in memory for my already called
student names. No, as far as interacting
with this RE goes, it's really the same principle. It will be the same
printing all over the seam. Interacting on inserting values. Really and truly, I'm
not going to spear the repetition of
going through those. You already have
these code examples. You can, here's an exercise for you to look at how
you would actually print the values at the values to this RA and how
you would print them out. All right, so I hope
you took the challenge. If not, then here it is. If you wanted to try
and she'd pause, don't look at what I'm about to explain and try it yourself. But pretty much I would have just replicate
that to see him for loop. So here I said enter all
that should be names. Literally I copied and
pasted enter all names. And then for each, sorry, for int I is equal to 0. I is less than student
names dot length. And then we go in
enter name and then student names with a
subscript I is equal to, because this is a string, I don't need to
convert anything. So I can just say
console dot, read, line. The same way when I'm printing, I just have the same kind of
for-loop and I'm just doing a console read line on student
names with a subscript I. So that's really it for arrays. When we come back, we're
going to be looking at lists, which is a dynamic list or collection type
here in C-Sharp.
21. Lists: All right guys. So we're coming
off the heels off discussing areas and I just learned to do a
quick recap so we can look back at what they are. My personal definition of
an array is a contiguous, homogeneous collection of data. Contiguous meaning that they
are, they're side-by-side. So you have RESP S1
are just like, Well, I'm writing these
pipes beside each other in memory there. All right, beside each
other, pretty much. Just going down the
line like this. So your data is stored in a chain like that,
that's contiguous. And then homogeneous,
meaning that you can only have one data types aqua icon to have an array storing both strings
and integers. So it's either going
to be in or it's going to be strings, etc. That is, what's an array is
not one of the limitations of an array is that it always
has to know its size. So it's hard to be dynamic in an institution where you
don't know the size upfront. Because you're either
going to define it with a space that is big enough to possibly
cover every scenario. But then you're going
to be reserving 255 species in memory when
you might only use ten. That's not very efficient. That's whole computers get war, no time crash under
heavy load from programs because of
decisions that those, the alternative would be that you don't know the
size of front, but based on the data
you're providing, you can infer that is still not very efficient because you have to have everything entered from the get-go to
then start using it. What we want to
do now is look at dynamic collection
type called a list, and that is available
to us in C-Sharp. So I'm going to jump over, created this new projects, new console up and I'm calling
this list collections. Of course we're using dotnet six and we create
this new console app. When we talk about lists, the syntax is going to look
something like this one. We define that we want to list. And then I say I wanted to
at least off what datatype, the datatype here, just like with the
array, can be anything. It can be at least of string, it can be a list
of integers, etc. So let's do strings. And I'm going to
call this one names. Then I went to initialize it
to a new list of strings. You'll start seeing
that we're dealing with far more complex types
than just intense on, because you're starting to
see that we have to be seeing equal new design that when we go over to the
object oriented exercises, you'll start to
appreciate why some off the variable declarations
have to look like this as opposed to when we just
see string is equal to that. We're getting a
bit more complex, but baby steps, but this
is all we declare a list. Like I said, this is once again contiguous
homogeneous collection, except it's not fixed
to us as initially. So instead of having one
variable called name, we're creating a list
of potential names. Let us look at how we
add values to our list. Instead of like areas where you had to use the
fixed point because I can invariably say names with the index name 0 should
have the value here. I can't do that. Lists allow you to do that because
really entry will at least is built on top of the general
construct that is an IRI. So it's like, here's a big word nor an
extension of an array. So they saw the
limitations with the area, so they made an extension of it. But at the base that
we can still do certain things that
would allow you to do. But then they also made other
functionalities for you. So you saw that Visual
Studio was suggesting to me that I see names
dot add name, or add whatever values I could add that value off my name. So these are two ways. Know the difference
between these two is here I have to know
the exact address that I'm adding the name tool, which I cannot know during runtime while the
application is running. I don't know if 50 names went
in already are only ten. I don't know. So by trying
to access an address, I might run into the problem of accessing one that does not yet exist because it's only as ten and I'm trying
to put it in space 50, or I will put it in space
50 and then end up with empty spaces between
10501149 will be empty. Once again, not efficient. So we would want to
rely on the program, the method, sorry, that has been provided
to us called dot AD. We see list name, which is names, names, dot add. And then we give it the value, will know that it
should put it in this space because this
space is the next in line. If one gets removed, it all automatically reorders. So you can actually remove and add as many things as you want. I can remove Trevor. From this I'm sorry about that. I can remove trouble or from
this just by seeing Dr. move wherever trivial or using the list of dates the first
star to stick on our third, it is going to go ahead
and remove it and then reorder everything so
that the address is 0 to length minus one
are always intact. So lists are far more
dynamic than arrays. If you try to remove or
MTO DSPS in an array, we're doing runtime,
it just won't work. Okay? All right, let us look at how we would add values
more dynamic bodies. So I'm going to write
a little console. And I'm just going
to say enter names. All right? And then here I'm going to
use more like a while loop. Why am I using a while-loop? Because I don't know the
number of items that I want. So I can't go by our counter. If I wrote this
program for a school, either programmer
can't assume that the school only
has 100 students. They might have ten and
they might have a thousand. So I can't be using
a for-loop to say, go through and do this for
that many times because the length here is going to be determined by the number
of things that goes in. A while loop would be
better because then I can a condition through which
they can exit this operation. So remember that the while loop or even the do-while loop, those are condition-controlled
loops, right? So I can see while name, let's say name, so
we have string name. You can redefine it already. Really curious if
you had erased it. And let's initialize
the string dot empty instead of the empty
string manually. And then I went to
say while name is not equal to the value negative one. All right, so I'm going to
show you now how we write programs using condition
controlled loops. You always have
that exit condition and you can write this
in a number of ways. You could write name is
not equal to negative one. You got to also write
this name dot equals. There's an equals film Shaun on a string they can use when you want to compare
it to another string. All right, I don't
think I mentioned that with the string
comparison earlier. So you could say name equals that what you
wanted when it's not. So you can see, not all right. You can see is
equivalent to false. Oh, there's so many
ways to write code, once again, half to explore. All right, so that's
one way to write it. I'm just gonna show you
all the ways to write it. Then you can also just see not this explanation saying
not just that not equal. I'm just saying while
this evaluates to false, while it is not equal to
negative one, then continue. Let me leave those to code some blues there for
your reference later on. I'm going to proceed
with this one. So while name dot equals
that is not true. That's what that
exclamation sign means. Then we wanted to
continue looping. I went to ask the
user for the name. I'm going to see console
dot write enter name. And then I'm going to say name is equal to
console dot read line. So I load the user
to enter the name. And then I'm going to see name, dots, Sorry, names that
isn't able to list dot add. There we go. And we're adding the name
that has been entered, which of course is a string. So I can just add that
name as it is a string. This is saying know that
it's a possible null. So that means that if the
person didn't enter anything, do you really want to enter
an empty string or something into the area of names are
the list of names rather. So here's where I'm going
to put an if statement. So know you see that this is getting more complex
as our program, as we expand our horizons, we're starting to
look at how we put an if statement inside of a while loop and how we're
interacting with that list. This is what we call
nesting when you have one constant control
structure inside of another if statement instead
of awhile is an nesting. All right, I can see if a
string is null or empty name. So let's do this. If it is not null or empty, and look at this node and it is not equal to negative one. Nice and complex this if
statement has gotten, then we can add it. Let's go from the top. While our name is not
equal to negative one, which it won't be because we initialized it to
an empty string. So it's empty. And this is, this pre-check is going to pass because
it will not be negative one before it hits here based on the way we
wrote or program. Then I'm going to
say enter the name, accept the input from the user. Then only if the input from
the user is not null or empty and the input from the user is not
equal to negative one. That time we should add it. That means if it is empty, then it will not
execute this if block, it will fail on that condition and then it will
come back and check. But then this is still
not negative one, so it will come back
into the loop run again. If I enter negative one, then it will fail this check because now
it's negative one, so it will still not enter, come back, and
then it will exit. All right, so this
is how we would use a while loop to ask the user for names until
they are finished. Because when I'm finished, I can put in negative one. So this is what we call
the exit criteria. And it's usually an
obscure value relative to the context that the user would never put in by default, I mean, who puts in
a bunch of names, inputs in negative one. So let us try that one out. So this is list collections. Let's start. Alright, so here we're
being prompted enter name. So I'm going to enter trope
or I went to enter test. I went to enter Matthew. I went to enter John. All of those values. Notice it just keeps on going. I'm just going to keep on
typing and pressing Enter. It doesn't nor cure. It's just accepting the values. And then when I
personally negative one, that is when it exits,
That's the soul. No, you would actually
be able to enter values onto the user is satisfied. If you want, you can put on
another little prompt here to know when the value was entered versus when it was not entered. Because if it is
entered and you could see name was inserted. So I can't just
interpolate this string. Then just see that name value
was inserted, successfully. Added, successful. Let me see, I added. All right. So then the user would
get some feedback. Yes, you put in that name, it was added successfully. Or rather you'd put that
after it was actually added. It was added successfully. Then that is how we fill
our list with values. Now, how do we print our
values from this list? So print values in list. Well, we have once
again a few options. We could reuse the while. I think it takes a lot of
coordination to use the while. So I'm going to skip
the while example, but we already have a way we could do it in the
form of the for-loop. It. So remember that lists are built on top of the
construct of an RA. I could actually just
write a for-loop that goes from 0 to the
length of our list. So the list is called names and I can see names,
thoughts, cones. This one doesn't have a length, but it has cones. Cones gets the
number of elements. If you see the right up
there from Visual Studio, gets the number of elements
contained in the list. Alright, so like I said, sometimes what you want is not necessarily presented to
or you might overlook it. Sometimes I end up
scrolling onto some, I eliminate some property or some method looks
like what I want. Visual Studio in more
recent times has starred the most used methods
and properties, and you'll see them at the top. Even though it's
alphabetically ordered, you would see certain
ones at the top. So like cones is at the top
because I figure, well, people usually want to know the number of
things in our list. Dot colon t, so you get it. It's there for you with, I raise its dot
length with lists. It's thought cones
seem that effect. We're going from 0 to
the cone to minus one. Then I could easily just say
console dot, write, print, buck each name called
the list bytes name, and then give it
the subscript of I, will actually iterate
through that list for as many items as we're entered. Because remember,
open didn't know, we still don't know how
many items to expect. That's the firstly
that you can do it. So this would be printing. Let me just do console
dot write, line printing. Let me get my spilling rights. Printing names via FOR loop. That's what this first one does. Now the reason I'm specifying
for-loop is you can also use another type of
loop and one that is specially designed for
this kind of situation, which actually got
an honorable mention when we were looking at
repetition statements, but I didn't get into it
because the time wasn't right. And that is the for each loop. Now we know how to, or nowhere going to look at
how the for-each loop, right? So I'm going to say console. I'll just duplicate
this slide here. And this would be
a foreach loop. Now the for each loop, you start typing for
each and then you press Tab twice and it generates
that stub for you. So for each, and you'll
see it's seeing var. We look at what var is later on. What I'm going to be strongly typed here and say
string because it is a string collection. String. Then we see item. What is the collection? We have to call the
collection by name, the collection here he's over
at least, which is names. It's basically saying for each
entry in this collection, for each item in the
collection of names, it could be more
specific density for each name in the
collection of names, but I can't do that because I already have a
variable called name, so it would cause
confusion. That's fine. I'll just leave it as item. My point is that this
is going to go through whatever number of items
are in that collection, whether it's 110105 thousand, will go through each one. Store it here for the
runtime of the loop. And then each time it runs, it gets the new one. You can do whatever you want
to do with the new one. Like I said, we
wanted to print them. I'm just going to say
console dot, write line. And all I'm going to
do is print item. All right, That's all
that it's really doing. That's a for-each loop. The for each loop is perfect. I don't have to worry
about the cones. I don't have to worry
about an exit criterion. All I have to do is say for each some variable in
this collection, do whatever you need to do. Nice and simple. All right, so let us take a look at
what this will yield. I went to enter a
few names here, so we see node seen *********
added successfully. Bought I did a console dot write instead of R
console right line. So that's why it's
printing all that out, but I think we'd
know that by now. So I'm not going to
stress that one. I'm just entering some names. After entering all
of those names, when I press negative one, you'll see that it went
through the for-loop, printed. And then it went through
the for-each loop and print it right up. To me. The foreach loop is just
much easier to formulate because it takes a little
less coordination. But there are
situations where you wouldn't need a
for-loop with the list, but context determines
your execution most of the times it's good to know the tools that you have in hand. But the which one to
use when is usually determined by your
needs in the moment. That's it from me on
the topic of lists. I want you to experiment, try lists with other data types, like arrays with
other datatypes, and write other programs
to accept values. Try to write a program that
accepts multiple arrays. Or at least you
could have names, student names, and the grids. And then you write a
loop to enter both, and then you print
them all back, get creative and explore.
22. Introduction to Classes and Objects: Hey guys, In this lesson
we're going to start discussing object
oriented programming. No, I've said before
that C-sharp is an object oriented
programming languages. And there are a few
of them out there. Java is a very popular one. Python, Ruby, C plus, plus. All of those are also object oriented in
programming languages. So the difference between object oriented programming
languages and other programming languages
will be the object oriented programming
languages allow us to create our own datatypes. And we do that through the definition of what
we'll call a class. To get started, let's
create a new project and we're calling this
one classes and objects. So go ahead and hit Pause,
created the project. And we can start looking
at what the class is. We have interacted
with classes upon to know Console is a class. And I would have
pointed out that based on the color coding, you would see that there
are classes, right? So Console is a class. When we looked at
least collections. We also looked at a class, and that class is called list. And I would have shown
you that the declaration for this kind of object was
slightly different from one. We add our irregular datatype. We would just say data
type equals whatever. List is actually a very specific datatype
that was created. That's not one of the, what we call the
primitive data types. Primitive datatypes would be what we would have used
when we started off. You don't int
string, char, bool. All of those are primitive. Notice they're all
the same color. Preventive because they're as
basic as datatypes can get. But they don't necessarily
provide all of what we want. So in order to capture a certain beats off
inflammation like when we had to do the program getting
all of these bits of data, we had to have 12345 variables to collect data on one person. We got to have full name age. But what if I wanted one
representation of a person? That is where creating your
own datatype comes in. So object or into programming has to do with creating
your own data types. You're a datatype since
you're the one creating it, you can determine
what properties are, what methods can be involved. We do that through
the definition of what we'll call a class. A class is a blueprint of what
the object is going to be. The object is the
manifestation of the class. So think about it
like with a host. When you are going to
be building a house, you have to put it on paper, you have to outline,
we're watching goal, how big each room should be if everything is done in a
visual representation, a 2D representation,
that is the blueprint. However, when you
build the host, that is the manifestation
of set blueprint. And then if you
have multiple holes is built off that blueprint, then you have multiple
manifestations. So if we were to do that in OOP, the blueprints drawing would be our class, That's the outline. And then each host is
an object of the class. To define a class, we seem to use the
keyword class, then we give it a name. So if I'm using person that
isn't name of the class, then I open and close. My brief says, my curly braces know in defining the class we have
to give it properties. So you see Visual Studios
here making a suggestion. I'm going to do it
manually so I can explain what each part means. Properties refer to the, let's say the
knowns, the object. So in the case of a person, what is unknown on data
person has ij is unknown, so that is a property
of a person. The name is a property of
the person right there. Address every bit of
information on a boat, a person in this context, or a boat or whatever
the class is, every bit of information
is indeed up property. To get the property firstly, give it an access modifier. So you have different
ones. You have public. That means 170
cannot type person, I can access their
whatever is public. Then I have to give
it a datatype. So public string, and then something that is
publicly accessible level most persons is their name. So in reality, name is
a very good suggestion. I know the thing that's. Probably be publicly accessible about a person
would be there each because each would
be more like an int. And then I would say age. And notice that it
has this get and sit open curly brace
and then get semicolon, and then sit, get semicolon, then set semicolon and
then closed curly brace. These are what do
you call accessors. So these allow you to get the value involved
here are stored here. And this one allows you to
sit the value stored here. So by seeing get to unset, it means that once
I'm interacting with a person that I have leverage to get what their name is or sit
what their name is, gets what their name is, sit what their name is. Alright, so class,
give it a name. I noticed just by
seeing class here that takes tier that
I have written. Null has the same color
as console and list and all the others because
it's only this is me creating my datatype. This complex data-type is making use of primitive
datatypes inside. A person has name, has age, might have
other properties. Before I move forward, what I'm going to do is split the salts. So generally speaking, you
don't want to just store name, you wanted to store firstname and you want to store lastName. Notice the naming
convention also, I'm not using common letters, isn't if I use the common
letter, it will work. There will be no problem. But then if I go here and
control dot Visual Studio suggesting that I use a capital letter so far
properties in a class, it is suggested that you
use the capital letter, that's just a standard. It's not that only works. Bits are one. We are at. The elbow is just
a standard and I'm always pointing old standards
and best practices to you. Public is the accessor
that's allows me to access anything
about this person, the first name, the
last name, and the age. Now, another thing that
I wanted to bring to the fore before we even
started looking at objects. I'm always showing
you best practices. You don't necessarily
want to have your class defined
inside of another class. So each class
should really be in its own file for contexts. And I'm just going to
collapse everything here on the Solution Explorer. You can do that easily by using
this button collapse all, it just collapses everything. And then I can target this
file open this file that is here specifically by clicking this sync with active document. Some more keyboard shortcuts, Visual Studio Tips and Tricks
coming out to you right. Here is our program.cs
file that work are in the working and
this is a program.cs file. It means that this
is a class file dedicated to the program. I want my own class, my own datatype defined. I don't necessarily want that defined inside of the program. There is another principle
that we adhere to call single, single
responsibility principle. This principle basically
means that let each unit, as far as practical, have only one responsibility. Our program.cs file
should not have the responsibility of
defining our class. I would want to put this in its own class and
easy way to do that, thanks to Visual Studio, would be two, while
you could easily just right-click Add New Item. Then you could say you want a new class file and he
could give it the name. So I went to see like person. And then it will generate this entire code block
showing you the namespace. So remember the namespace
is the name of the folder. All right, So our project
discusses an object, so that's our namespace. And then it generates
that method stub with another access modifier called internal public means that
anybody can access it, but internal means
that only other files instead of the same
project can access it. Alright? So I could easily do
this and then this is where I have my
class definition. No, I have this class defined in a completely
different files. So if I wanted to
know where my person crosses, I need to find it. If my program had a
thousand lines of code, I wouldn't have to comb
through all of them to find the person
class because I can just go and find Person.new ECS. And this file has a
single responsibility of dealing with anything
related to the person class. All right. No, i keyboard
shortcut to do that would have been to
do control dot. And then it would
actually ask you, do you want to move
type to person dot CSS? So it would actually
suggest that hey, I can just move all the
code to a new file for you. If I didn't create
this new class, let me just erase it
just to show you. I can just Control dots and then move type
to person dot cs. Then it would actually
just do a cut and paste for me and create
this new file formula. And there we go. I'm just showing you
the different ways that you can do that. Once again, public. Who have public that
can be on the class. You have internal. That means it's not
public to the world, but it's public to anything
inside of the same project. Then you'll have private, which means that you need various special
permission to access it. And even Visual Studio utility, you can't have something
that's private here and everything
else is public. So you don't generally
see private classes, you usually will see
public and internal. So I went to leave
this one as public. And then all of these
properties are also public. So now that I have a
class defined, code-wise, interact with it in
my main program. Let us say that I wanted
to write a program that allows me to fill in the
information for a person. I remember person could
easily just represent whatever object and properties
of that object you want. If it was a box, it could
be length, width, height. If person who was
to be a student, it could be first name, last name, age, and greed. Remember the whole head
the trouble of doing one array for grades on
one array for names. We could do everything inside of one big data type called
personal or whatever it is. Once again, it's
just a blueprint. To get the manifestation
of set blueprint, we would have to do what we
call an object declaration. So now I'm going to start
seeing object instead of variable plus variable tends to refer to the manifestation
of primitive datatype. To get more complicated datatype would say the
name of the datatype, which is person, called
the name of the object, which I can say this person. Notice I can name one with the capital P and one
with a common pea. And that's really because
I think by nowhere realize it's very strongly
typed and it's case sensitive, so capita appease
entirely different from common P. Then I'm going to say is equal to a
new instance of the class. If that looks
familiar, then good. It means you are paying
attention when we did our list, list, whatever names is equal to a new list and open
and close brace. Whenever you're dealing with a class that needs
to be declared, this is what it's
going to look like. Person. And then the name of
the object is equal to a new instance
of that object. Guess what? It can also have multiple instances
of that object. So you can have person
one, person two. Because the reality
is that you have multiple persons in the world. What we all have in common, however, the properties defined, every person has a
FirstName, LastName, and each, if not,
I stand corrected. This is the general
blueprint and then these are all manifestations. Offset blueprint. Know a shorthand way to
write this in C-Sharp, Eight upwards or 900 words
would be to just say new. So you don't have to write person who is equal
to new person again, you can just say
person is equal to person equals and then new. That's a nice sharp and we let us work with
one person for null, let us write an application that is going to accept the
information about this person. And then it's going to
print it back to the screen that this is dispersed. Information is I can see console dot write line,
enter, firstName. So these are things
that we have done. We know how to prompt
for the FirstName. What we didn't know
before is hold to store everything inside of one object so I
can see a person. Notice person is the object
that we have defined. I can see a person dots. And when I say data, I have access to a bunch
of methods and properties. So how do I know methods
different from properties? First hint is that in the intelligence the methods
are going to have that cube. Then the properties are
going to have that range. So our property
represents one of those variables that's I
would have defined here. Method would represent what
we looked at methods already. So we can actually do methods, we'll do that in a few. So just by having lactose, there are some default
methods that we would get like two strings. So I did say before that
every single object in C-Sharp can do toString what
is not always practical. It has the equals,
does get type. It has a bunch of
built-in methods. I'm going to start off
with the easy ones, which are the properties that I know I need to interact with. So person dots first and the meaning when you
create this entire object, which is a collection of
these three variables, I wanted to set the first name to have the value coming
in from the console, console dot read line. It, that's number one. Then I want to enter last name will be
the same principle. So person dot last name is equal to get the
value from the console. Then I wanted to get the edge. So enter. Then we say Person.new edge, and then age is an integer. So if you said we
need to convert, then you are absolutely correct. So we need to see
converts dots to int 32. Let me get my spelling
rights to int 32. And then we wrap that
up in parentheses. And there we go. No, I'm filling in the information
for this person. This is courtesy of the
get sorry, off the set. The set allows me to do this. If I didn't see that
I can sit the edge, I can only get the age. Then I'm going to
get an error when I tried to do this
assignment because now it's saying that there
is no, see it's read-only. I cannot be setting a
value on that property. All right, so that's
why I get ten sets are important if contextuality
are important. So you may not necessarily
want somebody to sit the age. Of course, once again, I'm just giving you the concept. The implementation is
relative to your situation. Firstname, lastName, age. And we can enter each
one directly into the properties as provided by our object of type in-person. Now let us look at
another property like maybe the salary. Most people keep their
salaries private. That's not something
that you'd be able to google me and find very easily. Within that context,
you wouldn't necessarily see public salary. You would see private, the datatype double and salary. And generally speaking, when
you are doing a private, what we'll call this a field. So these would be
properties when public. Generally, when they're private, you call them fields, just some jargon so you can
appreciate the difference. So generally speaking,
with the fields, you would actually
use an underscore here to denote that this
is supposed to be private. So if you were just
looking through somebody's code and you saw to underscore the
general guideline would be that are
the general theme behind that underscore preceding the name would be that it is a
field, it's private. Whereas when you use
a capital letter, it means it's public. So that's just that little thing that you can look out for. Once again, another
good practice to have. When I tried to interact
with anything that is private, like over here, if I said person dot salary, notice it's not
even coming up in the intelligence because C-sharp is saying
this is private, so I'm not even supposed
to show you that. If I tried to access it, if I tried to interrupt with it, it's going to say, boy, it is inaccessible due
to his protection level. I cannot help you. It is it is private. You can't just interact
with it like that. So that is another
thing that you can look out for
when it comes to private and public settings on your fields slash properties. When he comes to
our private fields, that's when we start talking on both methods that would be
needed to interact with them. A method would represent
more like a verb. So I did say that properties
will be like knowns. These are things that
are on the object. What didn't, what
can the object do? A person can walk, a person can run, stuff like that,
a car can drive. Those are things you can do. Those are methods. Anytime you're thinking about
something that is doable, that would probably
need to be defined as a method as opposed
to our property. So let us say for instance, we wanted to be able
to get unset salary. Those are methods
you're getting is verb, setting is a verb. We have it as a private. We would need methods to
help us to interact with it. What I would do here
is define a method. We already looked at
how we define methods. What do we probably
didn't do was put on access modifiers on
the method, right? So if I'm going to
set the salary, that means I don't need
to return anything. I would want something
like a void method. I don't want to
call it set Celery. Celery. Your job is to give the
private field a value. Let us say I'm going
to set it to 100. Once this method is called, salary will get
the value of 100. How do I retrieve this value? Well, I can do another one, whereas the public get salary. And this time I'm
going to make it that value returning because I want to retrieve
what the salary is, then all I'll have to
do is return salary. All right. Look at it, no private field. And then we have methods to interact with the private field. We have one to set its value. I don't need to do anything. I don't need to return anything
after setting the value, so I'm just going to do avoid. However, don't here, I'm
going to be sitting it. Sorry. I'm going to be retrieving it. So if I'm retrieving it, that means it needs
to return it. And if it returns it to the
camp before the test to be the datatype off
whatever I am returning, which is a double. Alright, just showing you
how everything is connected. So because these are
two public methods, I can easily say person dot, and then look at this
null set salary's listed. It gets salaries
also listed there. They are one public,
so they are visible. And two, they are methods. Notice the cube. Alright, I did describe that the cube represents
that it's a myth. Let's see if I call
it sits salary here. Then I wanted to print what
that salary is afterwards. I'm just going to see salary is. And then I'll just do
interpolation and then look at the second person,
dot get salary. For us not get salary is
going to retrieve what the salary is and
that's going to be printed in the
console dot write line. All right, so what we
can do is test the salt. Before I test it told
though I'm going to create some other lines. So I went to see a first name, last name, and age is. And what we will be doing
is printing the properties. So person dot first
name, person, thought, last name,
person thought age. And then these are properties. And then this is a method, but
all of them are related to the same person because we're entering the information
for one person. So I'm going to change my startup project that
causes an objects. And then we're going to take
this one for a quick spin. When the program executes
into the first name, I'll use my own name, last name, age, let's
say I was 45 years old. So nobody's going to print back everything
about that person. First name, last name is Susie, everything is being
stored on the salary was set to a 100 courtesy
of our method. But that's not the
reality because we are hard coding
the value, right? So my salary is
not your centers. So when they're entering you
as a person with your name, they need to enter your salary. What I can do, it simply is one creates a local variable
that I'm going to call salary here in
the main program. Then I'm going to accept
input from the console. When I get this low cost salary, that is the salary
that I want to use the SETT, the
person's salary. We looked at using
parameters in our methods. I'm going to modify
the set salary method. And already there was press F12. So when he'd wanted
to jump to a method, you want to navigate
to this method, you just click it, press F 12 on the keyboard and it jumps over to the definition. So that's another keyboard
shortcut for you. So from here, I can
tell set salary to accept a parameter
called salary. And we'll use that
value that is coming in to sit the private field. All right, so that means no, when I'm calling set salary, it is know as an
arrow because it's expecting a value so I can give it the salary which
was accepted as user input. So user inputs, salary, we call a person dot set salary, ascend over that value
that just came in. Then inside of that method where using that value to set
the private fields. So whenever we asked
for the salary, that is when we get back whatever was accepted
from the user. That is full, you would have to interact with private fields. Another thing that
I'm going to show you is maybe get the full name. Goes retina where getting
FirstName, last thing. What if I didn't want to
print firstName, lastName? What if I wanted to
see your full name is, your age is, and your
salary is, right. So I can easily put
in another method. It's up to me so I
can put in a value returning method of type
string that says get full name, get full name. This doesn't need any
parameters because it will be interacting with properties that are already inside the class. So I can actually just
tell it to return a concatenated version
that we'll see. I'm going to
interpolate a string showing firstName and lastName. Alright, so first
name and last name, and that's easier, fullname, and that is what I
want you to return. So anytime somebody calls get full name, it should return. That. A block of string with the first name and
last name included. Know instead of saying
firstName and lastName is, I can come in these lines. Then I will just
create one to see full name is get full name. Look at that person
not get foo name. Age is that I am
going to give you a challenge on this
point for age, what if you wanted to know what the year to person was born
instead of just the each. You can take the age. And then you can create a
method that says get to year of birth it and let it return the year of birth and
print it back to the screen. When we talk about
classes, objects, and encapsulation, I did see, I would explain what
encapsulation is. Aggregation. One, the
class is the blueprint. From this perspective,
anybody looking at this, it doesn't know
much about person. They only see person when they're interacting with
the object of type person. They can interact
with certain things. They don't necessarily
understand how it works. That's what we
call encapsulation because the inner workings of the salaries actually set,
it's actually retrieved. They don't know that others
seeing is get salary. Do they made simple
I wanted to salary. I just call the sky. Gets salary. If I
wanted to full name, I'll just call get full name. They don't know the hardware that's actually
happening behind here. However hard or
assume that it is, the person interacting with
the object does not know. In the blueprint is where you define all the
functionality, but the actual usage of
it is supposed to be as simple as possible for
the person using it. And then further to that, no one would talk
about aggregation. It means that inside of
this one complex class, we've aggregated a number of properties and operations
that they can do. So from this perspective, in a few lines of code, they can accomplish so
much without realizing how many lines of
code would've gone in our aggregated
to make it up in. Let us run through another
test with this so we can see if we get by the full
name and the salary again. I went to enter for
VOR William's age 58. And then when I press Enter, I think it's asking
for the subtler I didn't put in the
prone for the salary. So let me put you in 2 thousand. And then Node seeing
your full name is full name as given to
us by that string. And here I'm seeing two spaces. Alright, so that's something
we could look into. I think I put a space
after one of them. That's why we're
getting the full name. We're getting by the age
and we're getting buck. That's salary value that
we took via inputs. So that's a quick introduction to object oriented programming. Once again, practice
makes permanent. So go ahead and
get some practice by trying to figure
out how you would calculate the person's year of birth based on the age
that was provided.
23. Method Overloading: Hey guys, welcome back. We're continuing on our journey with object oriented
programming. And I just wanted to mention a quick concept called
method overloading. What would've noticed up until now in our
interactions with cost is even before we
started creating our own, was the fact that we
could actually write or access different variations
of the same method. This look at the console class. Console. Dot write line. No dot write line
has 17 overloads. So an overload means that you have different
ways to college. You could just call it empty,
meaning null parameters. You would call it with
the bull parameter, with a char parameter. And I think I'd
scroll through and showing you all of
the possibilities. This is actually a
method overloading happening right
here because it's the same method
with the same name would see him return type. It's going to operate slightly differently based on the value that is being passed in here, whether it's a string or
an entire chart, etc. Once again, encapsulation. You don't know, right
line is working. You'll only know its inner class and it's a method that
you call and you give it a value and you expect that is going to print out
something to the screen. You don't know what to do
to convert the Indians or the string or the W here into the string are what to test
it with irregular stream. And all of those things, right? You don't know. At the end of the method overloading can
be very important when you want to give your users
opsins to carry out the same kind of operation or different options with
the same method name. Someone to keep this one simple, but it's simply enough
concept that with one example you should be able
to catch on easily, right? So let's see, we're getting
the person's full name here. I wanted to overload
it to potentially gets their middle name as well. So I want an option
where I can put in the middle name and get the full name with the
middle name included. There's no property in
person for middle name. So I'm going to do this
from scratch, right? So let's say I have
a string variable, I'm going to call middle name. I'm going to initialize
it the string dot mt. I'm going to prompt. So earlier I did not add
the prompt for the salary. I'm just southern. Get my spending
right there we go. Enter the salary. Then I'm going to ask
for the middle name. So I went to say console
enter middle name. And then width for the middle name inputs from
our console, dot readline. Once that middle name is put in, if it is present, I would want to get the personal full name with
the middle name included. Otherwise, just give me
the regular full name. I'm going to use an if statement and I went to make
a quick decision. You've seen something like this. If string dot isn't
null or empty. So if there is no
middle name present, meaning the user did not put
in anything for this input. Then I will print the
regular full name. Else. I would like an option where
I can give you the full the middle name and that gets included in the full
name that's printed. Now, notice this one
takes no parameters. That's how we defined it. This one, I'm passing the parameter and it's
giving me an error. Why? Because it says no overload
for that method takes one argument saying that I see you're calling
this method, but I can't find a matching method definition that's supposed to take
any parameters at all. So it's up to me no, to go into the class and
make sure that it exists. So I can just go over to person, just click it, press F 12. And then I can create
another method that I can make a public String, give it the same
name, get full name. But this time it's going to take a string parameter
called middle name. See that? This one, no, I can make it return the same kind of string. But I'm going to include
the middle ear in the middle of that
string, right? So once again, that
user will never know that whether the difference was this complicated or that simple. They just know that they have an option to get the full name. And they have an option to get the full name with a
middle name included. Know that I have these two versions of
practically the same method. Say method because of the name. If I look back here, that error is gone. All right, so once I provide a middle name and
went to do a check to see which option
should I call them? Should I call on
Person.new setFullName here or should I call on the
one with the middle name? Middle name prisons. All right. Let us test that one quickly. Get the first name,
the last name, get the age, get the salary,
then the middle name. If I don't put anything there, notice it prints all my
firstName and lastName. Let's try that again. I'm going to run it again
and then see same thing. Age, salary. Then I do have a
unique middle name. It's a Greek, it
means reservoir, it means bread of life. Actually. I'm sorry. It's Greek, it means
bread of life. And when I presented
in the inputs, then I get that option for VOR, our toes Williams, right? If you want to make sure, let's do a little
debugging session. Someone who puts our
breakpoints on either one just to make sure
it's going to call the cart one when we expect. This is another way. If you were doing
it anywhere and getting the output
that you expected, feel free to use
your breakpoints. So forever Williams
or just kinda do shortened to get through this
quickly, no middle name. Press Enter and OTC called, get full name with all middle name because I
didn't provide a middle name, I just press enter
so it was not empty. And it's no calling this method
heating that break point. So at that point, firstName is true for
last name is Will. If I hover over the middle
name, There's nothing there. It won't even show me anything because there's
just nothing there. It was not sit. Right. So if I press F5 to
let it continue with the execution and press enter
in the console to F3 top. Then you will see It is No, I'm printing out as expected. And then just the same way. If I put in test user celery and
then a middle name, then it's going to hit this one where it's passing
over that middle name. That is a very simple example
of method overloading. Like I said, it's something
that we have been doing from the get-go with console.log wetland and some other methods. That's how you can implement
it within your own class.
24. Static Classes and Static Class Members: All right guys, So we're moving on and we're
going to be looking at another category
of class types. And this one is called static. Static gas is one that
cannot be instantiated. So when we talk
about instantiation, it means this when we create
an object of a class. Alright, let me, I should've
done this earlier, so let me just show you, give you a code or
reference to hold. It could also be done. All right, So what do you say
new or you say new person? And later on we'll be looking at another keyword
that we can use, the obscured a datatype. But let's focus
on static classes so we cannot instantiate a
new instance of the class. So we've instantiated
our own class. We have to instantiate list and we would've
had to instantiate to some extent the RAs when we did arrays or even the dead time so that those are all classes. Datetime is equal
to new datetime. We had to do that earlier. A static class would
be more like console. All right? So console actually gives us
well static methods rather. That means that we don't
have to see console creates an object is equal
to a new instance of console to then
access right line. Because there's no
way that I could say person with a capital P
B in the class type dot. That doesn't give me anything. It doesn't allow me
to access any of the properties without
instantiating it, which is creating the object or the manifestation
of the blueprint. And then through the blueprint, through the manifestation
on rather or the object, I can access the
properties and methods. So we're going to look at
creating a static class and how it could come in handy. Static class is usually
used as a utility class, so I'm going to create
a new class file. So I'm just going to directly call our classes and objects project go to Add New Item. And actually you can notice you have a short quotes
here that just says class, granted, it's almost
the same thing. He's going to bring up
the same dialogue window and just focus on class
for you automatically. Let us say, I call this
util class date util. Date utils. No day to till. It's going to give
you the new file, is going to give me all
of the using statements. And once again, this is a
traditional C-Sharp file. In dotnet six over we have access to minimum
files where we don't have the namespace and all of those curly braces and
all the using statements. Upfront. You can always clean these up by holding down Control
and pressing key than E. And it will remove anything that is not
needed from your files. So we don't need all of these using statements Control key. Sometimes you just wanted to file to show you what
you need to see, which is the whole philosophy of six is almost
over ****** with even these code blocks where
the namespace I can remove those curly braces and
just put a semicolon. C-sharp six will
automatically know that this is the namespace
that governs the entire file. So I don't need all
of those breezes. Know that we're
delving more into old PM showing you little things about hold the chord is laid out and how you
can interact with it. You could actually do
this in C-Sharp tin, granted, the way it was
before it worked perfect. I mean, Visual Studio
gave it to you so it must work that we also, all right, Let's
focus on our class, internal class, and
we're calling it D22. Know what if I wanted to calculate I wanted a method to calculate the debt based
on the year value. This utility class is
going to have a bunch of methods that just
do things for me. So public. Year of birth. And then this year of
birth class, sorry, method is supposed to return an int public into
year of birth. Let's take integer,
year or integer each. Let's start with that. Alright, so given the age, I want to return, what's your probability
year of birth is? In this method, I'm going
to do some calculation. I'm just going to
return the time. Date, time dot no, year minus h so easily that is. So datetime dot know gets
the current timestamp, get me the year. Off the core in
timestamp and then minus the age from
it and return that. That's an easy way to
know your year of birth. Nice projection at least, right? Let's try another one. What if I wanted an overload for that? That's allowed me to also
return a year of birth. So I'm still going to give it
the same name overloading. But this time I would want
to take your date of birth. Date of birth is the property, is the parameter that I want. Then what I can say is if okay, let me see what Visual
Studio has for me. If the date of birth that you gave me is equivalent to null, then return 0 or tilapia. Otherwise, return
the tough year, time, no, year minus date
of birth year. Oh my gosh. That's lovely, but
that's not really what I want. I want more. I wanted to just give you a buck the year because I
said year of birth. So if you give me
the time of birth, then I'll just return. What's your year
of birth is right? However, I'm just
showing you these are methods that the focal points is
not really the methods. I'm just making a
bunch of methods in this util class because
I'm just showing you that, generally speaking,
study classes are like util classes, right? So this one would determine your age based on
a date of birth. And then I will just revert
to the original suggestion. We're dead time no. Year minus the date of birth, year that you provided. Utility classes know on a normal circumstances to start interacting with these methods, utility methods, sorry,
interact with them. If I wanted to use them
instead of my program.cs, I would have to come in
here and say datetime D2 till creates a new
object of date utils. So notice now it's telling me
that it could not be found. This is because the namespace
was defined above the file and no other class in this particular project
has that namespace. The namespace basically is a declaration to the
files off the address. If that address is not included, then you use it. Simple solution, I'm just
going to remove the namespace. It's not absolutely necessary, at least not in
C-Sharp tenant on it seeks know that it's removed. I have access. Know if I needed to
calculate something out, I have to say object dot. Then I would be able to
access all of these methods. Accordingly. Year of birth versus the age versus what was
the other one year of birth was overloaded. Sorry. I just remember
it was overloaded, so know that it's overloaded. In intelligence here we're
going to C plus one overload. So you can give me the DateTime, date of birth, or you can just give me the
age of the person. Alright? No, static das removes the
need for this instantiation. So to meet this static, step number one
is you're writing the word static in the
access modifier section. If the class is static, then the entire everything is in the class must be
studying voted that way. The class is static, then the methods have to also
carry that access modifier, letting the whole
compiler thing, the whole program know
that they are static. Though, you can have static methods inside
of our regular class. Alright, let us see, know what the
interaction would be if I wanted to find out, okay, Notice sold since
making it static, this is not illegal. I cannot instantiate. Static type. Cannot declare a
variable of static type. I can't do that anymore. Instead, I'll have to
do is call this class. They do till then
I just say dots. I didn't, I cannot access any method that
is in that dress. Much local console works
where just say console.log. And then I access the method
I could easily print. Age is that. And then let's say
a year of birth. And then I just say D22 dots, Dots, year of birth. And then what am I passing in? I don't talk about
dates of birth null, but what I do have
this on my overloads. I do have an edge so I can
give you the age. Okay. So I can say Person.new age. There's a person's age needs to only what
their year of birth is. That's basically
it. Like I said, it's usually used for utilities. So any quick operations
are operations that you might find yourself
repeating a number of times. You can just create
a static class grade the method, those, that operation, sometimes it's as simple as I'm
joining strings. It could be trimming strings. It could be converting between datetime and
integers, stuff like that. It could be a math class where
you want to find the root, square root of stuff. You just create a
class for that. Alright. Let us test this out and
see how well it works. Alright, so I'll
fill the social war. Williams, 5070 years old into celery middle name.
We don't need that. And then you'll see
here fullname is that age is that year
of birth is 1965. So it took that age, did the calculation
as we defined, and return that value. That's basically it. So that's how you would use a static class in a very
practical situation. And throughout your journey with C-Sharp and object
oriented programming, you're going to encounter
a lot of static classes. So it's good to understand
how they're built, how they're used generally, and when not to use them also.
25. Inheritance with Classes and Interfaces: All right guys, welcome back. In this lesson we're going to be talking about inheritance. So we're still looking at
object oriented programming. Uncertain concepts are at
least key concepts to get you ready for writing
basic application. And for the second
part of this course, where everything is focused on object oriented programming. Where we went to
discuss inheritance by creating a brand new project, a new project, and console C-Sharp on where calling
this one inheritance. Inheritance. So inheritance has to do
with exactly what the word suggests to you as a human being when he
talk about inheritance, it means receiving something
from another source. Some persons are extremely rich because of
their inheritance, which is something that they received from their predecessor. Inheritance has to do with shearing properties
with somebody else, shearing my stuff
with somebody else. In terms of object oriented
programming in this concept, it means that we don't
necessarily have to repeat certain things across types that sheer
common attributes. So enough talking, let me show you some
practical examples. Someone to create new
class or at least someone to write the classes here and then we'll move
them to their old file. So let's say we have a class that we can
call it a 3D shape. Starts so this
cloud shape, right? A shape. In our
definition of shape, we're going to have at
least height and width. Let us say we have a property. So a keyboard shortcut for writing our property
is you can write prop and then press Tab twice and it generates
that stub for you. We can have double height
with a capital H, of course. Then we can press Enter at
the end of filling that out. And then go to the next line
and say propaganda tab, tab and then double. Then you press Tab
to move from the W2, the property name, put that in. So let's call this one length. Then press Enter and
that's it, right? Typically, shape will have at least the height and length. No, you have variations of the shape because
triangles would probably have your height and length within
they would also have, That's what we call
the hypotenuse, so to speak, right? Cube. Cuboid would have height, would have length, and it
would also have breadth? Or would we have a base class so-called
obese grass of type shape. You have an access
modifier called obstruct. This means that I cannot, sorry, this has to come
before the word class. Abstract class shape means
that yes it's a class. Yes, it exists but you
cannot instantiate it. So remember, just
like with the static, we cannot see sheep new shape. We can't do that. That's illegal because abstract cannot do that. At the end of that operation, when we have defined
our shape class, I'm going to Control dots
and move it to its own file. So move shaved dot
csv its own file. So we are shaped dot cs. Noah want other classes
that are going to share properties of a shape so I can easily create another class, public class, and I'll
call this one cube. To inherit, I just need to see colon and then give
it the class name that is inheriting from by seeing public class cube colon. It is inheriting from shape. Now this is complaining because of the inconsistencies with the, with the accessibility
access modifiers. So this is public, while
shape is not public. So if I remove the public here than it should
be fine. There we go. If I made both of them
public, it should be fine. Class cube, colon shape. Then here I can define
my own properties. So the cube or a, we already have high to
already have length. So here I'm just going
to give it a width. Know what I don't need to go and define any specific properties. So even if I'd tried to
define C double height again. C-sharp is going to
underline it green. And let me know that well, you already have
shaped dot height. So by defining height here, you're overriding
the original one. Being redundant, you're
repeating yourself. I don't have to do that. If I was to instantiate Q, Let's say I say cube. Cube is equal to nu cube. I can always. Let me move this
to its own file. So Control dots and then
move to cube dot CS. Yes, I would like to move it. There we go. So now I have an
instance of cube. I can see cube dots. I can get access tool length, which is courtesy off
shape. I cannot get toxic. It's the width which
I just defined, and it can get access
to the height, which is also currently
see how shape, that is, what
inheritance allows. It allows me to
define one source of common properties and then reuse that source across
multiple places. In. The reason I
made this abstract is that I don't know
what the shape is. Even if I was to say sheep
is equal to new shape, what is the type of ship? Is the circle? Is it a cube? Crc cannot instantiate an
abstract type of sheep. It's abstract. So the concept of a shape
is indeed abstract, which is why C-sharp give
us that abstract keyword. What is a shape? So the actual manifestation
of a ship is one that we can quantify like cube,
cuboid, rectangle, etc. As many other types
as I can think of. So let's say I thought of also including tranquil
class triangle. That's also a sheep. And I wanted to
inherit from shape. All right, because they do
have common properties, then our class called triangle
is just going to have its own unique property
called maybe hypotenuse. I might be able to slacking
with my geometry here, but just work with me. So triangle is also
inheriting from sheep. That's two fewer places to
put, height and length. All right? So I can always put
triangle in its own type. So single responsibility,
once again, if we wanted to modify a
triangle crowds in order to finance if we wanted to modify a shape, you know
where to find it. If you wanted to modify cube, we don't where to find it. And the mean program is here. You'd notice that because
I'm using inheritance, there's this, It's a
symbol coming up here. This is called an
inheritance margin, will actually allow
me to jump between the base class
inheriting classes. So if I modify the base class, and then from the base
class I need to jump back to one of these
cuboid triangle. I can always just click
that jump over to cube. And it's a nice, easy
way to navigate between classes that are
inheriting from another. If you want, you can
go ahead and write old console red lines and red lines and fill the cube and then do some
calculations, right? But then that brings up
another interesting point. When he comes to us
doing calculations. We would have to know how
to define the area or the volume or whatever based on the different
types of shapes. Shape? Yes, it could have one method
which defines as getArea. Sure. Public double get area. Generally speaking,
for the area, you just need the
length times the width. If it's a triangle, it's half base times height. And let's assume that
the length here is the base times height. So you'll see that each shape is going to have its own
way of getting the area. So it's not quite
easy to just say, oh, I'm going to have one
abstract loss and have one, it gets area method. That will work. Of course, you could just
define gets era inside of each one of these derived classes. So we call those derived
gases whatever is inheriting. He's called the derived class. Whatever is being inherited
is called the base class. So those are the
two keywords they can apply to your conversation. I can always see public double get the area
for the triangle. And I can always see public
double get area for the cube. But then if I go to
another one, Let's see, I bring up another class. Let me just, let
me do it this way. Let me be consistent. So let's say I have
class rectangle. That's another shape
that we interact with. And it's also
inherits from shape. All right, and then it doesn't have any
additional properties and then in this moment actually forgets to give it
its own get era. So right here, I'm
going to be very inconsistent because
cubed can get the area, triangle can get the area. And I'm human, I forgot to put in the GetArea
class or method, sorry, in the rectangle class. No. I get the area of the rectangle and I published my program. I sent you told to persons and then I'm getting
this feedback, Hey, this doesn't work when I tried to with
our tongues, work. This will support a
built etc, etc, etc. That's kind of feedback that should employ you to do better. But my point is that there are times when
this can happen. So that is when we want to move our inheritance
from just classes, abstract class here,
and then start looking at inheritance using interfaces. Interface is scaled down
version of a class, but it's more like a contract. I'm going to show you
an example of that. So I'm just going to
write it told here. I'm going to say interface instead of
class we say interface. And then generally speaking, the naming convention for an interfaces that
you started with a capital I and then
you give it the name. So i shape. That's the interface
that we're working with. No, like I said, it's
lighter weight class, meaning it can
define properties, it can define methods, but rather it can declare them, but it doesn't define them. Just to buck truck. This is a function call. However, the definition
of the function is the actual code that
a function is executing. Let me jump back
over to my person. So here's the definition
of the function. Those are definitions to interface may not necessarily
define it because the definition of get era for rectangle is going
to look different from the definition for the triangle is different from the
definition for the cube. However, I can
declare that every, every derived class
that is inheriting from this interface must have a definition for a
particular method. So here I can see
that there should always be dull blue get area. Alright? So anything inheriting
from IS shape. Most implement double get area. This is why we call it
a declaration or some, some languages would call this like a function prototypes. So this is just seeing that this is the datatype,
this is the name. Maybe there are parameters, but that's all there is to it. It's not a definition
and it's not a function call,
it's a prototype. Know, I can let everybody
inherited from a sheep. Know. You're going to
see WE inheritance one, you cannot inherit from more
than one classes at a time. However, you can inherit
from a class and an interface or multiple
interfaces at that time. Those are the rules
right here. I can tell. Jaws triangle. Yes, you're inheriting
from shape. And let's see. None of them have
get era defined. Let me leave get era in queue. Triangle and a rectangle do
not have get era defined. Now remember that the problem
was that we shipped it told without the ability to calculate
the area for rectangle. And no, I removed
it for a triangle. Book cube is fine. Qb just needs to return
height, a whole, Is it again? No, it's width versus
length, sorry, length times width,
The area of the cube. Alright, that's fine. But then these two don't have any method defined for that. I can actually
just go up here to the inheritance side
after the colon and see Como I wanted to inherit from i shape
also, let me move. I shaped Switzerland file. Let me do that quickly. I notice that it's still just a CSS file and
the name is I shape. And you always wanted
to make sure that your filenames match
the name of the class. It's in its own file,
It's an inheritance, it's an interface rather should enforce the
implementation of this, a method that is being
prototypes are declared. When I go to the class that is no inheriting from my shape, I see an error.
What is this area? It's seen that rectangle,
which is the class, does not implement
the interface member I seep dot get era. You see in that I need to make sure that if I'm
inheriting from Isua, that I'm implementing what IS shaped said should
be implemented. So I can just do Control
dots and implement interface and it will put
the method stub for me. This is a good way to enforce at the editing, the
compiling level. That certain rules
are being met. Every shape should be able
to calculate its area. Rectangle should have get era by letting all of these
inherited from i shape. I am no enforcing the rule because this
program would never compile. I couldn't never ship it all in this state because
it wouldn't work. I have to make sure
that I am going to have our representation of getArea
before I can move forward. No, this is just going to throw not implemented exception, which is another exception
type, but that's fine. We can erase that. Then for rectangle, I can
return length times width. All right, why do I
have access the length? Because linked was defined in the abstract sheep and
it's inheriting from that. So I can easily access
them without needing to redefine and redeclare and
put into many properties. Length times, what's its height? It was height and width. So that means I need
to actually put in the property for, with fear. There are a lot of permutations
as the whole this works. I mean, I'm not going
to be able to cover all of the variations. I just wanted to give
you guys examples of why you would
have inheritance by a class versus inheritance
by an interface versus both. Cube currently is not inheriting from any interface
I can tell it to, but it already did the
implementation, so that's fine. There's no error there. Like I said, with the
inheritance margins, you can always jump between
the class files and you can jump to the definition
if you need to. You can see where it's being defined all over the place, etc. So here in triangle, if I tried to get
area of a triangle, it would give me
an aerosol here. I can have the definition inside the triangle class, half B. So that's 0.5 times the base, which I went to use as length. That's half base
times height, right? Sorry, it's been awhile begin to let's just say that's
half base times height, really and truly, right? And that is what the
triangle area is defined as. This is the rectangle area. And then the cube area is that even though
you're inheriting, you can have your own methods
here because I can easily see public double get volume. And the volume of
the cube would be length times o. I said length times length
here, length times width. Length times width times height. All right? Yes, I'm inheriting
11 is begin forests, but I can have my own and my own properties
even though I mean, inheriting, inheritance helps
us to reduce repetition. And remember, DRY principle, don't repeat yourself wherever you see opportunities like this, where you might be repeating certain properties
across multiple classes, still flat that you want to
put them in a base class and do inheritance to cut down
all of that repetition. Let's write a quick program know just to put all of
that to the test. So I have the cube already and I'm going to show
you the var keyword. And also var is a quick way to define a variable or an object. The thing with var is
that it will assume it's type based on what is on the right-hand side
of this decoration. So when I say var triangle, it doesn't know what
data type it is until I tell it it's a new triangle. If I said var rectangle, then the object call, the object is called triangle. What is still just going to
be of type or tongue glucose? That's a type I defined. This is a common it, you'll see what C-sharp development where
instead of seeing string name and
an age and so on. But I was strict on that earlier because I wanted us
to appreciate the data types. Over going forward,
you will see me using var, a bit more. Var triangle. I can say var cube. I can say var rectangle. Rectangle is equal to a new object off
rectangle, etcetera. Then I'm going to do some
console, right lines. You can hit pause and replicate those but the three objects, and then I have three. Little variables here. I'm just defining them as int, but like I said, equal
to easily just see far. Just by seeing virus with no, That link is supposed to be integer because on
the right-hand side, we're seeing we're
sending over an integer. That can be a double-edged sword because it's not willing to give you an error if the datatype
isn't what you expect. However, when you are
strict like this, you can see from a mile away
that you might be writing incorrect code for the
situation at hand, like here. I tend to favor using the primitive
datatypes when I need to. But for the more complex ones, I will rely on fire, but sometimes once again, you can use them
interchangeably. Here I'm just prompting
for the length, the width, and the height. Then I'm going to set cube dots. Length to be equal to length, cube width to be equal to
the width value coming in. Cuba, dot height to be height. Another the same for the
triangle with length, triangle with width and
triangle with height. Notice that this
one is giving me an error because
triangle does not have a width property
defined in it. So that was Visual Studio's
suggestions. I'll remove it. That's fine. And then rectangle
is going to be taking length and
width, whatnot tight. Know, there are different ways to define our assign
these values. This is one way to do it. There's another way to do it. There are at least two
other ways that he could be done more efficiently. In the next lesson, when we
were looking at constructors, I'm going to show you that after we have all
of these defined, we can easily do some
console dot write lines. I'll just borrow one-off
these and see triangle. Let me start with
cube, cube area. Then we call cube
dots, gets area. Usually that is after
we assign the values, we have a definition
for it gets era there and q would
also have get volume. So I'll just call that
at the same time. Then we'll just do the triangle. Here is called the
triangle gets era method. And we can do the same thing
for the rectangle area. All right. I'm just showing you that
when you start getting the hang of how everything
works, everything flows. You will see that object
oriented programming is there to make
your life easier. So look how much
code we've written. Leuko minute things
we've done yet, or program.cs is so simple, it's just defining
three objects, getting data and then sending it over and then the process
to get all of those values, those are hidden from us. When I test this out, I'm just going to use
the same three values obviously for all three shapes. The cm length to see
him within the seam. All right, So let me
free up my console. So let's say length
is 50 by 40 by 32. And then the area of
the cube is 2 thousand, but the volume that should
have been volume is 2032. And then the triangular areas. So this one I'm not
sure if that's right. Yes, I knew he was incorrect
because I said plus instead of asterisk, apologies. But I hope you see the value of inheritance
in this context. Once again, through inheritance, I was able to one
creates a base class, that's I call it an abstract
class called shape, that define certain
shared properties that all shapes have. And then I could let
every other shape just derive or inherit from
that BCE shaped gas. I also looked at
using inheritance to enforce that a certain method or a certain myths thoughts, because you're not
limited to just one. But certain methods were
enforced in the shape. So here I think of alleviated
some naming convention. So control data and it should be Gettier with a capital G. Same thing for it to get volume. So it wouldn't throw
incarcerated for me. What I was saying is that the, the interface when inherited
acts like a contract, a contract states that you are obligated to do this thing. Once you're inheriting
from this contract, he means you're
already good to carry out the operation
defined by the contract. That's basically, oh, whoa,
inheritance interfaces work. And he didn't get far more
complicated than this. But when you get to
bigger applications, then you will start seeing it. But this is a nice
quick introduction to inheritance with
classes and interfaces.
26. Constructors: All right guys, In this lesson we're looking at constructors. So I'm going to reuse the inheritance project
only because it already has so much coordinate and I
think it's a perfect way to show what constructors
are and hold their useful. When we're going
through inheritance had mentioned that there
are easier ways, quote-unquote, to assign values
to newly created objects. One such way would be to, and I'm going to just
rearrange the code little tear someone to move the object definitions to below, where we asked for the values. When I am defining cube, similar to when we
were doing the IRAs, when we could say give me a new area and then put in
the values at the same time. I can actually do that
with a brand new class. I can actually say,
give me a new cube, open and close curly braces. And then I can start assigning the different values to the
different properties, right? So I can see a width. You should get the value of ten. Since I got it
through user input of whatever k-mean toward
local variable. All right. You get the length of whatever came in through
our local variable. Height gets the same treatment. Alright? That is, this one line of code would actually
eliminate all these three. Sit at. This one line would be seeing
given me a new cube that is defined to have the width
according to what came in, length and height
according to what came in, as opposed to me going cubed dot length is equal
to length cubed dot width. It works. But once again, I'm
showing you alternatives. I can actually remove
those three lines of code. I can do the same thing
for the triangle. And I would actually
not running into the same issue when I was doing the triangle that
accidentally put in width. And I didn't ask where our
hypotenuse for my triangle. You see that? That's fine. Height is equal to height Como, and then length is
equal to length Como. Then I could easily put
in our hypotenuse value. I didn't ask for one, so I'm just going to hard
code it to ten. So that eliminates those
two lines of code. And then I can replicate that with the rectangle instead
of typing in from scratch, I'll just borrow the
code from the cube and remove the height, in fact, I believe is the one that I
don't need for the rectangle, which eliminates those
two lines of code. Look at that much neater. No, that's not what
constructors are. Working my way up to showing
you what constructors are. Constructors are default methods that are going to be created in a class that enforce
that an object must meet the specific occasions before it can be instantiated. That's a mouthful. Let me explain. No, there is a
default constructor that takes no parameters. So that's why we can just see cube is equal to a
new instance of cube, whether or not we
provided values. Because remember it worked, you worked earlier
when I created the new cube without
any values there. So it was just an
object with no height, width, or length. That
would have been 0. However, if I wanted
to enforce that, in order to create a cube, you have to provide those
values from the get-go. Then I would enlist the
help of a constructor. To create a constructor. I'm just going to press
Shift 121 cube and jump over to the definition. In force out constructor, you can easily write the keyword CTOs and
then press Tab twice, and then that generates
this method stop. Node. Constructors are
characterized by the fact that one they're
public and the tool, they have the same
name as the class. Three day take r can
take parameters. Default constructor
is one that doesn't need parameters without
me actually doing that, there was a default,
it's implied, which is why I was
able to get away with cube with no parameters. That's the default one
that is already implied. But sometimes you
would find people that are very explicit
with the default one, especially when they have an alternative one that
is not the default one. You can actually
have constructor overloads the same
weekend method overloads. I can define another
constructor, CTR top tab. And then I can say no, you see it's complaining
because it's seeing two constructors
that are identical. It's seen these
already one here with the sea environment that
types, that's fine. But what if I wanted to enforce that in order
to create a cube? You have to give me width. You have to give me a height. And you have to keep on
splitting height wrong. And then you have to give
me what's the other one? Breadth and length. There we go. You have to give me
these three values. And once you give me
those three values, I can easily instantiates the
properties to those values. That we can't have a cube that has no sides and think about it. Have you ever seen
ICU with 0 length, 0 within 0 height? Maybe you have, it's
called an invisible cube. In order to create a cube, you must give me the height, the width, and the
length of the cube. So I can actually remove this default constructor and
have only one constructor, which is seeing, I
need these three. No, when I do this, look at what happens
in the program.cs, there's no way I can
define an object of cube widow passing in those values inside of
the parameter listing. So even though this does not
appease the compiler, right? So let me remove that and show you that in order
to create a cube. If you hover over it, it's going to show you that
you have to have the int width in
tight and length. So I'm going to have
to provide the width. And I'll do that with
the user provided value, the height and the length. No, it is satisfied. So now it has that cube. When you say, give me a new cube and you
say give me nu cube, you have to provide the width, the height, and the length. Once you do that, all the values are going to be assigned. Because in the constructor
I said once you get those automatic idea scientists, this new object of a cube already knows
what its width is, what its height is, and
what it's meant is upfront. I don't have to tell it
afterwards like this, our whole we had
the code initially. All right, triangle
can still get away because I didn't define
in the constructor. So even if I give it the
default constructor, it will still work. But let us say I gave it both. Both the default and one
that says give me the width, sorry, not with the length, the height, and the hypotenuse. Or they'll say hype, right? Or I could enforce that. I only want the hypotenuse. It's up to me what is
the default value? What is the bare minimum that this object must have in
order to be instantiated. If I have two
constructors like this, then what happens is
that I can easily define a default one like this and
assign the values later on. Or I can have another one where
I can pass in the values. So let us see I'm going to
use the other one node. So if I say triangle
open parentheses, then you will see that
it's showing me that I have two options to
create a triangle. I can created blank, or I can put in the
hypotenuse value. And let's put in ten, or I put in user
input, whatever it is. That's all that is
required of me. Know I have triangle using
the default constructor. Let me do it in triplicate. So you have the
default constructor. This is completely empty. No height, no with no length, which is not really practical. Then you have the one where yes, I created it, empty boats, I'm assigning the values. And then I have
one where nothing else has any values
except the hypotenuse. All right? So all of those things are options when it comes
to putting in constructor. So you can have
multiple constructors. So lots of times when we're
defining new classes, even C-sharp built-in classes, you might find that you
can define an object. Sorry, when we're defining
objects off glasses, you can define the
objects with parameters, sometimes with all parameters. These are just overloads off constructors that
exist in the class. If I create one constructor, that is my base constructor. If I create multiple
constructors, then those are all options
because I could easily just expand this and say
give me the hypotenuse, the height, and the length. If you want. You have that option also if
you're creating a triangle, if you can't do it blank, you can give me
just a button user, you can give me all three. What am I going to do when I get the values in each settings? So when I get the
hypotenuse alone, I would say that hypotenuse
here, new values. Keep. If I get to all three, then hypotenuse your
new value is heap. Sure. When I get the height, its height, and when I get
the length, its length, then all of my
definitions can be characterized by all of
those options, right? So triangle three
could use the Option, option overload s1, we're just passing the hypotenuse
is one where I pass in hypotenuse
and let's use ten. I pass in the height, and I pass in the length. Three different ways to
instantiate a triangle object. Of course, each one
is going to have a different outcome
because this in its current state when I
tried to get area down here. Sure. I'm just going to get 0 because there is nothing I signed. However, getting ear from this one would give
me a bitter value. Let's try that
with Triangle Tool and then try it with travel, the one where we only
have that button use, that's probably going to give
me 0 also cause that's half times 0 times ten, which is 0. And then this one
where I'm providing all the values should give
me yet another outcome. So let's say that for a spin, All right, so entering
some random values. And then we see here
that we have two zeros. The first 0 for
the triangle here is because it was empty,
those empty objects. The second one is triangle to where it got
the height, the length, and the hypotenuse
hard-coded as tin, which of course could easily
have been user inputs. Then we have the third one
which is 0 because well, we only got the hypotenuse
nor height and width. So the half base or
half-length times ten is going to give us half times
0 times ten, which is 20. Then the triangular area for
the third one where it got ten to height and the
width is going to be 270. Alright? So that's pretty much, That's pretty much it for hole you use
constructors, right? So once again,
constructors are there to enforce the requirements for a certain properties
before you can move forward with the
instantiation of an object.
27. Create GitHub Account: Let's look at creating
a GitHub account. If you don't have one of these, then you can get one in
maybe five or fewer minutes. Step number one, go to
github.com, that's GIT HUB.com. Step to go ahead and give them your e-mail
address to sign up? No, I already have an account. I can actually login to my
account and show you what it looks like. That's what it is. It's almost like a
social media platform, but for developers and
open source contributors, and the more
followers you have is the more activity you
will have on your screen. The more repositories or
projects that you add, the more you have here. So it's very advantageous for keeping backups of
your source code, for referencing other
person's source code. And in general, collaborating
when working in teams. So go ahead and
create your account, and once you do that, you can move on
to the next step. Like I said, it is a
very simple procedure. You give them an email address. I'm going to try and
give them one that I don't think they
have for me yet. And then once you do that, you just go ahead and fill
out the rest of the form, enter the e-mail content
and give them a password. And I think after this they send the verification code
and you check that. And then boom, you
have an account. Once you do all of that, you're ready to move
on to the next lesson.
28. Add Solution To Source Control: All right guys. We are at a very good milestone. We have done a lot since we started this
course up onto know. We have a number of
projects and a number of code references and exercises. And I think it has been a
wonderful journey thus far. This juncture I wanted
to introduce to you the concept of source control, and it's a very simple
concept to grasp. Firstly, you'll need
your GitHub account, and I'm sure you've
already created one of those from Visual Studio. It is very easy to integrate
with your GitHub account. The first step here, and I'm just going to
close all windows, all bits of code. I can just right-click and
say all tubs goes on TBS. So we have a blank window. From here. If you look at the bottom
right-hand corner, you'll see a blended as
his ad to source control. And you click it,
it will say get. If it does not see that, then you may need to revisit the installation of
your Visual Studio. So when you click Get, it will actually launch
this little window that is suggesting that you
create a Git repository. You may also ask you to authenticate against your
GitHub or cones at this point. So here you have two options. You have GitHub
and Azure DevOps, both our Microsoft owned, both our variable
for both of them are source control engines
that are supported, are support that gets protocol. Either one works.
However, I'm going to show you with GitHub as it is the most recognized
open source platform. And he told us you to
publish your code and have a portfolio to show others that this is what you've written,
this is what you'd know. Proceeding with GitHub, you can just go ahead
and fill in off-course, you're authenticated so it would fill in certain
blanks for you. Then he would see a
repository name and then you can give it some more texts. C-sharp with net six, I went to see console
programming with knit six. And he can give
it a description. Activities surrounding the B6 of c-sharp programming
in dotnet seeks. Know. You may be wondering, okay, why do I want to
publish my code? I have it on my computer. Well, one source control
helps you to manage changes. So we did a lot of changes. We weren't trucking
with source control. So I'm sure if I asked
you to recall and our undo some of the things
from the earlier lessons, it would be very difficult. So that is a benefit
because if you make a mistake to what was working and you're
not sure how to fix it. You can always go back
to Source Control, get the version that was
working and then start over. So it's good, it's
beneficial for that reason. Another benefit, once again, is that it helps you
to share code not only with potential employers
or your friends, but also with your team. If you're working on a project
and all of you have to be contributing instead
of copying and pasting, I'm trying to zoom session. You can actually just
use source control and centralize where
the code is stored, which will be on GitHub. And then everybody
would like subscribe to that repository and be able to see everybody else's
changes, make changes, and everybody can be on the
same footing when he comes to leveling up diversion or contributing to
the application. I'm going to untick private repository because
I don't want this to be private if you want to
hide your application from the public than
you choose private, otherwise you untick that. And then you can just
say Create and push. Once you've done that
successfully and noble things will change
in Visual Studio one, you will no longer see the button that says
Add to source control. And I just wanted
to show you master. I just wanted to show you this. It'll get symbol with the name of the project
or other projects. You can see I have a number of projects on Source Control. You'll also see
some blue padlocks appearing beside your files. Those mean that they
are checked in there? No. I have the same level as
what is centrally hosted. If I'm to go to my
GitHub profile, I would be able to browse
through my repositories. Let me start from scratch. So this is my GitHub homepage and to the left are
my Repositories. I can see console programming within that seeks and
it will filter and show me the project and all
the files saw off the project files that I uploaded are here for public access
because I made it public. If I wanted to change,
it's a private. I go to settings and I scroll down and I see
change visibility, then that would allow me
to change it to private. I could also archive
transferred to somebody else or just delete it if I really
didn't want to anymore. I can also rename. So as long as you don't
clash with names, then that's no problem. If I wanted to add persons
to this repository, meaning by my teammates, persons I'm coding
with for free, you can add up to, I think, about five
collaborators. So you just have to confirm your password and then you'll
be able to add people. All right, so I can
go ahead and say, eyed people search
for the username, send them an invitation. Once they see that invitation, they can accept and they
will have access to the repository where
they can come and close knit or open it with Visual Studio by clicking
code on their own computer. They can say Open
With Visual Studio, which will then allow
them to open the project locally in a new
Visual Studio window at a location of
their preference. So if I changed this path
to testing, alright, now I can clone it
and it will get to, it will automatically
download all of those files and set them
up for me on my computer. This is what your
teammates would be doing after you created
the project, your team, it would go ahead and clone and then they
will be able to open the solution file
console programming SON. And then they would
be able to interact with all of the project
files accordingly. Now, when I change
his me to a file, let's say the Hello World, this was very simple. And then maybe I
made that change. Whatever the change is, it could be new text,
it could be a space, whatever it is, you'll see
that this now has a red tick. This red signal
means that I have modified the file
since the last time. So this is going to be
a test modification. That's my modification. I have modified
the file relative to the last version
that was on GitHub. After I've done my change, we're working as a team, no one. It was my opportunity
to know to modify this particular parts of the program and I've done
it and I'm satisfied. I need to check it in to GitHub so that everybody else can have access
to the change. I would have to go to a window
called the good changes. You can get to there
by going to View and seeing changes will
actually hide it earlier. But it was Datsun when students
to dock it to the side here alongside
Solution Explorer. There we go. I'm not sure why not going to get
fixated on that. Get changes. That's
what I'm focusing on. So get changes, one, you put in a message,
what did you modify it? I would give a nice message, modified the Hello
World program. Then I can see commit
all and seeing, so click that little
caret besides comments All and click Commit
All and sync. And what this will do is
push that change to GitHub. So no, all I did was
a few button clicks. Now the padlock is back. And if anybody else
goes back to GitHub, notice all of these project
files when I refresh, it's going to show
them it at 20 seconds ago there was a change. And it's going to show me which project was changed
and what the message was. If I click on that
message, modified program, you see here that
it's showing me that it added two new species as well as what's in red is what was what's in green
is what note is. All right. That's why I said he can
track changes because now you can see exactly who
made that change. So somebody who destroyed the application, you
know who to blame. Alright, you can easily
truck all the changes that have occurred on each
file with each iteration. And you have all of the
versions that he can go back to seek and
click on the commits. Then you can see based
on the messages, who did what and what
the message was, you can always go back
to that version of the code and retrieve that one. All right, so this is good
for if somebody in the team destroys the code or even you destroy the code and
you wanted to revert, it's easy to just scroll back
to that, a cleaner version. The flip side for the team members who are
waiting on the updates. So this is the original
Visual Studio window. And if you look
at my program.cs, it does not have
that update, right? So let us say no, I made the updates
and, you know, needs to retrieve
the latest code for that update onto your
application, onto your machine. All you have to do is
go to get changes. When that window comes up, let me fix the
docking once again. When get changes comes up, you just need to
do a pool cycle. I'll give you a call
and say, Hey, you know, I finished the work and I
checked it in, get later. So you're like, Okay, thank you. And then you just come to get changes and then you do a pool. And then it will
go up to GitHub, get all the changes,
and then look at that. It just automatically
updated my file for me. That is why GitHub is perfect
for team situations and not just github bought using a source control
management system. Github is just one
of your options. You have other options are there get is one
of the protocols. But I'll be the most popular. And the one that
I would recommend GitHub is one of the
most popular platforms, easiest to get started
within my book, and also one that I recommend.