In this lecture, we'll write
our first console application or console app using MonoDevelop.
And even if you plan to use visual studio as your ID in this course,
you should still watch this video lecture
because there are some important ideas in this lecture as well.
So, before we actually get started writing some code,
you should do an in-video quiz to make sure you understand
the importance of software and you should do another
in-video quiz to show how you feel about what language you want to use as you program.
Now, there are a variety of useful languages in which to program.
And in this particular course,
we're going to use C# which is a high level programming language for writing our code,
both for console apps and for our unity script.
So, how does all this work?
Well, we start in our IDE and we type some stuff,
and that's stuff that we type is C# code and it's often called source code because it's
the source of the code that
the computer actually executes when we're finally running our program.
So, what happens is we start with our C# program or our C# source code,
and we run the compiler on it.
And the compiler is a piece of software that converts
that C# source code into what's called Common Intermediate Language.
After we have our Common Intermediate Language when it is
time to actually run the code somewhere,
the dot net Common Language Runtime takes that CIL or Common Intermediate Language
and turns it into the actual machine instructions
that will run on the chip on that particular computer it's running on.
So, we typed some stuff in C# and we turned it into CIL and then,
we turned it into machine instructions when it actually gets run. OK.
Let's go to MonoDevelop and actually see what one
of those console app things actually looks like.
I've started up MonoDevelop and here's our opening screen.
You won't have a list of previous projects
you've worked on in MonoDevelop at least until you've programmed for a little while.
So, we're going to create a new solution.
So, up here under solutions, we'll click new.
We are building a console app or a console application and in MonoDevelop,
that's called a console project.
So, make sure that's selected and then click the next button down here on the right.
And now, we select our project name and I'll call mine
'first console app' and click Create.
And so, it creates this template for a console app.
And we'll go through the pieces here as we go along.
So, this first line of code is called a using directive.
So, this System right here is called a namespace
and people will write a bunch of code
that we can use so we don't have to write it ourselves.
Tons of that code comes in C#.
And when we get to Unity,
tons of that code comes in Unity.
So, this using directive that says let me use code from a different namespace lets us
use code that has been provided as part of
the C# programming language or as part of the Unity engine.
We actually by default get our own namespace when we
have created this project and our namespace is the same name as the project name.
Now, we come to this thing called the class Main Class.
And we'll actually talk about classes in the next module,
but this is a way to collect together the functionality in our console app.
So, I'm going to,
above the class, I'm going to put three slices.
What I'm doing here is I am providing what's called a documentation comment.
Documentation comments are in XML and we can actually use other tools to generate
documentation that other programmers
can use when they want to use the code that we've written.
And being able to automatically generate documentation for the code you
write is pretty useful if you distribute that code for others to use.
I'm just going to call this first console app lecture code.
And so, I'll now move to this big long line
here that says public static void main string square brackets args.
This thing called Main is a method.
So, methods are chunks of code that we call from somewhere
to execute the code that is between the curly braces for that method.
And so, if I were to run this code right now,
I would end up executing this Console.WriteLine Hello World.
And why not show you what that does?
I've Control F5.
It says Hello World. Just like that.
Now, it turns out in this particular case,
this main method gets called by the operating system when we run the code,
but you're going to write lots and lots of methods that you call yourself.
Let's add a documentation comment above this as well and I will
just say, prints a message.
And even though this automatically filled out
information about this args thing right there,
we'll talk about the specific terminology for that when it matters.
I'm just going to clean that up a little bit and finally,
I'm going to get rid of this.
I'm just going to make it print a blank line.
So, what I'm doing here is I am calling a write line method
on the console class and write line lets us provide output to the command prompt window.
So at first, I'm going to F8.
You can also select Build - Build All,
if you forget what the hot key is for building.
But I'll just press F8 and you can see here I get a message that says build successful.
Now, you should go do an in-video quiz to
see if you understand what really happens when I press F8.
Of course, the answer is the compiler generates that
Common Intermediate Language that we talked about before we get to actually doing coding.
Alright. Let's actually now run this.
So, I will Control F5,
but of course if you forget how that works,
you can select Run.
And you would definitely want 'start without debugging,' which is Control F5.
So, I Control F5 and there you can see the output is just a blank line.
This 'press any key to continue' comes by
default when we run Console apps from within MonoDevelop.
OK. So, I just hit 'enter.'
I press the any key and here we are back in our environment.
And you should go do another in-video quiz to tell
me that you understand what happens when we Control F5.
OK. And it runs the Common Intermediate Language that was generated.
I'm going to say 'print supportive message.'
And I will Console.WriteLine.
And now, I'll put the message I want to print out.
Remember by default, we got that 'hello world' message,
but I'm going to say "hi, noob!"
Like so.
I'll Control F5 and I ran the code,
it printed out that message and we can press any key to continue.
Now, you'll notice that I did not F8 to build the code before I Control F5 to run it,
that's because it's easier to just Control F5
and the IDE builds the code along the way before running it.
Now, you might wonder,
how do you know how to use this WriteLine method in the console class.
And the way we regularly discover how to use methods is by reading the documentation.
So, let's go take a look at the Microsoft documentation and here it is.
So, in the upper right,
we can search on something.
So, you could search on console class for example.
But look, it even actually gives us a pop up that says Hubble Console.WriteLine.
So, let's just` grab that, I'll click it.
And the documentation gives us a number of results for a console write line.
Let's follow this one,
which is in the console class.
And here, for a particular class,
we get a number of pieces of information.
We get information about properties and we will talk
about properties in detail in the next module.
We get information about methods,
the methods that we can call on the console class.
And so, we can make our console beep and so on.
But write line, it's all alphabetical, so that's good news.
But write line is down here.
And the information that we get tells us both the name of the method and
any information we're supposed to provide between those two parentheses.
So, there's a write line where we don't provide
anything and we used that to print a blank line.
There are other versions of write line with different data types that we provide.
And we will talk about data types in the next lesson in this module.
And down here is the one we're actually calling to print out the string.
So, the documentation says,
"it writes the specified string value followed
by the current line terminator," so it moves to the next line,
"to the standard output stream" which for us is the command prompt window.
So when we call this method, we put Console.WriteLine,
open parenthesis, we provide a string between
double quotes and a close parenthesis and a semicolon.
The thing that we pass into the method when we call it is called an argument.
That's why up here,
this autogenerated comment talks about argument.
The only other piece of this puzzle is there's an S over here on the left.
And that means that this is a static method and all that really tells us,
all we have to know at this point,
is that means we put the name of the class console dot.
We'll see other ways to call methods in the next lesson in this module,
but for now for our first program,
we just call this with the class name dot method
name and whatever arguments we want to provide to that method.
This one, we're calling the version of WriteLine that takes in a string.
This one, we're calling the version of WriteLine that doesn't take any argument.
And that's it. This is our first console application in MonoDevelop.
I have provided a copy of the chapter of the book that talks
about console apps and the structure of
a console app like the using directives and so on.
So, you should feel free to download the PDF copy
of that chapter for MonoDevelop if you're using
MonoDevelop and Visual Studio if you're using Visual Studio
to get additional discussion about all these pieces of the code.
To recap, you can use mono development,
if you choose to, to write your console apps and your Unity scripts in this course.
You can also use Visual Studio,
but I'm going to use MonoDevelop to record all the video lectures for the course.
I will, however, provide the code for each lecture and
the exercises and the peer reviews as both MonoDevelop and Visual Studio.
So, whatever IDE you've pick, it should work fine.
Providing that code though,
I can't give you a zip file as an attachment to
a particular lecturer because I'm not allowed to attach zip files as lecture resources.
So, each lesson will have a reading that
include all the code for the lectures within that lesson.
I know that's a little less convenient than we'd like,
but that's, you know,
the way it has to work on the Coursera platform.
So, check the reading in each lesson to get
all the lecture code that goes with the lectures in that particular lesson.