c# – An overview of commonly used variables

Previously we looked at why variables are categorized as either “value types”, or “reference types” (primarily for memory reasons).

Now lets take a look at these variables in action, starting with the string variable.

String variables

We already encountered string variables when we created the “message” string variable in our hello world program.

You can also Concatenate existing string variables to form new string variables:

using System;
class ConcatenateStrings
{
	static void Main()
	{
		string firstname = "James" ;
		string lastname = "Bond" ;
		string fullname = firstname + " " + lastname;
		Console.WriteLine ("My name is " + lastname + ", " + fullname );

                string country = "uk";
                string CounterUpperCase = country.ToUpper();
                Console.WriteLine(fullname + " lives in the " + CounterUpperCase );
	}
}

Line 8 – here we are using a non-static method called ToUpper(), which is a member of the string class. We will cover more about static and non-static methods later, but for now it just gives early sight of how methods are used.

Boolean

This variable can only have one of 2 values, “true” or “false”. Booleans are commonly used to store the result of something we check.
o

using System;
class BooleanExample
{
	static void Main()
	{
		bool AboolVariable = true;
		bool AnotherBoolVariable = false;
		Console.WriteLine("First boolean's value is:  " + AboolVariable);
		Console.WriteLine("Second boolean's value is:  " + AnotherBoolVariable);

		int x = 1;
		int y = 2;
		int z = 1;

		Console.WriteLine ("x is equal to:" + x);
		Console.WriteLine ("y is equal to:" + y);
		Console.WriteLine ("z is equal to:" + z);

		bool FirstComparison = (x == y);  // True
		Console.WriteLine ("Is x equal to y: " + FirstComparison);

		bool SecondComparison = (x != y);
		Console.WriteLine ("Is x not equal to y: " + SecondComparison);

		bool ThirdComparison = (x == z);
		Console.WriteLine("Is x equal to z: " + ThirdComparison);

		bool FourthComparison = (x < y);
		Console.WriteLine("Is x equal to y: " + FourthComparison);

		bool FifthComparison = (x =>= z);
		Console.WriteLine("Is x greater than or equal to z: " + FifthComparison);
	}
}

In the above program we have taken our first look at comparison operators, we will cover more about comparison operators later.

Arrays

An array is essentially a list of values grouped together into list.

using System;
class ArrayExample
{
    static void Main()
    {
        string[] fruits = new string[5];
        fruits [0] = "a";
        fruits [1] = "e";
        fruits [2] = "i";
        fruits [3] = "o";
        fruits [4] = "u";

        for (int i = 0; i < fruits.Length; i++)
        Console.WriteLine (vowels[i]);
    }
}

Here is another way to declare an array:

using System;
class FruitsArray
{
	static void Main()
	{
		string[] fruits = {"apple","bananas","oranges","mangoes"};

		Console.WriteLine (fruits[0]); // this prints "apple"
		Console.WriteLine (fruits[1]); // this prints "banana"
		Console.WriteLine (fruits[2]); // this prints "oranges"
	}
}

Integers

Here are some examples of integer, integers are often used for doing maths:

using System;
class LotsOfNumbers
{
	static void Main()
	{

		int FirstNumber = 12;
		int SecondNumber = 3;
		int SumOfBothNumbers = FirstNumber + SecondNumber ;

		Console.WriteLine("The first integer is:   " + FirstNumber);
		Console.WriteLine("The second integer is:   " + SecondNumber);
		Console.WriteLine("Adding up both numbers gives:   " + SumOfBothNumbers);
		Console.WriteLine(FirstNumber + " minus " + SecondNumber + " gives:   " + (FirstNumber - SecondNumber) );
		Console.WriteLine("Multiplying both numbers gives:   " + (FirstNumber * SecondNumber) );
		Console.WriteLine("Dividing " + FirstNumber + " by " + SecondNumber + " gives:   " + (FirstNumber / SecondNumber) );
	}
}