First C# projects

Creation of a few simple C# programs

Hello World!

The usual simplest of the simple C# Programs. Following the steps of how to create a New Project for a C# Console Application.

  1. Open Visual Studio
  2. File -> New Project
  3. C# Console Application
    Image
  4. Change the name to Hello World, the Solution name will automatically change to match it.
  5. Click OK to create an empty C# application
    Image
  6. In order to print “Hello World” to the console, we must use Console.WriteLine(“”);
    Following the WriteLine with a ReadLine will allow us to actual view what was put on the screen by having the program wait for the user to enter something.
    Image
  7. Hitting Start or using the Shortcut F5, a Command Prompt will appear with the words Hello World! and will not exit until the user inputs something like hitting enter.
    06222014NewProjectHelloWorldStart
Simple Variables

This next program will just be an example using variables to compute values and display them to the screen.

Creation of a new project for variables example:

06222014variablesExample

In the following code two variables are created, x and y, which are declared as integers. After the variables are declared the x is defined as 3 and y is defined as x’s value plus 7.

 

06222014variablesExampleCode

Once the code is ran, you can see that the value of y is 10.

 

 

06222014variablesExampleCodeRan

There is a special variable type called var that assigns the correct type of variable based upon what you define it as.

06222014varKeyword

As you can see in the following code, the var keyword is used two different variables, one with an intenger and one with a string.

var x = 1;
var y = "Hello World";

The code also displays how a variable that had it’s type declared can have the value defined as soon as it’s declared or later in the code.

int x;
int y = 7;
x = 3 + y;

06222014varKeywordCode

When the code runs, the output shows the value of y and the two var keyword variables.

06222014varKeywordCodeRan

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s