In order to declare a variable in C#, first you put the Type of variable, and then the variable name.
Code: Select all
Type variableName;
Obviously I'm not going to list them all off, if you continue learning to program, then you will learn them when you need to.
int - a whole number(an integer, no fractions allowed)
float - a real number(can have a decimal point)
byte
short
long
The only difference that is important in the byte short int and long data types to us is the size of the numbers that they can hold.
So in order to declare an integer you need to make this statement
Code: Select all
int myIntegerVariable;
The .Net framework will automatically instantiate variables to their default values, for numbers it is 0
so in the above code myIntegerVariable would have a value of 0 even though I didn't assign 0 to it
So if I want to instantiate my integer variable to 45 there are multiple ways to do this that achieve the same result
Code: Select all
int num;
num = 45;
or
int num = 45;
Some other examples of creating variables
Code: Select all
bool myBooleanVariable = false;
float myFloatVariable = 3.14;
int myIntVariable = 420;
Methods are where the work gets done in programs. Methods are functions that either return a Data Type or void(no return value)
So to declare a function you first declare the return Type, then the name
Code: Select all
ReturnType MethodName()
{
...
}
Code: Select all
bool IAmLearningToProgram()
{
return true;
}
To make a useful method at this point we will need to add what are called parameters
Parameters are values that are passed into the method
You declare parameters in the method declaration, they are like variables that can be accessed inside of your method.
For example say we wanted to have a method that takes two integers as parameters and adds them together and doubles them and returns that you could do it this way
Code: Select all
int DoubleSum(int firstNum, int secondNum)
{
int sum = firstNum + secondNum;
return sum * 2;
}
Now declaring a method doesn't do much good unless you know how to call this method. I am going to assume that you created a Command Line project in Visual Studio. So you should have something like this:
Code: Select all
class Program
{
static void Main(string[] args)
{
}
}
Well when your program runs, it will automatically call the Main method, so from there you can call other methods if you wish.
Here is an example of a program that will ask the user for two numbers, add them together, and double that, then it will display the result
I will be using some methods from the Console class that will allow me to read and write to the Command Line display.
Console.ReadLine() will return a string when the user types into the command line and then presses enter
Console.WriteLine("....") will write a string to the Command Line
int.Parse("...") takes in a string and returns an integer
Console.ReadKey() just reads the next character the user enters, so it will wait until the user presses another key and then finish with the Main method, which ends the program
Code: Select all
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter two numbers");
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
int summedAndDoubled = DoubleSum(num1, num2);
Console.WriteLine(num1 + " and " + num2 + " added together and doubled = " + summedAndDoubled);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
static int DoubleSum(int firstNum, int secondNum)
{
int sum = firstNum + secondNum;
return sum * 2;
}
}
Some of you probably noticed that the method DoubleSum now has static in front of it. Don't pay attention to that right now, just know that the methods you define inside of the class Program need to be static in order to call them. So basically what happened in our main function is we read two numbers from the Command Line, passed those numbers as parameters into our DoubleSum method and stored the return value in the variable summedAndDoubled.
Hopefully you learned a little bit about some basic programming. I am not the best at organizing my thoughts sometimes, so if you need something explained better or are confused, don't be afraid to ask. Hopefully this was a decent tutorial, if not I'm sure I will end up rewriting it to make it more understandable in the future.