Welcome to Connors Programming Primer!
Here we will be talking about how to use c# to program, as well as talking about the language of c#.
Here is a list of what we will be going over in this primer:
- A brief overview of c#
- How to create a program
- How to create output
- How to declare and initialize variables
- Boolean
- Floating Point (Double, or Decimal)
- String
- Basic math operations
- Concatenating strings
A brief overview of c#
Pronounced "see-sharp" c# is a programing language created by Microsoft that is very similar to c++ and java. C# is an object-oriented programming language and is very efficent because the user has the ability to build off existing code instead of needing to repeatedly type out the same code. c# was designed to be used with Microsoft's .Net platform.
How to create a program
The two types of programs we will be focusing on in this primer will be console programs and Windows Form Programs. Both of these programs will be creatyed in Microdoft Visual Studio. Microsoft Visual Studio id an IDE or Integrated Development Enviroment, what this means is that you can type your code, debug the code you have written to find out where you have problems ion your cosde allowing you to fix them and finally Microsoft Visual Studio allows you to "drag and drop" items like text boxes or picture boxes into your windows form on screen speeding up the design process.
We will start with creating a Console Program:
- Begin by opening Microsoft Visual studio
- After opening Microsoft Visual Studio move your mouse to the top left of the screen and click "file" then click "new project" on the menu that appers after clicking file.
- After selecting "new project" select "Console Application" in the window that pops up. This menu is how you select what kind of program you want to make.
- Name the program before you create it, make sure the name is something you can use to help you keep your files organized. Also make sure you know where you save your projects in Visual Studio so that you do not have to go on a scavenger hunt to find your programs.
After you have opened your first Microsoft Visual Studio program you willse a window a bunch of writing in diffrent colors as well as may brackets. this is your program and right now it really wont do anything if you try and use it. To begin your journey towards creating your first program locate this string of text:
static void Main(string[] args)
{
}
now wht you want to do is type in the following lines of code:
Console.WriteLine("Hello World!");
Console.ReadLine();
What we have done is coded our first program! To try out your programmand check for errors click ont the green play arrow in the middle of the top toolbar. When your program opens it should Display "Hello World!" in the console.
Next up is how to crate a Windows Form Program:
- The beginnig process is mostly the same except when you open the new project menu you want to select "Windows Form Application" instead, this tells Visual Studio that we want to create a windows form program.
- Same as before, give your new program a proper name as well as save it in a place that you will remember so you do not have to hunt for it
Unlike last time instead of a bunch of code coming up when you create a new program this time there will simply be a blank window titled "Form1"
- Click on the menu item to the right of "file", entitled "view" then click on "toolbox" on the drop down menu.
- find the item called; "button" on the toolbox menu that you just brought up and drag it onto your windows form that is titled "Form1"
- double clicking the button you just created will bring you to the code editor.
- now look for the following chunk of code:
- This line of code is an uncomplete section of code that was created when you double clicked the button. What it does when it is completed is tells the program to do something when the button is clicked. What the program does when the button is clicked is up to you to fill in.
- now we want to type in this line of code:
private void button1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}
now try running the program, your program should do this when you click the button:
How to create output
Createing output can be as simple as using the computer to display text like we did with our first console program. Createing output can also be as comp[lex as outputing picturers, videos, and more.
Creating Console Output:
Our first console program is a perfect exaple of creating output. To understand how to create output we will take a look at our first program.
the line of code we used to get output on our program looked like this:
Taking a look at this line of code we see the three main parts:
- The first part, "Console" is considered an object in Visual Studio. It was the black widow that poped up and displayed our text.
- The next part ".Writeline" is known as a function. In Visual Studio functions are used to preform certain actions whithin your program
- The third and final part is our text that is displayed. This text is considerd the directions for ".WriteLine". Simply typing ".WriteLine" is like telling someone to "spell", our text in the brackets is needed to tell them what to spell. Functions in Visual Studio require brackets to work properly.
Creating Windows Output:
- Begin by creating a windows form project in Visual Studio.
- In the toolbox on the left side of your screen find a "label" and a "button" and cliclk and drag both onto your windows form.
- Select the button by clicking on it and check for the "properties" menu down in the bottom right.
- Find "text" in the property menu and change it to "press me"
- Find "name" in the property menu and change it to "btnPress" the name property needs to be one word with no spaces or punctuation, the name property is what you use to refer to the object.
- Find the name property for the label and change it to "lblOutput"
- Double clicking the button will bring you into lines of code that should look like this:
- After this line of code:
type in the following:
- when you run you program and click the button the label should change to what you typed in your code.
How to declare and initialize variables
Variables are items stored in ram whitin your program that are used to keep a value of something, they are placed in ram so that they can be acessed whenever they are needed. The variables we will talk about are: Boolean, Integers, Decimal, and string.
How to declare variables:
- Boolean:
Boolean blnVar;
- Integar:
Int32 intVar;
- Decimal:
Double dblvar;
- String:
String strVar;
How to initialize variables:
- Boolean:
blnVar = true;
- Integar:
intVar = 5;
- Decimal:
dblVar = 25.68;
- String:
strVar = "hello world"
Basic math operations
We will use our knowledge of decalreing variables to preform basic math operations.
- Use the following lines of code as a demonstration of how to add and subtract
- Just like adding and subtracting, multiplying and dividing also uses our knowledge of declareing variables. Multiplying and dividing are similar to adding and subtracting, one of the diffrences is the symbols you use. The following picture is a demonstration of how to Multiply and divide in c#:
Concatenating strings
String.Concat() is a Function in Variable Studio used to put words together
- Start by Writing "String strFirstName = "Your name here";
- Next Write out "String strLastName = "Your last name here";
- now we want to connect the two pices of text, to do so you want to use this code: