|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
[FAQ] A Beginners Guide to Programming, C#.
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Programmer
|
Before beginning this is for the absolute beginners. Because we all know at first coding can be a little daunting. Most people (although i picked it up right off the bat) have issues just grasping the basic if statement system. So this is a quick overview to get them started up on that and programming. In this guide i will be using C# (pronounced C Sharp), a very powerful language with a managed environment. Just as fast as C++ with the exception of speed lost due to it being a managed environment but you can get 10 times the work done in the same amount of time (not literally of course but you get the point.) Keywords such as if are colorized, pay attention to these words as i have done this for a reason.
First off, download and install Microsoft Visual Studio 2008 Express Edition C#. This is a free compiler made by Microsoft for C#.Net. Accompanied will be the .Net framework either version 2.0 or 3.5 (pending your OS.) I wouldn't recommend downloading the MSDN Library since its only a small section of information that is rarely used and adds a large amount of time to the setup. Additionally i want to explain the color coating. Light blue is for data types such as strings, integers, or doubles. Blue (regular) is used for statements, such as : if, switch, else. Light green is used for various other things like project names. Part 1 : Hello World! Applications If you are using Visual Studio now, heres instructions to create a form with a button to execute our code. Open Visual Studio and click create new project. Then select windows forms application. Once thats done open your toolbox and drag a button onto the form. Right click and click properties if you want do edit anything about the button, such as text. Double click the button to bring up the button click event (an event is a section of code executed when something happens.) To achieve this we call the MessageBox class. And the Show function. For arguments we enter the string "Hello World!" Take note that all strings that are not variables must be enclosed in double quotes ("".) Code:
MessageBox.Show("Hello World!");
Part 2 : Variables, Data Types, && Specifics Above we used a string as an argument. A string is one of the 13 data types in C#. The most common data types used are : strings, integers (sizes 8 - 64 bits), doubles, decimals, booleans and floats. The following is a list of these, and what types of information they can store.
Code:
String mystring = "This is the value of the string, mystring is the name of the variable."; Int32 myint = 378; //This is a 32bit integer Double mydouble = 378.0001; //This is a double, unlike the integer it can store decimals, it is slower then an integer. It is a member of the floating point family. Decimal mydecimal = 1000.873; //This is a Decimal Boolean mybool = False; //This is a boolean, it can be only true or false. It accepts no other value. Single myfloat = 2008.29; //This is a floating point. mydouble = 64.3 * myint; /*This sets mydouble to 64 times the value of myint. In short, mydouble is now equal to 24305.4. We used a double because it can store decimal places necessary for this value. The / followed by a * surrounding this text is a way to comment out text of multiple lines.*/ Code:
Int32 myint; //Initializes the variable, note it was done without setting the variable this time to show you this can be done as well. myint = 10; myint *= 10; //Now myint is equal to 100 Part 3 : The Statements - if, switch, and else. This is where usually people have issues understanding when i try to teach them how to program. I often start here but decided not to for this FAQ. Lets start with if statements. These statements execute code in brackets only if their requirements are true. The following example only executes code if a boolean is equal to true. Code:
Boolean mybool = true;
if (mybool == true) //= is used when settings values, == is used when checking values.
{
MessageBox.Show("This message box is showing because the boolean mybool is equal to true, and thus the requirements of this if statement are met.");
}
Code:
Boolean mybool = false;
if (mybool == true)
{
MessageBox.Show("mybool is equal to true");
}
else
{
MessageBox.Show("mybool is equal to false");
}
Code:
int32 myint = 2;
switch(myint) //This specifies we are talking about the value myint.
{
case(0) :
MessageBox.Show("myint is equal to 0");
break;
case(1) : MessageBox.Show("we can also format it this way"); break;
case(2) : MessageBox.Show("this is what will execute since myint is equal to 2"); break;
}
__________________
Last edited by Licht : 04-30-08 at 06:29 PM. |
|||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
Programmer
|
Part 4 : Data Type Conversion, Explicit and Implicit Often you have the need to have 1 type of data be another type. For example you need to display an integer in a string. To do this you would use an explicit data type conversion. In this example you will see a message box displaying the value of an integer using explicit data type conversion. Code:
int myint = 75;
MessageBox.Show("myint = " + Convert.ToString(myint));
I'm sure you are wondering why you need to do this. To make it simple, the data is stored in the RAM in different formats. Just like jpg images and gif images are saved in different formats. You can't open a gif image in a program that doesn't support it, same as you can't reference a string as a number. Part 5 : Arrays, Matrices, and More This is where it gets interesting, an array is basically a list. Using coordinates you can find what data is located in a position on that list. For example lets imagine the array as a series of dots. Each dot contains a value. It would look something like this. ![]() Dot 1 is equal to 10. Dot 2 is equal to 12. And so on. This can be unimaginably useful. For example we have the user tell us a series of muffins 0-8 and each muffin he inputs a string telling us what kind it is. Then in code we enter it into out application. It would look like this. Code:
string[] myarray = new string[6]; myarray[0] = "blueberry"; myarray[1] = "plain"; myarray[2] = "raspberry"; myarray[3] = "chocolate"; myarray[4] = "mixed berry"; myarray[5] = "all berries"; Ok next we will cover Matrices. A matrix is a 2 dimensional array. We have all seen those tables where you have the answer to multiplication questions. You know 1 horizontal coordinate and a vertical one. Using these two coordinates you could find the value of one number times another. This is exactly what a matrix is. For example i will use this diagram, with coordinates marked. ![]() To initialize and use a matrix we code like this : Code:
int32[,] mymatrix = new int32[2,2]; mymatrix[0,0] = 0; mymatrix[0,1] = 0; mymatrix[0,2] = 0; mymatrix[0,0] = 0; mymatrix[1,0] = 0; //Note i didn't set all the coordinates to show you don't have to. Part 6 : Loops A loop is a section of code that is repeated until a given value is met. There are a few times of loops out there but we will focus on 2 specifically. For and while. Loops are one of the most useful aspects of programming. They can be used for anything from searching for prime numbers to checking how many friends you have on a list. Lets begin with the while statement. Lets say you want to repeat some code until a value reaches a certain point, and keep track of how many times it took. One way to do this is to use a while statement. Code:
int Fnumber = 0;
int Snumber = 2;
while (Snumber < 10)
{
Fnumber += 1;
Snumber += 1;
}
MessageBox.Show("Finished, it took " + Convert.ToString(Fnumber) + "repetitions to make the second number equal to 10.");
Code:
for(int i = 0; Snumber < 10; i++)
{
Snumber += 1;
}
MessageBox.Show("Task complete, it took" + Convert.ToString(i) + "repetitions to complete.");
Code:
for(int i = 0; i < 10; i++)
{
MessageBox.Show("Every iteration (cycle, repetition) i is increased by 1 (this is done by ++ which effectively means += 1) until it reaches 10. Since we started at 0 it takes exactly 10 cycles to get there.")
//The above message box is shown 10 times before the loop ends.
}
Part 7 : Threading (Explanation Only) I want to start out by saying i'm only covering the basics of threading. This is a difficult topic for most people and honestly doesn't belong in a beginner's guide but it is a crucial part of programming i think no one should do without learning for any reason. A thread, to the CPU, is a sequence of code to be executed. Each application by default has a single thread and windows divides up how much time each thread gets on the CPU based on its priority level and how active the CPU is verses the needs of the application. For example if you use two threads, this effectively allows you to split the workload between them and make your program work on 2 cores at once, or 4 threads can use quad cores. Note that it takes up CPU cycles to control threading, so if your not talking about major CPU tasks it may actually sacrifice performance to use multiple threads regardless of how many cores are on the machine. Note i won't be providing how to multi-thread, or manage the main thread. None of that belongs in a beginners guide, probably shouldn't have even mentioned it. At a later time i may write an independent FAQ concerning threads. Until that time this is all i will provide.
Last edited by Licht : 04-30-08 at 06:30 PM. |
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
Programmer
|
Part 8 : Knowledge Application Now that we've covered most of it all, its time to look at an application and use it to further your understanding of the previously studied concepts. We will cover a few basic assignments with full explanation. The first is how to return the remainder of 2 numbers when divided. The second is use of data type conversions for reasons more then just converting the data type. The third is adding 2 matrices. Code:
int 1int = 5; int 2int = 9; int 3int; 3int = 2int % 1int; Code:
Double mydouble = 1.337;
int myinteger = 3;
while (true) //The while statement with the value true as an argument signals an infinite loop, broken only by the break statement.
{
if (mydouble == myinteger / 3)
{
break;
}
else
{
mydouble = Convert.ToDouble(Convert.ToInt32(mydouble));
}
}
Code:
int position = 0;
int[,] 1matrix = new matrix[2,2];
int[,] 2matrix = new matrix[2,2];
for(int i = 0; i < 3; i++)
{
for(int o = 0; o < 3; o++)
{
1matrix[(i - 1),(o - 1)] += 2matrix[(i - 1),(o - 1)];
}
}
Part 9 : Optimization Optimization is a crucial part of programming, especially with loops. Optimization is all about minimizing CPU cycles. For example division is said to take many CPU cycles, this is because as we do the CPU tests each number to see if it can divide a number over and over until it finds the answer. This consumes much power. So 6 / 2 is 3 (3 CPU cycles) times slower approximately then 6 * 0.5 (about 1 cycle.) Division is one of the floating point operations. All floating point operations are very slow and hence you want to avoid them. As i said before floating points themselves are the single, double, and decimal data types. The fastest data type is an integer, so you are going to want to use them whenever possible instead of floating points. Another point i want to make here is AMD's K8 architecture is heavily geared towards floating points over Intel's architectures. Just an interesting bit of info. Part 10 : Finishing Touches Thanks for reading the guide, hope it helps allot of people who want to learn how to program. I spent my entire day on this ( ?) and i think i did a decent job, any suggestions for additions or questions post or PM me pending your judgment on which it deserves. Programming is a truly great field to partake in. It is allot of fun and challenging mentally to boot (and sometimes physically, oh the longs nights spent over a hot mouse... then you pass out during the 37th debug run.) Heres some helpful links i use often, enjoy.http://msdn.microsoft.com/en-us/library/default.aspx http://www.wikipedia.org/ http://forums.microsoft.com/msdn/default.aspx?siteid=1 http://www.microsoft.com/express/ http://msdn.microsoft.com/en-us/vcsharp/default.aspx
__________________
Last edited by Licht : 04-30-08 at 06:31 PM. |
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||
|
WaterCooler
|
Awesome man
![]()
__________________
|
|||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||
|
News Fiend
|
Hey Licht, Thank you Thank you Thank you Thank you Thank you Thank you
__________________Rep+ I just started looking for something just like THIS! Now FINISH IT!! ![]()
|
||||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
Programmer
|
All finished, enjoy.
__________________
Last edited by Licht : 04-29-08 at 11:51 PM. |
|||||||||||||
|
|
|
|
#7 (permalink) | |||||||||||||
|
Cthulhu Fhtagn!
Join Date: Jun 2007
Location: West Palm Beach, FL
Posts: 4,048
Rep: 548
![]() ![]() ![]() ![]() ![]() ![]() Unique Rep: 332
Trader Rating: 8
|
+
![]()
__________________
Watercooling Loop: MCP655 Vario -> Black Ice GTX360 -> D-Tek FuZion V1 -> MCW-60 -> MicroRes Loop Cost: $395.03
|
|||||||||||||
|
|
|
|
#8 (permalink) | |||||||||||||
|
PC Gamer
|
Great job licht!
|
|||||||||||||
|
|
|
|
|
#9 (permalink) | ||||||||||||
|
Overclocker in Training
|
Sweet, man... i've been wanting to learn how to program for ages, and this will help loads, esp since i can get the stuff off of Dreamspark...
__________________
Any Windows OS automatically crashes every minute. That way, they can promise in the next more stability.
|
||||||||||||
|
|
|
|
|
#10 (permalink) | |||||||||||||
|
Overclocker in Training
|
Awesome!
__________________
When in doubt, overclock ![]()
|
|||||||||||||
|
|
|