Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C# Visual Studio Setup – Part 0

What tutorial series would be complete without the quintessential hello world? You might also be wondering why “Part 0”? well back in the 80s if you were to pickup a c++ or objective-c book they always had a chapter 0, this is to pay homage to those who wrote before us. Its also an indication of a chapter that could be skipped if you’ve dabbled in this topic before and have Visual Studio installed already. While, I could certainly install visual studio and do screenshots for the entire process, Microsoft has documented the process very well. Here is the install link to Visual Studio 2017, the community edition will be just fine for us. Go install that, I’ll wait here…

Now that we got that installed, lets create a new project. File -> New -> Project.. will show you this screen:

Lets select console application from the list, name the project whatever you’d like. The console application will give us a playground to test some basic variables (covered in part 1) and see some output. At this point you should be looking at a screen similar to this

I’ve highlighted the major areas of the editor:

  • Light Blue – Menu
  • Red – Programming area, this is where you’ll write code
  • Orange – This is where you’ll see console output, errors,warnings
    about the code you’ve written.
  • Yellow – Solution Explorer is the file structure that makes up your
    entire project. There are boiler plate things in here that
    makeup whatever type of project you’re creating.
  • Pink – Properties window will show the properties of visual items, so if we had a button, we could set its color or name. We’re doing
    a console app so this isn’t as applicable.

So in the red area copy and paste this Code replacing the class Program that your project created.

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace helloBlog
{ 
    class Program
    {
        static void Main(string[] args)
        {
            string messageText = "Hello Blog";
            Console.WriteLine(messageText);
            Console.ReadLine();
        }
    }
}

Starting at the top of the program lets talk about each line and what its doing.

Using System; The using directive is telling your program what outside packages it needs to operate. Thankfully languages have evolved and other programmers have bundled up useful code and shortcuts to make our life easier. In this particular example the system directive allows us to use the console to read and write values. As seen further down in the code.

namespace helloBlog { } While this topic will be covered in more detail, think of namespaces right now as groups. It allows you to organize your code and group it with other similar code. It also allows you to have the same method names in two different places without conflict.

class Program  A class is similar to a blueprint, it holds generic information about what an object should be, while allowing you to change its values, use its actions to do things. Think of a car, if we made a car class, ever car has a color, model, make, maybe ever car can drive. The class would represent these values and actions. So if you wanted to build a new car, you could use the car class to make that happen. Again this topic will be touched on in more detail later.

static void Main(string[] args) This is the main entry point for your program, we’ll break down exactly how methods are constructed in a later tutorial. Understand for now that your code will be written here for the next few tutorials. Whatever code you write between the curly braces is what will execute when pushing the play button.

string messageText = “Hello Blog”;  In this line we’re creating a new string variable called messageText and assigning the value of “Hello Blog”. Note that every statement we write ends with a semicolon.

Console.WriteLine(messageText); Remember way back when we discussed the using system directive? The Console.WriteLine( ) statement wouldn’t be possible without that using directive. So the console object has a function called WriteLine() which takes an argument, and argument is just a value provided by you!

So we call the WriteLine and pass in a value of messageText and voila the console displays our message.

Console.ReadLine(); This statement is here so the console window doesn’t disappear automatically.

If you run the program using the green play button, you should see the following output. Well done, you’ve just created your very first program.

Challenge: At the end of every tutorial I offer a challenge to my readers. So your challenge here is to modify the messageText variable to output your first and last name. Post your screenshots here in the comment section!

Bonus Challenge: Output your first name and last name on separate lines.

Happy coding,

Corey



This post first appeared on Diaonic - Coding, Tutorials, And Life Hacks, please read the originial post: here

Share the post

C# Visual Studio Setup – Part 0

×

Subscribe to Diaonic - Coding, Tutorials, And Life Hacks

Get updates delivered right to your inbox!

Thank you for your subscription

×