C# Basics - For Complete Newbies ✅ | Grant Klimaytys | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

C# Basics - For Complete Newbies ✅

teacher avatar Grant Klimaytys, Software Engineer

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

13 Lessons (1h 20m)
    • 1. Introduction to The C# Language

    • 2. Variables in C#

    • 3. Operators in C#

    • 4. Converting Variable Types in C#

    • 5. Comparing Stuff in C#

    • 6. Have a Break and Bonus Content For You

    • 7. Round and Round We Go Loops in C#

    • 8. Arrays in C#

    • 9. Methods in C#

    • 10. Classes in C#

    • 11. Inheritance in C#

    • 12. Exceptions in C#

    • 13. Summary of The C# Language Grasp The Basics Optional

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

Have you ever thought, "I can't program" ?

I once thought that but I tried anyway. Turns out it was pretty easy and after a few months I became self employed, ramping up my earnings to the tune of $120,000 per year.

Would that amount of money change your life?

Well this C# course is where it starts, but only if you click the ENROLL button. Give it a try, you have nothing to lose and everything to gain!


Meet Your Teacher

Teacher Profile Image

Grant Klimaytys

Software Engineer


My very first software program was the artificial intelligence brain of an underwater robot in the early 2000's, still the coolest project I have ever worked on!

Since then I have designed and built websites, software and apps using all manner of languages and frameworks. Javascript, Bootstrap, .Net, Python, PHP - you name it, I've probably used it.

These days I focus on building quality cross platform apps using Xamarin studio in C#, Xcode and Swift 2 and Android Studio.

If you're considering becoming a freelance developer then I can honestly say it is the best life in the world. You will have no boss, earn an hourly rate of $60 - $150 and take holiday whenever you want!

Of course you have to learn how to make good apps first, which brings me to my second pa... See full profile

Class Ratings

