|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
The Common's Guide To Programming, C#
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Luck : 10pts
![]() |
![]() --- Introduction To 'The Common's' - 1.x --- "Before we get started." Licht Guide Purpose && Beginnings - 1.1 Before getting into the code, I would like to take a moment to explain this guide, it's contents, the purpose, and so on. The name, The Common's Guide To Programming, C#, is a name derived from the original guide I have posted here. It would be extended to something like, The Common Man's Guide To Programming In C#.NET, Version 2. Or wording similar to that. Let us call it "The Common's" for short. What I mean, roughly, by this, is that this guide aims to take the learning of programming, and make it easy. This means I take all the technical jargon out as much as possible, and put it into more common words. Often, things like programming books, and classes, use too much of this. They forget they are talking to new people who don't know all the terminology, and need everything explained on a more simplified basis. Not to say people are stupid, but they need to learn the basic concepts behind everything. This is the difference between learning, and memorizing. This guide's purpose is to bring aid to those who have not been properly taught programming, or not taught at all. Often, I find people who have tried, and given up. Usually, this is a result of someone who doesn't really know how to teach, being requested to as a teacher. This is not their fault, of course, it is simply something they haven't learned to do. I have spent a great deal of time teaching people how to program from the basics up, and I believe I have learned from it. After writing my first guide -- which seems to be very successful judging from praise in personal messaging, and reputation points -- in teaching, I believe I can write a more in-depth version now, and further my teaching method, bringing a more complete study to those who read it. Another aim of this is to bring great quality to illustration. Some times your vocabulary, in all it's might, cannot entirely explain a concept. Conceptual learning must use more then one part of the brain. It must use many parts together, as you would in any other environment, to truly learn something. In conclusion of this subsection, I have created this guide for all who are at the very beginning, or even those who don't know if they are all that interested yet. One thing I cannot provide in this guide, however, is the love of problem solving you must have to be a programmer. Programmers almost never get code right the first try. The joy is in all the toil you put into bringing it to life, then watching your creation live that life. Accredited Writer, Editors && Thanks - 1.2 "The Common's" has been written by Licht (Contact : lichtm@ymail.com .) Edited by Lucid.Mediums. Coedited by afzsom. Any errored/outdated information I would ask to be reported to me (Licht) by post, personal message, or email. I thank everyone who has contributed to this project. I appreciate all support in all forms from everyone who will. Makes all the hours spent typing up this guide worth it. I thank especially Overclock.net for not only hosting it, and being the reason for it's creation, but the community on Overclock that has enjoyed, and put to good use the guide before. This one I hope can be used just as well. If anyone has any questions or comments of any kind feel free to post (preferably.) Of course you are also free to use my email, personal messaging box, or other forms of communications. I am typically free 5pm-12am, and sometimes during 9am-12am. Please note I do have a busy life, and I may not respond quickly. But I will get to you typically within 1-2 days. Any more and you can contact me through a second means to assure I got your inquiry. Introduction to programming - 1.3 "C can shoot your foot, C++ makes it easier to do so. C# blows your foot off without asking." afzsom An introduction to programming is simply an explanation of the workings of code, and in this case, the code is C#. Obviously, a derivative of C language, C# can be considered a cross between Java, and C++. To explain it in another way, ++ is to a full step in code, as # is to a half step in music. Thus, C = 0, C# = 0.5, C++ = 1. So, C# is a half step above C, and a half step below C++. This is accurate to it's difficulty, as well, and capabilities. C# code’s speed is about the same as C++, the exception being that some memory operations are slower because it is managed code. Managed code refers to the way memory is managed in a language. C++ and other unmanaged languages deal with memory in-code, by the user. C# and other managed languages automatically manage memory with a “trash collector.” Another important component to the C#.Net language is the CLR. The CLR (Common Language Runtime) is what C#, and other .Net languages, are translated to before compilation. What this means is that when you click compile from your compiler’s menu, the compiler actually takes it, and converts it into the CLR. Then it takes the CLR and creates an executable with that. This allows all the languages to interact with each other without excessive complication. It also allows them to use common resources in the .Net Framework. ![]() The .Net Framework is a software framework designed to ease the load of programmers. The Framework basically provides many functions, tools, resources, and so on to improve application quality, and reduce development time. The .Net Framework is a huge part in modern applications as many programs (including games) rely on components found in it. C++.NET specifically is found in many games, and it’s runtime is required to run them. You will come to understand this fully as we progress in this lesson. Introduction to your tools - 1.4 "Registering, it makes Visual Studio quit whining." Licht This guide will assume you have downloaded, and installed, Visual Studio 2008 Express Edition, or own a copy of one of the commercial editions. Other tools such as calculator, and notepad, are required. That is all pre-installed on your computer, most likely, so you won’t need to stress about getting anything else at the moment. As I mentioned, I will link to the download pages for new tools. Visual Studio 2008 Express Edition is a free edition of the most popular compiler available. Made by Microsoft, this is, in my opinion, the ultimate tool for quickly, and effectively, developing applications for the Windows platform. If you don’t have it, you will need to go download your copy before continuing. The application is available at the following link; http://www.microsoft.com/express/vcsharp/ Be sure to get the English language version for optimal compatibility with the guide. Also remember (as if VS would let you forget) to register your copy. It is totally free, and quite frankly, it will shut Visual Studio up. --- The Language Itself, C# - 2.x --- Quickly, Why C#? - 2.1 The first question that may be asked is "Why C#?" C#, as mentioned before, is a cross between C++, and Java. This takes Java's flow, and flexibility, with C++'s power. Basically, this means C# is a language that will require very few lines to get a lot of work done. In the same time, we aren't sacrificing quality. C# is just as fast as C++ with the exception of memory operations. C# is also highly flexible, and is greatly picking up speed. One of the many things C# has been used in is XNA. A group of libraries and tools allowing indie games to really pick up. The XNA system supports making games for Xbox 360, Zune, and PC. You can even sell the 360 games through it's online marketplace. Being able to support game pads, powerful 3D engines, and more, XNA is a powerful system, with C# is at it's heart. C# is also picking up speed in general. It seems like every day, I see it more. It makes me glad I learned it as one of my first languages. Because of my ability to pick up on it, I base everything I can in it. I also endorse it to any beginners I see as my favorite, and recommended, language. C# is very well rounded, very logical, and fast. Something Very Basic, Operators - 2.2 "You can add and subtract... right?" Licht Before we go into detail it is important to teach you the basic of basics. I thought long and hard about this, and decided to go with operators. Operators are small parts of code. Without them nothing could work. They are even more basic than statements (going into that later.) The group of operators we will be exploring in this section are simply math operators I will be using in my demonstration. If you don’t know this I couldn’t go on into other sections, so they have their own little section. // Code:
//The first thing we will explore is the // operator. //This allows you to comment code. //Anything located behind these will not be executed, //and is only there for you to see. Code:
//Next on our list is +, which combines two values into one, temporarily. //It only lasts as long as it is used in that line of code. //The following are examples of + usage. MyVariable = 6 + 7 //Makes MyVariable equal to the sum of 6 + 7. //If I translate this into words it would look like this. MyVariable is equal to the sum of 6 and 7. ++ Code:
//The next operator we will explore is ++, an incrementor. //It will add 1 to the variable it is preceded by. MyVariable = 6 MyVariable++ //MyVariable originally started at 6, //and one was added to it and stored in memory. //To put it in words. MyVariable is valued at 6. Add 1 to MyVariable += Code:
//Next we try using +=. This one is relative like the last one. //+= takes the value on its left side and increases it by the value on the right. //Basically it is the same as the ++ operator but you can specify- //how much you want to increase the value. MyVariable = 6 MyVariable += 4 //MyVariable is now equal to 10. Because we added 4 to its value of 6. -, -=, -- Code:
//Just the same as the + group of operators there is the – group. //I will provide a brief example showing that this group is the same working, //but for subtraction. MyVariable = 10 // We start with 10. MyVariable-- //We use an incremental function. //Just like the ++ this means we are using a value of only 1. //This time the increment gos down instead of up. MyVariable -= 10 - 1 //Here we are taking our value, and dropped it by (10 - 1). //Since we started with 10, decremented it by 1, //Lowered it by (10 - 1) (which equals 9), //We now have MyVariable set to 0. Code:
//Next up is the divide command. //Same syntax. MyVariable = 6 / 3 //MyVariable equals 2 now. Since 6 divided by 3 is equal to 2. You can also use many others. For example * is the operator for multiply. And can be used like *= as well. Although, you cannot use it as an incrementor. % Code:
// I should mention, however, the % operator. //This operator is used to return the remainder when 2 variables are divided. //To put this to use I will show this example of usage. MyVariable = 7 % 3 //This would make MyVariable equal to two. //This is because 7 / 3 = 2 with 1 remainder. -- Variables - 3.x -- Introduction To Variables - 3.1 Variables, one of the most important pieces of software code. Without variables even the operators would be useless, for operators exist only to manipulate values, which are stored in variables. In this section I start with the most basic how-to. Then I move on to specifics on data types, and how to use them, as well as manipulate them. Before I get to that part, I will further explain what a variable is. In computers a variable is roughly the same as in your math class (or at least some of them are.) They store single instances of information. Depending on the data type you use limitations are always placed upon what and how much of that you can store. Unlike in your math class where variables store infinite amounts of data. In the following section we will ignore specific data types all together in favor of simplifying the process, and easing into it. So do note that this is not the actual way things are written in the beginning of this section. Declaring && Using Variables - 3.2 Before variables can be used they must be declared. This may seem annoying and redundant at first but like all things in computers, it's only logic. Declaring a variable is basically telling the CPU to allocate some space in the RAM. Once this has been allocated you can now reference that place in the RAM. Using this, your next step is to reference it, and tell the computer to assign it a value. This is called initialization. It is key to make sure your application initializes it's variables before it tries to read from them. If you try to read from an uninitialized variable, you will receive an error, and the program will be unable to continue. Let's move on to some code. Note, again, that this may or may not be 100% accurate to syntax as it is just to teach you the very basics I have just discussed above. Code:
//First we will declare our variable, adding it to the RAM. VariableType MyVariable; //The first statement is a variable type. //In place of an actual variable type i have inserted the placeholder "VariableType." //Following it is the name of our variable. //In this example i chose to simply put "MyVariable" as our variable name. //You can name variables pretty much anything you want, //however they must start with a letter and not be too long. //Also, you cannot assign them names already taken. //The same as you wouldn't name all your children Marcy, and for the same reason. Code:
VariableType MyVariable = MyValue; //First we type in the data type, referenced here as VariableType for easy reading. //Next we type in our variable's name. In this example i have named it MyVariable. //We follow this by an equal sign and that by MyValue. Code:
VariableType MyVariable = 6 //First we declare the value, dedicating it to memory. //In the same line i have also set it to 6. //Please note VariableType is just a placeholder until we learn actual data types. VariableType MyVariable2; //Note no value is yet set. MyVariable2 = 6 + MyVariable; //MyVariable now is equal to 12. //To have read this plainly, it would have been read something like : //Set MyVariable2 to the sum of 6 and MyVariable (which equals 6.) Code:
VariableType MyVariable = 10000 * 2 / 5; //Just an example to illustrate you can use anything you normally could. //If you are wondering that adds up to 4000. VariableType MyVar, MyVar2, MyVar3; //Here i have initialized 3 variables in a single line. //When you initialize like this all the variables must be the same data type. //Note that you can also merge lines into single lines by separating lines with semicolon. VariableType MyVar; VariableType2 MyVar2; //Note this has no performance advantage and is bad for organisation. //It is only used in select cases for organisation. //The compiler will merge everything before compiling anyways. Data Types && Uses - 3.3 We are starting to piece things together now. Assuming you are still following and haven't cursed your keyboard or ran off, you are doing well. By now you should be getting the concepts of programming and be able to quickly understand and absorb this section. Data types are basically how the data in the RAM is written and read. To explain this, we have many different types. The differences between them pends which data type you are comparing to another. For example, if you compared numbers to numbers the differences would be : decimal points (yes/no), max and minimum values, negative values (yes/no), processing speed and more. Below i will explain the data types and their uses. --- Very common data types : bool : True or False Bool stands for Boolean. This data type is extremely simple, it can only be in 3 states. The first state is True. The second state is False. The third state is null. (Null is any value defined in the memory, but not yet set.) Booleans are good for things like check boxes where it can only either be checked or unchecked. Nothing is wasted this way from using a complicated data type that consumes more memory or processing power then you need to. Before the next value i would like to take a second to introduce the concept of a floating point. A floating point is a value that in actuality, doesn't exist. A computer "comprehends" it by representing it with calculations. For example a computer cannot store a decimal point without a floating point, and it does so in the same manner you could in algebra. 5 / 10 would be equal to 0.5. In this way computers can calculate decimals and other things that they normally couldn't, it's done by storing math. double : –5.0x10-324 ~ 1.7x10308 A double is the larger floating point in C#. Use of this data type is usually to store things that require you to use decimal values. Often it is used because integers will truncate (erase) to make the number 0 decimal points. If they didn't it would be impossible to do any calculations that may involve decimals with integers, as they don't support decimals. int16 : 0 ~ 255 This data type is a 16bit integer. It is the smallest integer available for use in C#.NET. Holding very little data at a minimum or 0 and a maximum of 255. Even though it holds little data, it is useful for being an extremely fast data type when processing. So if you are doing lots of math but with numbers not exceeding 255 or delving below 0, 16 bit integers are the way to go. int32 : -2,147,483,647 ~ 2,147,483,647 This is a larger integer. This time it is 32bits, slightly slower, but holds much more data. It has a minimum value of -2,147,483,647 and a maximum of 2,147,483,647. When you are using numbers that will exceed 255 or go negative, not contain decimal points, and still need to process them fast, this is the data type to use. It is probably the most common data type you will be using. single : –1.5x10-45 ~ 3.4x1038 A single is another floating point, the same as the double but it is slightly smaller. Same as the other data types, this means it will consume less resources. Use it when your values fall within it's range and you need something with decimals (or it's other traits) while still being faster then a double. string : Unicode Characters A string is another one on our list of common data types. This one holds unicode characters in an array. (Arrays are explained later.) Basically it is a group of char variables. Char variables are explained later in this section. Unicode is a standard for text data on a computer. It contains over 100,000 characters and is a standard text encoding used today. You will see unicode mentioned quite a bit. However, don't let that make you think there aren't other standards out there. --- Less common data types : decimal 1.0x10-28 ~ 7.9x1028 Decimals are another member of the floating point family. Following the same rules as the others. The major difference is this time there is no support for negative values. And of course, the range is different. char : Unicode Character A char value is a single character. "a" or "b" are char values. This isn't to be confused with a string which is a list (in order) of char values put together. int64 –9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 This is another installation in the integer series. This time it is 64 bits long. As you can see above this gives us an extremely large number to play with. But is slower then the 32bit variety. Use 64bit integers only when you plan to actually use all that numerical storage power. Remember to ask yourself these questions : "Will my data really exceed the upper or lower boundaries of a 32bit integer?", "Is there no way to decrease the number to fit into a 32bit integer?", and "Is my code flawed, or is the number really supposed to be this large?" Generally, make sure you have a good reason before using this data type. sbyte -128 ~ 127 An sbyte is a data type ranging from -128 to 127. You might as yourself why (you might have done that a few times.) But this is because a byte has a maximum storage capacity of 255. So a byte stores 0 - 255. And an sbyte, which obeys the same rule, stores 255... minus half of 255. The numbering goes from 0 to 255. But it is read as the value, minus 127. The reason for this is to provide a fast and small data type that supports negatives as well. Before going further i would like to explain what the u in front of int means when referring to these special integers. This character stands for "unsigned." Sign referring to the minus or plus sign. Or in other words, they lack a positive or negative value and are assumed to be positive. uint16 0 ~ 65,535 This is yet another in the integer line of data types. This one is named uint. The u basically tells you it does not posses the ability to store negatives. However it consumes the same amount of space as a regular 16 bit integer. This gives you quite a bit more numerical storage if you don't count negatives. If you don't need to use negative values this allows you to store lots of data in a small amount of room and be very fast as well. uint32 0 ~ 4,294,967,295 Another in the line of the uint series. This obeys the same scheme as the 16bit uint. No negatives, but a much higher maximum value. uint64 0 ~ 18,446,744,073,709,551,615 This is the last value we will examine. This is the uint64. Or 64bit uint. It has the highest maximum of any of the data types. To clarify this number. This is over 18 quintillions (yes, that is an actual numerical term.) Lets put it this way. It gos hundred, thousand, million, billion, trillion, quadrillion, quintillion. Then take 18 of those and add a couple hundred quadrillions and you have a (relatively) close number to the maximum amount of numerical value that can be packed into this data type. As per the other numbers it cannot store negatives, is slower then the smaller integers and uses more memory to store. Because i am sure you are wondering. The limits on how much data can be stored in these values is determined by how high binary can go in the amount of bytes each type uses. For example, 16bit integers contain 1 byte of binary data in storage. So the maximum value that can be stored in a single byte determines their capacity. Since the absolute max that can be stored in a byte is 255, 16bit integers have a limit of that, 255.
__________________
Last edited by Licht : 05-30-09 at 11:31 AM |
|||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
Luck : 10pts
![]() |
Data Type Conversion - 3.4
First, let my explain why we need data type conversions. As we learned, not all data can store all kinds of variables. Some store decimals, negatives, or larger/smaller numbers. Some store a character, or list of them. The same as some can be processed mathematically and others cannot. Some can be processed with other certain data types, and others can't. Before going further i must provide a VERY light explanation of functions. Functions are used similar to variables. You type them in and the computer reads their value in place of their name when processing. The key difference is that a function will be followed by parenthesis and code is executed in order to find it's value. Within these parenthesis are what is called arguments. In this section all functions will have a single argument that is the value that the function is to process. In sections beyond this functions may have more then one argument separated by comas. In data type conversion we have two main ways of converting. Implicit and explicit. Implicit means it can be converted without the need of a function to do so. Explicit means you will need to call a function to convert it. Implicit data conversion requires no explanation on how to use. Simply enter the value as if it was the same data type. To avoid confusion or unnecessary memorization you can just take these rules. All integers are implicitly converted, all floating points are implicitly converted, booleans implicity convert to nothing, and chars implicitly convert to strings. Explicit requires a bit of code. We need to run the data type conversion function with our variable as the argument. Let's use an example. Say we want to display a message box to detail how many cookies the program calculated could be made with so much flour. We would use the function MessageBox.Show. This function's argument for what it displays only takes a string. But because we did the math with an integer, we need to display an integer. This can be done by using our Convert.ToString function. We will also be using the & symbol. This symbol, when used in strings, appends one string to the end of another. Code:
integer cookiecount = 0;
//Code gos here calculating how many cookies can be made.
//Let us say for curiosities' sake that the outcome was 12.
//Before continuing i would like to mention quotation marks.
//This should be pretty self explanatory but putting text in quotations
//is just like putting numbers down in mathematics.
//It uses that as the data (a string to be precise) and
//is unchangeable after compilation.
MessageBox.Show("Cookies : " & Convert.ToString(cookiecount));
//To convert this to every-day language it would read something like...
//Open a message box, display the string "Cookies : "
//Also display after that string, display the string equivalent of the value of cookies.
Arrays - 3.5 Arrays are one of the most important concepts in programming. Arrays allow you to code things that would be otherwise impossible. To describe an array i would tell you it is a list. Basically it is a list of variables of the same data type and name. It is accessible in ways you could not access variables normally, such as mathematically. This may not make sense now but you'll understand by the time we are done with this section. Next is an example of any array. Notice the changes in how you initialize them. Code:
int[] MyVar = new Int[5]; //Note that if you type int, it is the same as typing int32. //First we call the data type as an integer. //Followed by this is a set of []. These declare it is an array. //Next we include our equal operator. //Then the statement "new" i used. //Following this we include the data type again, //and the size of the array in []s. ![]() ^Example of organization of an array. Code:
MyVar[0] = 56; MyVar[1] = 59; MyVar[2] = 10; //The 3 above examples detail the use of an array. //Here we have specific positions 0-2 and set their values. //So now position 0 is equal to 56, 1 to 59, and 2 to 10. //Notice how the numbering starts at zero. //This means that even though you specified the size 5, it holds 6.
Last edited by Licht : 06-03-09 at 04:52 PM |
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
Luck : 10pts
![]() |
This is a place holder. Please do not post until i have completed the guide. total sum of placeholders should amount to 10 posts (to be safe.) Spare posts will be deleted by moderator. All posts that are between the placeholders will need to be deleted. Thank you.
3/10
__________________
|
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
Luck : 10pts
![]() |
This is a place holder. Please do not post until i have completed the guide. total sum of placeholders should amount to 10 posts (to be safe.) Spare posts will be deleted by moderator. All posts that are between the placeholders will need to be deleted. Thank you.
4/10
__________________
|
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||||
|
Luck : 10pts
![]() |
This is a place holder. Please do not post until i have completed the guide. total sum of placeholders should amount to 10 posts (to be safe.) Spare posts will be deleted by moderator. All posts that are between the placeholders will need to be deleted. Thank you.
5/10
__________________
|
|||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
Luck : 10pts
![]() |
This is a place holder. Please do not post until i have completed the guide. total sum of placeholders should amount to 10 posts (to be safe.) Spare posts will be deleted by moderator. All posts that are between the placeholders will need to be deleted. Thank you.
6/10
__________________
|
|||||||||||||
|
|
|
|
#7 (permalink) | |||||||||||||
|
Luck : 10pts
![]() |
This is a place holder. Please do not post until i have completed the guide. total sum of placeholders should amount to 10 posts (to be safe.) Spare posts will be deleted by moderator. All posts that are between the placeholders will need to be deleted. Thank you.
7/10
__________________
|
|||||||||||||
|
|
|
|
#8 (permalink) | |||||||||||||
|
Luck : 10pts
![]() |
This is a place holder. Please do not post until i have completed the guide. total sum of placeholders should amount to 10 posts (to be safe.) Spare posts will be deleted by moderator. All posts that are between the placeholders will need to be deleted. Thank you.
8/10
__________________
|
|||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||||
|
Luck : 10pts
![]() |
This is a place holder. Please do not post until i have completed the guide. total sum of placeholders should amount to 10 posts (to be safe.) Spare posts will be deleted by moderator. All posts that are between the placeholders will need to be deleted. Thank you.
9/10
__________________
|
|||||||||||||
|
|
|
|
#10 (permalink) | |||||||||||||
|
Luck : 10pts
![]() |
This is a place holder. Please do not post until i have completed the guide. total sum of placeholders should amount to 10 posts (to be safe.) Spare posts will be deleted by moderator. All posts that are between the placeholders will need to be deleted. Thank you.
10/10
__________________
Last edited by Licht : 06-03-09 at 04:53 PM |
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|