Monday, October 8, 2012

C# for beginners Part 2


Hello Me!


Welcome to your second C# for beginners tutorial, in this tutorial you will learn the simple hello Me program after you've learned Hello World!.

Things you will need:

  1. Microsoft Visual Studio you can download the express edition for free from here free visual studio.
  2. Read Hello World! if you didn't read it yet From here.
  3. 10 minutes of your time.

Lets start:

Now that you ready to make your second program after installing Microsoft Visual Studio lets open it, now you will have to create a new project there is lots of project's types you can choose from but we will start with the simplest and very basic one of them the Console application.

File>>New>>Project
you will get a window similar to this (it may look different depending on the version of your Visual Studio)

New Project
Project Type

Choose Visual C# for the programming language then Console Application for the program type.
For the name type HelloMe or whatever you like your program name to be, But in here I will be using HelloMe as the project name so its a good practice to do the same so that names wont make you confused.

Now that you created the project its time to start writing some codes, This is how your code will look like when you open Program.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloMe
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
Our program is very simple all we want to do is to make our program ask for your name and Then welcomes you by saying Hello Yourname! and to make the program read anything from the Console we use Console.ReadLine(); command we will also use variables in this program.

NOTE: Variables are containers that hold our data temporary it has many types as we may need to store text or numbers or anything else
text = string       ex:"This is sentence"
number = int      ex: 1
point number = float or decimal       ex: 1.5 

Now lets make our program ask for your name and run it, In the Main type Console.WriteLine("What is your name?");
Then we need to get your name from the Console window so we will declare a variable called name of type string and put your name there first here is how to declare a variable of type string

string name;

its as easy as that but we need  to tell our program to put your name in this variable because now we reserved a string space in our memory and never used it and this is how to tell your program to read your name from the Console window and put it in the variable name.


string name = Console.ReadLine();


now all we need to do is to display the welcome message which is simple as we learned before how to display any text on the Console window but there is a little thing we will add there because we want the program to say Hello Your name so this is done like this


Console.WriteLine("Hello " + name + "!");


What the plus sign means is add the text in this variable from type string to that text in that position


NOTE: Always remember to  put space after hello because if you didnt it will be HelloYourname without spaces it doesnt matter how many spaces you put after the quotation mark it will be ignored. 


Now this is how your final code will look like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
                 Console.WriteLine("What is your name?");
                 string name = Console.ReadLine();
                 Console.WriteLine("Hello " + name + "!");
        }
    }
}

Now lets Run our program and see what we did press Ctrl+F5 to run your program

Console Result
The Result


Congratulations now you have done your second C# Program you are ready to learn more


NOTE: Click Download below to download sample project. More Download Links

Download From FileFactory!



Hope you enjoyed and benefited from this tutorial please feel free to comment or ask about anything and more tutorials will be added.
Thank you for reading and enjoy your day. 

No comments:

Post a Comment