Expectations Met?
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Introduction to The C# Language: Welcome to this section on learning the basics off. See shop. Once you learn the basics of C shop, you are going to have smooth sailing through C sharp courses and indeed, any other object oriented language. The only thing that changes between say, see shop Java or JavaScript is the language itself. The themes and the concepts that I'm going to teach you here are applicable to almost every object oriented language you can think off. In this section. You'll learn about things such as variables. These are the basic building blocks that hold data inside programs. You'll learn about operators that we can use to adul subtract, for example, some of those variables. You'll learn how to convert between different variable types when you should, and when you shouldn't, you'll learn how to compare variables due to numbers actually equal each other. You'll discover all about loops, which are instructions that run round and round until a certain instruction has been met. Then we'll look at a rays, which are lists of data. Well, look at methods which are ways off dealing with data such as variables or a raise. Well, look at classes which are one of the fundamental building blocks off all object oriented languages. Well, look at class inheritance, which is another cornerstone off most 00 P. Languages. And finally, well, look at exceptions, which is a fancy way of saying errors when your code does something that it doesn't expect will learn how to handle them. So you don't experience a crash in any of your APs, so let's get started. 2. Variables in C#: let's begin by looking at variables in C Sharp. A variable is the smallest practical working block for most applications that you will construct. This isn't technically true, but practically speaking, it will be now, as this is the first lesson in this little miniseries, we have to set up our development environment. We could go about setting up visual studio or mono develop or a similar environment. But these days, as were in 2016 people have already set up a Web sites that allow us to develop in C Sharp and evaluate that development. And one of my favorites that we're going to use throughout this section on Learning C Sharp is to be found at www dot c sharp pad dot com. So head over there now and let's begin learning about variables in C sharp. As I said previously, a variable is a container that holds a little bit off data. Now in C, Sharp variables have what's called a type, so their containers that can only hold certain kinds off data. The very 1st 1 we're going to look at is called an integer, and this is defined in C sharp, generally by typing int there are different kinds of vintages, such as an int 16 or an int 32. But for most purposes, simply defining int will do the trick. Then a variable needs a name because variables are there for us to use later on and reference. And the name is the correct way to do this. Now. You should never name a variable after its type, so you should never call this int int because see shop and dot net will get very confused and your program won't compile, so you have to give it a name. Let's call this grant Age and your Motus. I've placed the first letter in lower case, and I've placed the first letter off the second word in upper case this is called Camel Casing. It is a common convention in C. Sharp. Don't worry, you'll pick this up as we go along. Now we have to make that integer equal to something so we simply type equals and this is called an assignment operator. It's going to make whatever we put on the right hand side, and it's going to grab it and store it inside the integer called Grant Age. And because we've said explicitly. This is an integer. We can only store whole numbers in it because that's what an inter Juries in the programming world. So my age is 33 if I remember correctly, and then we put a semi colon after that declaration to tell C Sharp where ending this line where we are. So now we have an integer called Grant Age stored as 33. Of course, we can have other types of variables. What if we have a more complicated number where we can have something called a float and we can have grant age in months is equal to whatever 33 times 12 is? I'll probably get this wrong. Something like 360 point 87 and floats allow us to have a decimal point, but we have to put an F at the end off the number to indicate that it is afloat. We can also have numbers called doubles, and this could be a gin. Yes is equal to 33.45 and you don't need to upend that with anything, so that will store some decimals for you. We can have another kind of variable called a Boolean, and we define this with Boo and I'm going to call this true or false. And a Boolean can only contain one off two values, either true or false, and we define it as true or as false. And these variables billions help us keep track of things that are switched on or off inside of our program. So they're very handy, right? We have one more variable that I'm going to show you in this section, and this variable is something that holds letters and characters, and it is called a string, someone to call this my sentence is equal to, and the way we define a straighten is by enclosing it in double quotes. Say my name. It is Grant and close your double crooks. So this stores a bunch of characters for us that we could possibly want to display to a user as a message. So, in effect, think of strings as items that hold human readable words or numbers. Okay, what if I wanted to change one of thes items that have already defined Well, I could say grant age and C sharp references that integer that I first declared, and I can make it equal to 34 instead of 33. So now that Grant Age box holds a different value, it's no longer 33 that's been over written, and now it holds 34 in Si shop. There's no way to go back in time and undo this. So if you're completely new to programming, you can't simply say, Undo that operation and give me the old value because the old value is gone now what if I want to change Grant age to a string grant? Age is equal to 34. Let's see what see Shop pad has to say about this. I've got a little triangle on the left and it says, cannot implicitly convert type string to int. What this means is that if you have a container that you've explicitly said stores and integer, you can't then go and put a different type of item in it so you can't put a string in it. You can't put a Boolean no floats and no doubles. It can only hold in Tages, so never try to mix your variables. Now what if my name is Grant never changes as a sentence well in that sea shop If someone else came along on my programming team and said My sentence is equal to name is Fred, they would change the value off my sentence. And if I don't want them to do that because I realize that I want this sentence to be always the same, what I can do is put a key word in front of string my sentence called const that stands for Constant and I'm telling, See sharpen .net. This thing can never change once I've set it. That's why we're now getting are warning triangle that says the left hand side of an assignment. Let me just expand this window must be a variable property or indexer. It's basically say in really complicated language that, hey, dude, you defined a constant earlier and now you're trying to change it and I can't do that, so it will give you an error. And this kind of thing ensures that other programmers don't change the strings or the variables that you want to keep constant. Okay, that's all. There is really two variables in C sharp. If you want to know more in simply, jump on Scougall and look up variables. But this is all. You really need to know at this point just grasp the concept that a variable is a container that stores a value off a certain type. 3. Operators in C#: Let's learn all about operators in C Sharp operators are Justus. You imagine there the plus minus is multiply and divides off this world. We're going to use them on some variables. And now that you already know how to define a variable, this lesson should be pretty easy. Go ahead and open up C sharp pad dot com and let's begin start by defining to inter jizz. Let's have int. My number is equal to nine. Let's have another int other numb is equal to 12. Now let's look at operators. Let's say interject result is equal to mine. Um, plus of the numb. And what this does is grab mine, um, and add other numb to it and provide us with an integer result at the end. How do we know this is wet? Well, in C sharp weaken, send ourselves a little message by using console dot right line and then we can pass it over the item. We want toe output to our user. So let's try result and close brackets now They should write out the result to our consul. And how do we access this in C sharp pad? Well, week Simply click this go button at the bottom right. When I click go, it's going to add those numbers for me and print out this result 21. Of course, we can do the same calculation by copying that code. Re pasting it in. We can do. Let's try a multiply, which is the asterisk character. Let's click Go and our result is now 108. Let's paste that in again and try to divide so nine divided by 12. We would expect North 0.75 to come out of this, but we get zero now. Why is that? It's because an integer can only store ah whole number. It cannot store decimals. And when you do calculations that have decimals in them, what the interview does is simply drop off the decimal. It doesn't care about it, so it'll just return zero part off 0.75 So when you're using operators, you need to be very, very aware off this phenomenon, and you need to be hyper familiar with what certain types of integer are capable off doing or not doing. So how do we fix this particular problem? Well, let's copy that code back over with the division inside of it, and let's modify it into a double type and another double and make our result a double. Then let's click Go and we get access to the full decibel number that we expect off not 0.75 so doubles a fine. You could also do this with float types. Now what about strings? It turns out you can actually add strings together, So let's to find some Let's have string. May is equal to ground. Let's have string. Last name is equal to Klim. Then what we can do is we can say strength. Food name is equal to name, plus last name. Then let's print this out without Consul, right line as full name. Feel free to change these things, or the strings that you've quoted according to your own name. But once you've done that, click go and your console will print out your full name. Now, of course, there's generally a space between first and last names. So let's fix this little era we have. That's copy our code from last time. Paste it in. Let me create some space so we can see and what we can do here, where we define the last name is we can add a space in so we can save plus double quote space, double quote, plus last name. So in C sharp, we can define a variable on the fly. We don't have to give it a main, and if we don't give it a type C Sharp will try and infer its type. When you put things in double quotes, See, Shop assumes it is a string. So this is simply taking a space character and inserting it between the two names. When I click Go, we now have my name with a space in between. So those are very basic operators in C Sharp. There's a hidden one in here that I referred to in the last lesson, and it's this equals sign. It's called an assignment operator. We're making this site last name equal to the right side Klim. So that is also known as an operator 4. Converting Variable Types in C#: Welcome to this lecture on type conversion in C Sharp. Let's get started right away. Open up, see shop at dot com and let's begin. So we've looked at variables and operators on variables so far. What if we have a variable that we need to convert to a different type? Well, we can actually do this, but again, you should always be careful when doing this. Let's see how this works in practice. Let's have an integer called my number equal to nine. Then let's have a strength called also number equal to zero. Now, even though I've called this a number, it is actually a string. So if I wanted to add these two items, I wouldn't be able to, and I'll show you what I mean. By that I'll type mine, Um, plus also numb. And this C sharp pad will throw up our little warning triangle. So how do we get around this? Well, let's first create. A far result is equal to mine. Um plus also known now the also numb. We could convert from a string to an integer if we wish, and we use what's called the convert class. We can say to integer 16 bit, 32 or 64. Let's see what happens if we use the 16 bit version. It's open a bracket and pop are also numb into their, and that seems to compile. Fine. Then let's print out our console dot right line result. Then that's Click Go, and we have our nine plus zero equals nine. Now there's something you have to watch out for here. If I copy this code, paste it back in and I make my string equal to grant, which is clearly not a number. Let's see what happens when I click Go, we get an error. Input string was not in a correct format, i e. You told me to convert the words to a number, and I cannot do that now. This gives us a nice little, pretty error here. But if you try to do this in an app or any kind off software, you would get a crash. The whole thing would die. And of course, that's not a very pleasant experience for the end user. I'm going to show you how to get past these sort of errors a little later on in this section, but for now, Let's look at converting some more variables in C sharp. Let's say I have a double. My numb is equal to 786. And then let's say I wanted to add this number 7 86 to 1 of my sentences so I'd have string . Sentence is equal to my number is I would close off my quotes and then I would add my numb . And if I close off that line, you'll notice we have what? We don't have an error here, and that's because sometimes see shop congrats. I double and infer that you want to change it to a string sometimes, but we're going to explicitly ask it to do this. So we're going to invoke a method called my numb dot to string with a capital T. And what this does is explicitly change this to a string. So sometimes, yes, you can rely on C sharp to infer what you want, but I find it's usually better if you explicitly tell it what you want. So now we can, of course, print out with our consul right line. I was sentenced, and when I go, my number is 786 and That's really all there is to converting variables in C sharp. I'm going to leave you with a warning that if you can avoid variable conversion, then definitely avoid it because it can cause a lot of problems down the line inside of your code. 5. Comparing Stuff in C#: when we're programming. One of the more common tasks that we need to perform are comparisons and comparisons. Take a look at two variables and see if they're equal or one is greater than or less than another. Always check if another one of their properties is the same or different. Comparisons will form a core part of your programming logic, so let's see how they done at C sharp pad dot com. Let's create to inter jizz to compare int mine, um, is equal to 10 and int. My number two is equal to 20. So how do we compare these two? The first way of comparing two variables is to use an if statement if open brackets my numb is equal to, and the way we define this or type this in C Sharp is with two equal signs next to each other, and then we pass it, the one we're going to compare it to, which is my number two. Then we open up a curly brace and we close a curly brace. Now, if my num is equal to mine, um too everything between these two braces, we'll get executed. So let's type console dot right line numbers are equal, then let's click. Go and see what happens. Well, nothing's output on our console because the numbers are not equal, so this code never gets called. Let's copy that section of code paste of in and let's change our numbers to both Be 10 then click Go, and we get a message back saying numbers are equal because the if statement has compared them. Come back with true and allowed us to execute the code. Now again, let's pace that in. Leave the numbers at 10 and 20 and we're going to play around with this. If statement a little more, we can say if the numbers are equal, will do that. We can also add an else and else simply says, If the if statement doesn't execute, then I would like you to execute this as a default. So inside here we can have some more code console right line numbers are in equal. If we could go on this, we get the outputs off in equal. That's great. Now what if I copy all of that? I paste it in again. What if we have sort of two items that we want to check where we can add and else if my numb is not equal to my number two. So this is going to achieve the same thing as before, except that I can check explicitly if these two numbers are not equal to each other on the way we define, Not equals is with an exclamation and a equals. And the beauty of doing it this way is that if I wished I could have another else if that would check for something else on, we'd have whatever condition in there and we had some more code to execute. And I can have as many off these else ifs as I wish I can keep going and checking for program logic. So let me just remove this last one and run. This numbers are unequal because it's gone into here. Seen it? They aren't equal and returned this code. So that's a very basic level. That is how you compare stuff in C sharp. Now you can imagine if we have 20 things to check if else if is going to start getting a little bits unmanageable, and to solve that, we can compare things using a switch case statement. So I'm just going to reload C sharp pads to give us a fresh window and let's look at house . Define a switch case statement. Its first making integer called My Numb is equal to one. Then let's define our switch. This is going to crab mine, Um, and then we open our curly braces, create some space and close them. Now, inside, off these curly braces, we define a case for each switch. So let's have case one. And this says if mine, um, is equal to one, then we are going to write console doctor, right line number is one then, because we've executed this case, we need what's called a break, and this stops executing the whole switch because we found our case. Now, of course, we can keep testing. We can have a case where the number might be, too, and reprint back to and so one and so one. What a switch statement does need is a default case, so if it cannot find any of thes, then it will go to the default will have console dot right line, and we'll have something like, let's just say default case and of course we need a break with that and then let's indented to make it look good. So that is a switch case statement. As you can see, it's a fair bit easier to read than a whole bunch off if else statements. And in some ways it's a little more flexible. So let's run this. And I would expect numb is one. If I copied all of this and I changed my numb to 10 we should get the default case, which we do. So those are two ways of comparing stuff in C sharp, the if else statement or the switch case statement. 6. Have a Break and Bonus Content For You: So I hope you guys are enjoying this section so far on the basic C sharp language. This is just a little intermission toe Let you know about a special offer that I have for you. If you head over to learn app development dot com and sign up for the email newsletter, I will provide you with a free, basic C sharp cheat sheet that contains everything you've learned so far in this section. In a handy reference guide that you can simply flick to when you want to look up a raise, conversions or inheritance, whatever you need is contained in this cheat sheet. It will save you hours of time trolling the Internet, trying to find answers, And it will allow you to create amazing APS in much less time than it takes a regular developer. So head over there. Now sign up completely free, and I will send you this guide 7. Round and Round We Go Loops in C#: Let's learn all about loops in C. Sharp loops are required in order to run an instruction multiple times until a certain condition is met. So go ahead, fire up C sharp pad and let's begin. Let's begin by defining an integer called I equal to Not this integer is going to be the counter that keeps track off how far round, or how many times we've gone through our loop. The first loop will look at is a while loop, and this loop does things while a certain condition is or isn't met. So we have to place that condition inside of brackets, we will say, while I is less than 100 open curly braces and close curly braces. Then everything we define inside of this loop will be run. Whilst I is less than 100. Let's have a console dot right line and put value off. I is equal to Plus I got to string. This will keep running as long as ized less than 100 so we need some way to increment I every time we go around. The first and most obvious way is to say I is equal to I plus one and that's perfectly legitimate. But in C Sharp is a much shorter way off writing this, we simply say I plus plus, and this adds one to an interview. So every time the loop goes around, we add one to I. Let's see what happens when we click Go and we have I printed out 100 times from zero to 99 . Now can you see a place where we could get an error? I'll show you back in our original code, which I'm going to copy and paste. If we had neglected to put in I plus Plus watch what happens when I click Go It spins and spins forever, and it says my execution took way too long. And that's because the while loop kept running forever because the value of I never changed . And this is called being stuck in a loop. It's something you obviously want to avoid in your code because you cannot execute the rest of your program. If you're stuck inside off one bit With that in mind, I'll show you a better way of constructing a loop. So let's refresh this page so we have a fresh window and this loop I'm going to show you is called a four loop four. And then we define how many times this'll loop goes around by saying Interject, I equals not until I is less than 100. And then each time we go around, add one toe I with I plus plus. Now I say this is better because every time you write this loop, it forces you toe have a beginning, an end condition and an operator to increase I. You could also have I minus minus, which would defeat the whole point off this. But I tend to find that having a four loop helps me avoid that endless look problem that we have in while loops. So let's try our console dot right line inside of this loop I is equal to plus I dot to strength. Let's click go and there we have our values printed out. You'll also notice this is a lot shorter and more compact when we come to run our coat. It also has the advantage off being able to use I again someday to copy that, going to paste it in and again and again now because thes eyes only belong to the four loop . They won't affect the next four loop at all. So in each of these four loops, I will always start as zero and see shop looks at them as completely different variables. If I had a wild loop and I had defined my int, I equals not outside of that, then I would have to be very careful off all of those values of I interfering with multiple while loops. So when you come to create loops, my sincere recommendation to you is to always use a for loop, if you can. 8. Arrays in C#: a raise in C shop. An array is exactly what it sounds like. It's a list off items that are all group together in one container. It's an array of stuff. Let's see how to create a raise and pull values out of them over on C. Sharp pad dot com. First, let's create an array off strings. Let's have a string and then we need to add to square brackets an opening and a closing to this definition, and I've just put a space between them. So it's easy for us to see if you remove the space. It looks like a strange character. It doesn't matter when you come to code this in something like visual studio, you won't have to put the space in because you'll be able to see them separately. Okay, let's call this. My array is equal to you, a new string array. Then we open some curly braces and we feed it with some values. Let's have learn. AP Development clothes are curly Brace and put a semi colon in. Now we have defined a string array and given it three values. What if we want to define an Inter Jura ray Well it's the same process. Int open and close our square brackets int array is equal to and you int open and close square brackets. Open your curly brace and put in some numbers separated by Komis, and we now have an integer array. Now what if we want to grab something out of those arrays? Let's grab the app word out of our string array that's have string. Word is equal to my array, and we open a square bracket and we pass it and index off the element we want. So I'm going to pass it one and close my square brackets, and this one is going to grab. And if I had put zero, it would grab, learn, if I had put to it, would grab development. And that's because a raise our zero indexed so the first element is actually found at address zero. The second element is founded. Address one. You always need to remember that when programming in C sharp or generally in any programming language, most arrays are zero indexed. If you recall from our previous lessons when we did four loops, I always started my interview I as equal to not and it just helps to keep that consistency throughout your C sharp programming code. Okay, Back to a raise. We have our word pulled out of our array. Let's print it. Console Dr. Right Line Wed. This should print out app. Click Go. And we have our app printed out for us. Now what if we have our Inter Jura ray and we want to discover how long this array is and why would we want to do that? Well, if I tried to access element number 10 inside of this array, I would get an exception. Ah, horrible error that crashed my program because there is no element 10. So it's always best to check how long are array is before we try and access something that might not be there. Interject array length is equal to our integer array darts length. And this returns an integer to tell us how many elements are in the array. Let's print it out in our console dot right line array length, dark to strength and click Go. It tells us we have four elements. 123 full. Now, don't forget If I want to access the fourth element, I would have square brackets with a three in it because 0123 three is actually the 4th 1 in terms of indexing. So that's how we get the length of our array out. I'm not going to go into other kinds off a raise or lists here. But just know that C. Sharp have has other ways off storing batches off variables together. One of them is list. So if you want to look that up on the Web, please go ahead. The other one is I innumerable, its capital I capital E. I'll just spend it out for you innumerable, I innumerable. So you feel free to go and look up that and also feel free to go and look up list. These are other ways off storing a raise, but the basic one we have now covered. 9. Methods in C#: see shop methods. If you recall, we've learned how to add and subtract variables or print them out to our console now, wouldn't it be a pain in the You know what? Toe? Have to rewrite that every time we had to numbers, we want to add Well, that's where methods come in in C sharp. When you define a method, you define a way of handling some data that you can repeat by simply calling the name off that method. This is best seen by example. So open up your C sharp pad dot com and let's find out what methods are all about. Let's make a method that adds to numbers and Prince the result. For us, the way we define a method in C Sharp is by first giving it what's called an access modifier. We'll start with public public means that other parts of our program can see and use this method if they wish. If we want to hide it away, we could type private or we could just have no access modifier. For now, let's have public. The next thing we have to define is doesn't method return something? Do we expect it to give us something back. If we don't expect anything back, we have to type void. That means nothing is returning. The next thing we add is the name off the method at numbers. Then we open some brackets and we pass in here What are called arguments. These air simply little bits of data that the method needs in order to execute its logic. Let's pass it to numbers. Inter Gia, my Numb and Inter Gia other numb. So this is receiving two numbers and you separate your arguments in a method with a comma. Then open your curly brace and close your curly brace. Now all of the code in here is going to execute when this method is called. So in the method we put all of our logic, let's have integer Result is equal to my numb plus Evernham. Then let's print it out console dot right line result dot to string. So this will add the numbers and print out the result. How do we call this method? Well, that is as simple as it gets. We simply say at numbers and when we open the brackets, it requests to numbers from us and this we have to give the method. It's not a choice. Let's give it nine and 10. Then let's say we have another two numbers. Well, it's add some more numbers. 108 199 and we could do this all day with whatever numbers we pleased. Can you see the power of methods? Now? I can run this entire method simply by calling one line and almost even just one word. So methods avoid the need to repeat code. If you find yourself repeating code in C Sharp, then stop typing. Step back and think about the construction of your program. Let's click. Go on this and we should have three results printed out for us. So that is the power off methods. It really cuts down on repetitive code. And a method performs a function on some kind off data that's either passed in or available from somewhere else. 10. Classes in C#: Now that you're familiar with variables and methods, we have to ask fundamental question. Where do we put them? C. Sharp in general likes you to organize your code inside off classes. So that's the first point of a class is organization off. Your code is another point of classes that we're going to learn as we work through our examples using the site c sharp pad dot com. So go there now and let's begin. Let's define a class public class and let's give it a name my car. Then open a curly brace and some way down close a curly brace. It's this class defined something called my car. Now inside of the class, we can define some properties or variables that belong to my car. We can have an integer off top speed. We can have a string off color. Now. All classes in C Sharp for the most part, need what's called a constructor, and this is a method that fires off whenever the class is inst enshi ated. I'll show you what in Stan Shih ation is in a little bit, but for now just know we generally need to have this method. This method is called a public and it has no return type, but we do not type void for it. We simply say Public my car and we can pass this some arguments or data if we like. Let's pass it over integer past, talk speed and a string past color. Open our curly brace and close it. Now let's pass over these past in items to the class level variables, so top speed is equal to past top speed. And what if we got hit? We have color is equal to past color. So when we instead she ate this class, we will be forced to pass in these items that then get spread or assigned to top speed and color. So this class so far has two variables and one method. We can continue adding as many variables and methods as we like, so let's have a method that allows us to access the top speed of the car public int. And when we type into were implying that when we call this method, we're going to pass back, a result which will be in the form often interject, get top speed opening close brackets because we're not passing any data, open your curly brace and close it. So what are we going to pass back in interview form? Well, we want to give back this top speed, and you may ask, Why don't we just simply go here and grab the top speed? Well, that's kind of a bad thing to do in C shop, because if you simply access this straight away, anybody else that's working on the same program might do the same thing, and one of you will modify this. The other will modify it what they want. And in the end, you get what's called spaghetti code. No one knows who's doing what. So you can think of methods like this as public channels that you must use in order to access variables in your class. You should never access variables directly, So in get top speed, we are going to return the top speed. Now let's have another method. Let's have a public for Wade paint car, and this is going to pass over a string with a lower case s called New Color. On this string, we're going to use to paint our car so color is equal to new color. Oops, I just tried to save it there. But it's a Web page. I always forget that. Okay, now I'm just going to copy this class because we're going to reuse it a little later. But just looking at this, we have now to find a class that holds the blueprints, if you will, for something called my car. Now, the word blueprint here is very, very important. That is basically the main function off a class. It's a blueprint that holds instructions that relate to creating a kind of object. So this one has the blueprints for a car. The car has a top speed. It has a color, you can get its top speed and you can paint the car. And this is something that you could apply to any car that you see on the street outside your window right now. So a class in and of itself doesn't actually do anything. What we have to do is called inst an She ate a class, and this is just a big fancy word. That means we're going to make an object using that blueprint. So this object is a variable in a way, like we've already experienced strings and inter jizz. This variable, however, has a type off my car. So let's type my car Toyota is equal to and you my car. And when we open up this bracket, it goes over to that public my car and asks you for some top speeds and colors. So let's pass it 89 and a color rent. Now we have created a new car called Toyota. Let's print out the top speed off this Toyota console dot right line to iota dot and now these methods that we defined earlier become available to us. Get top speed and let's convert that to a string inside of our consul right line. So this is going Teoh, open up a new object based on the blueprint off my car, it's going to be called Toyota. It'll assign a top speed of 89 on the color of red and then print out that top speed to us . But it's going to access the top speed using this access method here. So let's click Go and hopefully that all works out and it has now. What if we wanted to paint our car a different color? Well, let's copy all of this again and let's paste it. So we have our car class. We have Toyota, which is red, and then underneath that in our consul right line. Let's paint our car So Toyota dot paint car and this asks us for a color. Let's have a rain. Now. When I run this, our car color will change to green. But we don't know because we haven't printed out anything to the console. So that is the basic idea. Off classes, they hold blueprints that allow us to define new kinds off objects. Classes are one of the fundamental concepts off object oriented programming are. You may have heard that term before, but this is where it fundamentally comes from. We have blueprints on. We create items from those blueprints, and those items are generally called objects. I could have a Toyota object, a Ferrari one and Nissan one. Whatever we liked. So object oriented programming always thinks about the objects and not really about the classes. The classes do their thing, but we always focus on the Toyota that we made or the Ferrari that we made from that class . 11. Inheritance in C#: inheritance in C shop Again, this is one of those fundamental cornerstones you really have to know when you start programming. Inheritance allows us to write much less code and for our classes to inherit a bunch of properties and methods that we can expand upon. So let's find out what this is all about. By opening up c sharp pad dot com. Let's define a class and let's call this class vehicle public class vehicle. Let's give it some properties off integer, top speed and integer or not interject. Let's have a string off color. Then let's put an access method in there to grab out our top speed and color so public interject gets top speed and let's return our top speed. Then let's have a public string get color and of course, we're going to return our color. Finally, we need a constructor so public vehicle, and we will just have an empty constructor for this. Okay, so we have our vehicle. What if we wanted to make a truck class? Well, if you're thinking linearly, I can't even say that word linearly. Then you might copy all of this and create a new class called truck but that would be the long way of doing things still valid, but nonetheless, the long way. What we can do is create a class that inherits from vehicle public class truck put a colon in and start typing vehicle. Now, this class, I'm going to leave completely empty. Then what we're going to do is actually before we carry on, I'm going to set my top speed in vehicle equal to 90 and my color equal to read. Just so we've got some variables to play with. Okay, so we've created our truck that inherits from vehicle. You'll noticed. I've put no code whatsoever in here. Let's see what happens. And I'll just make a little space for us. When I type truck, my truck is equal to a new truck and close off my line. Now what I'm going to do is console dot right line my truck. I'm going to grab this truck of just created don't and your motus. It gives me get top speed and get color so we'll print out our color. So what inheritance does is it grabs a class and basically steals everything from it and repurpose is it for itself? So If I run that we have a truck that has a color off read, and that is inheritance. Inheritance is incredibly powerful. So if you think about inheritance like evolution, we all came from some kind of single cell organism. So that organism has, let's say, a property off cell. And we all have cells, no matter what species. So we inherited that sells properties as we went down through the tree off life. Now inheritance can be used multiple times. So if I copy these lines and in fact, I reload the page two make everything a little more clear. I'm going to paste them in, and what I'm going to do is remove to get speed and remove the top speed. I'm going to leave everything else in there, but just smash it all together. So we have a little more room to see what's going on. So I have my public class off truck now. What if I wanted to create some property inside off truck called String Size is equal to large. Well, I could create another class public class large truck, and this would inherit from truck and again, I don't have to put anything inside of here, then I could say large truck. My new truck is equal to a new large truck. Then let's do our console dot right line, my new truck dot and what we need is to get the size so inside off this public class truck . Let's have public string, get size. And of course, we will return the size. Don't worry if I'm going a little fast right now, I'm going to explain it all in a minute and then in our console dot right line my truck dot Get sighs. That should compile, but that's because I've put a semi colon rather than a semi colon at the end there. Okay, so what's going on here? Well, we've defined a vehicle with the color. Then we defined a truck and we added an extra property called large. And then we defined a class called large truck, which simply inherited from truck, which inherited from vehicle. So what I'm trying to show you here is we can inherit multiple times vehicle to truck tow, large truck, and when we do that, we pick up all of the properties and methods along the way such that I can create my new truck and I have access to all of those properties and methods, and if I click go, it will return the size, which is large. 12. Exceptions in C#: see shop exceptions. This doesn't mean probably What do you think it means? I'm not going to talk about exceptions in C Sharp as the English language defines it. Exceptions are a currencies that take place in our app. When the code does something that C sharp or dot net. Rather doesn't expect toe happen, and what happens is it throws an exception, which is basically an error. And this error doesn't just confine itself to its own space. It takes down the whole application with it. So obviously, this is something we need to be acutely aware off. We also need to know how to deal with it. Open up your C sharp pad dot com and let's find out how to deal with these pesky little exceptions. Let's start by defining a string called My Name is equal to Grant. Now let's try and convert that to an integer integer. Mine, um, is equal to confess. Two in 16 my name, and can you see what's going to happen here? Well, you cannot convert a word to wear number, so when I click go, we are going to get input. String was not in a correct format. That's a nice era, however, in an APP or any kind of software, that error would crash the entire program. These errors do not confined themselves to their own little space. They like to take down the whole system with, um, so there are two ways of handling these errors. Number one. Don't do it in the first place. Do not convert something like a string with the name in it to a number. Don't do it unless you really, really have to. So the best cure in this case is prevention. However, there are times when you will have to run code that could fail. So let's look at the solution for that. I'm going to paste this code back in. What we can do is surround the line that you expect to fail with something called a try catch statement. So we surround the line that we're interested in with Try. We open and close some curly braces to surround it, and then we type catch open brackets exception e open and close curly braces. Now, anything that gets attempted inside of try if it fails, the error or the exception is passed over to catch, and we can execute a bit of code based on the assumption that something has failed. So inside of our catch statement, let's send ourselves a developer message console right line called era. And then let's add on the exception message, which is contained in e dot message. So let's click. Go! This is going to fail again but will pass ourselves over a message. Input string was not in a correct format. Now, in the second case, our APP will not crash. We just get a message back as the developer. If you are going to present this to a user as a message, you would never say e dot message because input string, not correct format is very confusing for an end user. You would say, Hey, man, you put your name in when you should have put your age that's a bit silly, isn't it? You would give them a message that they can understand. So that's the basics of exceptions in C. Sharp. An exception is basically an error, and that is something that you can catch inside of a try catch statement when you expect ah , high likelihood, often era to take place 13. Summary of The C# Language Grasp The Basics Optional: Congratulations. You have finished this section on C. Sharp Basics. You now have all the tools you need to program basic C sharp programs and toe understand more complicated concepts. You've learned all about variables operators, conversions, comparisons between variables and loops that allow you to run code over and over again until a certain condition is met. You've also learned about a raise, which are lists of data methods that prescribe certain ways off dealing with data or manipulating variables. Classes that are a fundamental building block off object oriented programming along with inheritance, another fundamental building block. You also learned about exceptions, which is a fancy name for errors and how to deal with them inside of your programs so well done once again for completing this section.