Sunday, October 25, 2015

Your First Project & Application

Your First Project & Application

Introduction:

In this chapter you'll be creating your first project in C# as well your first application. The very basics of a C# programming interface is covered here as well.

First of all start by starting Visual Studio up and then go to File -> New -> Project.

I will only cover Console Applications in these tutorials, because that's the best way to actually learn the language, instead of jumping into Windows Forms and event handling then you won't have to struggle with it later, so as of now start out with a Console Application.

I will be calling my application "My First C-Sharp Application". You can call yours whatever you want, the name doesn't really matter.

At the right side you'll see your solution. A solution can be consiting of more than one project, but that's not important now as we'll only work with one project at a time. When you start to use more projects then when you start to create real applications.

Solution Explorer:
[Image: o94Jq.png]

You can view all the files and folders in your project. The .cs files is the C-sharp Codefiles which is the files you want to code in. If you want to add new files you can right click your project and choose "Add".

We won't look into this now, so don't sweat it.

At your left you should see something like this:
[Image: sFYxj.png]

That is the place you want to produce your code. You might not understand what these things are now, so let's break it down.

We'll start with the first few lines showed which are the following:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

The using keyword basically tells that we want to use a namespace. The "words" you see afterwards ex. System; is the namespace we want to use.
You may want to use one or more namespaces and that's why you can use multiple namespaces.

The ; (semicolon) is used to finish a statement. If you come from VB then it'll be hard to be used to use that all the time, because in VB you won't have to use it at all, but the beauty of using it is that you do not have to have seperate lines of all your code.
This:
Code:
using System;using System.Collections.Generic;using System.Linq;
using System.Text;

Is actually equal to:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

Now I'm not saying you should do that, because you should still keep in mind that your code should be readable by yourself and others who's maybe going to use your code or is your team-partner.

The next thing we see is the keyword namespace.
Basically it will give a sequence of classes, structs and other things a name which you can use to access them. Basically namespaces are useful to group your classes, so it's easier to navigate through your classes.

In our case the default namespace is My_First_C_Sharp_Application for me, but for you it will be whatever your project was called. If you make a new folder then to create a new namespace (Will use the folder name) then a good idea is to right click the folder and then add your classes through there, because they'll be given the default namespace + the folder name as a namespace.

We could use this as a reference:
Code:
System.Linq

System would be our default namespace and Linq would be a folder under the System project.

Now this is not exactly how it works and it was just an example.

Now we'll see this:
Code:
class Program

I won't really explain a lot about that, because you'll learn about classes later on, but basically a class is a sequence of variables and methods which you have to access through that class.

Example on MyMethod:
Code:
class Myclass
{
    public static void MyMethod()
    {
    }
}

Cannot just be accesed like:
Code:
MyMethod();

We would have to do it like:
Code:
Myclass.MyMethod();

And if it was in another namespace we would have to type the namespace as well or do as the first thing I explain use the using keyword to define the namespace. Then we wouldn't need to type the namespace, but could just do as above.
Code:
Mynamespace.Myclass.MyMethod();

The next thing we'll see is this:
Code:
static void Main(string[] args)

That is a method. I won't really explain this what it does, but what I'll explain here is that the Main method is first default entry point, but it's possible to actually execute some code before it gets to that using constructors, but I won't be teaching any of that now as it's not relevant and I do not expect you do understand it, if you do not have any experience at all, so just remember the Main method as being the entry point of the application. That's where we want to do out code for the next tutorials, so do not focus on any of the codes shown here as of now.

At last there is brackets { & } which you may question yourself what does.
Brackets in C# is used to declare a scope. Basically every scope in C# has some sort of code. The places where you usually are using scopes would be namespaces, classes, methods, properties & loops.

And explanation of the scopes for the different types.
Namespace scope:
A scope for a namespace can hold different classes, structs, interfaces, delegates, enums and such things, but not variables, methods etc.

Class scope:
A class scope can hold variables, properties, delegates, methods, other classes, interfaces, structs, enums and such things.

Method scope:
A method scope can hold variables, operations (ex. i += 10), loops and codes for execution. A method cannot hold any other types can variables thought.

Property scope:
A property scope will consist of one or two scopes. A scope for get and a scope for set. The get has to return a value which is the same type as the property and the set will basically set the value of the property (Usually linked to another variable.). The two scopes is actually the same as a method scope. You'll learn more about this when we get to properties.

Loop scopes:
A loop scope has to be declared within a method scope as it's just looping through a sequence of codes that are getting executed. That means the loop scope can hold the same things as a method scope.

Now let's get started with some writing to the console. We have to use the Console class which is a part of the System namespace, but since we have already declared the System namespace (As I explained earlier) then we can just call Console. and we want to call the WriteLine() method, however there is Write() as well. WriteLine() will write to the console and create a new line afterwards, which Write() won't. You can try type a string in the parameters. You start a string by using " and ending it by using " as well.
Ex:
Code:
"Hello Hack Forums!"

You have to do that as the parameter. A parameter is the variables or what you can say that is passed into a method when called.

Ex.
Code:
static void MyMethod(string parameter1, string parameter2)
{
}

You'd call it like:
Code:
MyMethod("parameter1", "parameter2");

This is how we'd use WriteLine().
Code:
Console.WriteLine("Hello Hack Forums!");

Now I don't really explain where to put this now and it may confuse you, but remember what I explained about scopes?

Where do you think a code for executions should be placed? ;)

That's correct in the Main Method.


Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace My_First_C_Sharp_Application
{
    class Program
    {
  static void Main(string[] args)
  {
    Console.WriteLine("Hello Hack Forums!);
  }
    }
}

To build you press F6, you can find your application in your project folder in either bin\debug or bin\release. However if you wish to build and run you simply debug by pressing F5. Debugging is a good tool, because if there is any exceptions thrown (errors) then it will jump into your code and tell you where the problem is and tell you exactly what the problem is, but you can also set breakpoints. Breakpoints can be used to check how your program actually works and you can use it to check when variables change and what their values are. It's a good thing to check for errors and mistakes.

To set a break point you just click at the left side of your code editor and it will create a red dot, which marks the breakpoint. You can click on that to remove it again as well.

Your program will pause/stop when it reaches that break point and then you can follow your code line by line. You can have more than one break point.

To follow your program you'll have to press F10 for each line. If you have finished checking a breakpoint you can press F5 and it will either run the program regular (If there is no more breakpoints) or jump to the next break point.

Now try to debug.

What happened? The program closed.

That's weird, isn't it?
Not really, because the program has no more code to execute. We can stop it from doing that by creating an infinite loop or just call the read methods from the console class and wait for an input.

ReadKey() will wait for a key input, where ReadLine() will wait for enter to be pressed. ReadLine() can also be used to give a string a value, because it returns a string.

Code:
Console.ReadLine();

Final code:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace My_First_C_Sharp_Application
{
    class Program
    {
  static void Main(string[] args)
  {
    Console.WriteLine("Hello Hack Forums!");
    Console.ReadLine();
  }
    }
}

Congratulations you have made your first C# application.

Downloads/Links:
My First C-Sharp Application Download (Source)

Original 

No comments:

Post a Comment