Programing Primer for C#

Basic Overview of the Language

Microsoft developed C# intending for it to be a modern, general purpose, object oriented programming language. When working with C# you will be developing code using Microsoft Visual Studio which is an IDE (Integrated Development Environment) This basically allows you to do a variety of different things such as write code that tells the program what to do, debug the code for errors, and create forms (buttons, picture boxes, etc...) by dragging items on the screen.

The Language C# was developed during the .Net Framework by Anders Hejlsberg and his team.

There are many reasons C# is such a commonly used programming language:


Creating a C# Program (Console Application)

Creating your very first C# program is really quite simple! I will walk you through step by step in creating a program that will display any message of your choosing.

To begin first you must:

You will now see a small bit of code; however the program will not do anything unless you add to it. To create a working program you must type the following:

Once you have created your code to run it hit the start button on the menu tab. If there are errors in your code the program will not run but instead it will try and locate what went wrong. Remember to always include a semi-colon (;) at the end of each line of code. Also capitol letters do matter so make sure to use them properly.

If you would like to create a slightly more complicated program you can have multiple codes of Console.WriteLine to simulate having a conversation, etc...

For Example:

Creating a C# Program (Form Application)

This is very similar to a console application however it allows you to add buttons, picture boxes, labels, etc... To start the application follow the steps below:

Now you will be at the Windows Form Design. To start open up the toolbox (View, Toolbox in the menu bar) then find and drag a button onto your form. Once you have a button double click it. The following code will show up:

private void button1_Click(object sender, EventArgs e)

{

}

Then in between the two brackets proceed to type MessageBox.Show("Again whatever message you would like it to display");

This program when debugged should allow the user to click on the button which then would display a message. This opens you up to be able to code a much larger variety of different programs.

For example to code a program that asked you a true or false question you would need to drag another button and 1 label onto the form design. You would then proceed to rename one button True and the other False. The message that the message box displays would need to be changed to either correct or incorrect and you could rename the label to ask the question.


Creating Output

Computers output you different information all the time without you consciously knowing. It can be basic output such as lines of text or it can be more complicated output such as solving mathematical algorithms or equations.

In your first console application you created you typed the following code: Console.WriteLine("Random Text"); by doing this you are telling the program to output you the text that is typed between the quotation marks.

Lets break down the code even more:

Windows Form Applications also use Output in order to display text, pictures, tables and more.

In a Windows Form Application you always change the properties of your objects you place in the form design. For example if I placed a label in my form design, then I should rename it with the abbreviation lbl followed by what the labels purpose is. For this purpose i would rename it lblOutput. Based on what you learned on how to create a Windows Form Application you should be able to make it so that when you press the button it displays the label.

In a Windows Form Application to output text for the label we just created the code would be as follows: lblOutput.Text = "Random Text";


Declaring and Initializing Variables

Variables are a key part of programming. A variable is an item that is stored in the programs RAM (Random Access Memory) for later use. It is important to make notes in your code showing where you are declaring and initializing variables.

There are 4 basic variables that we commonly use in our C# codes:

Boolean Variables: Boolean is only used in true or false situations. For example answering true or false questions or in a video game the variable goalIn could be used to determine whether the goal went in or not.

To declare a Boolean variable the code would look something like this: Boolean blnVar;

bln is the abbreviation for Boolean and Var for variable.

To initialize the variable (to set its value) the code would look something like this: blnVar = false;


Integer Variables: An integer variable is any variable that is a whole number. For example 19 or -3. This is useful when you know that you will not receive a decimal value such as for the year of publication on books.

To Declare: Int32 intVar;

Initializing: intVar = 1995;


Floating Point Variables (double or decimal): The most common floating point value is double which is used in programs for things such as cash registers and anything that deals with simple math. However decimals are not always accurate when using a computer as computers run on a binary system instead of a decimal system.

To Declare: double dblVar;

Initializing: dblVar 9.25;


String Variables: String variables are words instead of numbers so are therefore the most simple.

To Declare: String strVar;

Initializing: strVar "This is a string variable";


How to Get Input

Example Code:

Console.WriteLine("What year were you born?");

Int intBirthYear;

intBirthYear = Console.ReadLine();

Console.WriteLine("What is your name?");

String strName;

strName = Console.ReadLine();


In the example code the program is requiring user input twice. Once in line 3 and again in line 6. In lines 2 and 4 we are declaring the variables and 3 and 6 we are setting them to Console.ReadLine() which allows the user of the program to type the desired answer. However if the users answer is not an integer or a string the code will not work.

Getting input on a Console Application and a Form Application are very similar and the code is much the same. The only major difference is the code may be grouped differently.


Concatenating Strings

Concatenating strings is often referred to as adding strings however this is not correct. It is not possible to add two separate strings however since strings are words it is possible to group two together. The code for concatenating strings is String.Concat();

In the bracket is where you would put two or more string variables that you wish to be concatenated each separated by a comma.


Basic Math Operations

There are numerous math operations that can be completed using Microsoft Visual Studio. The most basic math operations are as follows:

Calculators are among the most simple forms of displaying code for basic math operations in C#.


Code Examples

Works Cited

"C#." - For Dummies. N.p., n.d. Web. 26 Sept. 2014.

"Hello World -- Your First Program (C# Programming Guide)." Hello World -- Your First Program (C# Programming Guide). N.p., n.d. Web. 26 Sept. 2014.

"Over 30 Million Stock Images, Vectors, Footage and Audio Clips." 123RF Stock Photos. N.p., n.d. Web. 26 Sept. 2014.

"Tutorials Point - Simply Easy Learning." C# Environment. N.p., n.d. Web. 26 Sept. 2014.

Unit 1 Textbook