Let's start by opening your old "Hello World!" project or make a new one with the exact same code. Replace the text "Hello World!" with a variable.
Start with declaring the variable "helloWorld" by typing "string helloWorld = "Hello World!";" above "Console.Write("Hello World!");" and replace "Console.Write("Hello World!");" with "Console.Write(helloWorld);". If you run your application by pressing F5 on your keyboard now it should show the exact same thing as when you started it the last time.
String is a special data type in C# namely a basic data type. String tells your variable with the name "helloWorld" that it will hold multiple letters and/or signs or in our case "Hello World!". In C# there are in total 15 basic data types and are shown in blue in Visual Studio:
- bool : true or false
- sbyte: -128 to 127.
- byte : 0 to 255
- char : One letter or sign
- short : -32,768 to 32,767
- ushort : 0 to 65,535
- int : -2,147,483,648 to 2,147,483,647
- uint ; : 0 to 4,294,967,295
- long : -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- ulong : 0 to 18,446,744,073,709,551,615
- decimal : ±1.0 × 10^−28 to ±7.9 × 10^28 - numbers with comma
- float; : ±1.5 × 10^−45 to ±3.4 × 10^38 - numbers with a comma
- double : ±5.0 × 10^−324 to ±1.7 × 10^308 - numbers with comma
- string : Multiple letters and/or signs
- object : This can be anything. It's data without a meaning.
You will probably have to practice a lot in order to memorise all the basic data types and you should, because they're really important. The most used basic data types are: string, object, int and bool, so you should start with those.
Back to the coding part. You can show numbers on your screen by using int. Replace "string helloWorld = "Hello World!";" with "int randomNumber = 25;" and replace "Console.Write(helloWorld);" with "Console.Write(randomNumber);". Run your application and you should see the number "25" on your screen.
You can do the same with booleans (bool for short). Replace "int randomNumber = 25;" with "bool randomBoolean = true;" and replace "Console.Write(randomNumber);" with "Console.Write(randomBoolean);". Run your application and you should see "True" on your screen.
Until now you've only used variables with basic data types, but there are also normal data types which you can add yourself or are available in the .NET Framework. Replace "bool randomBoolean = true;" with "Random myRandomVariable = new Random();" and replace "Console.Write(randomBoolean);" with "Console.Write(myRandomVariable.Next());".
Random is a class in the .NET Framework. "new Random()" returns data with the data type Random (convenient huh?). Your "myRandomVariable" variable now has the information to call the "Next" method of the "Random" class. The "Next" method returns a random number with the basic data type int (point with your cursor on "Next"). You should see a random number on your screen when you start your application.
Note: You should not take it easy on naming your variables. You should give variables a correct description based on the data it will hold in order for you and others to easily understand your (large) projects.
Source Code Variables Project
>> Basic Calculations
dude really good job with these!
ReplyDeleteThanks, that really gave me a good feeling about myself.
ReplyDelete