C# Workshop – Types & Variables

Standard

Types & Variables

C# is what’s known as a strongly-typed language where every Variable has a type as does every expression that has a value, there are many types available, some of which will be covered here.
The first thing is to define the basic structure of the code by entering the following in dotnetfiddle.net in the main window:

using System;

public class Demo
{
	public static void Main()
	{
		// Code
	}
}

The first way to use types is like algebra in mathematics, so enter the following below // Code:

int a = 5;
int b = 2;
Console.WriteLine(a + b);

int is the type which is an Integer which can contain large or small whole numbers, a is the name of the variable and = is used to assign the value to the type, you can try changing the operator to * for multiply, for subtraction and finally / for division and check the answer it gives when use Run in dotnetfiddle.net
In mathematics, it of course it is possible to get answers that aren’t whole numbers so will need to use a type that allows this so replace the code from before with the following:

double a = 5;
double b = 2;
Console.WriteLine(a / b);

double is a type that does support decimal places in numbers so need to remember to use the correct type and you can use Run to see the correct answer.

Variables aren’t just used to do mathematics, they can be used to store anything for use in the application either one or many times and can be changed with new values.
You can try this with string which represents a series of characters by entering in to dotnetfiddle.net the following:

using System;

public class Demo
{
	public static void Main()
	{
		string message = "Hello World";
		Console.WriteLine(message);
	}
}

Text and Numbers relate to things you encounter day to day and can relate to but there are also Types that are a bit more specific to programming, enter in to dotnetfiddle.net the following example:

using System;

public class Demo
{	
	public static void Main()
	{
		bool a = true;
		bool b = true;
		Console.WriteLine(a && b);
	}
}

You can then run this in dotnetfiddle.net, bool is a type to store Boolean values which can either be true or false, in this example it will output true and uses another operator && which means “and” so if both a and b are true the output will be true, it’s also possible to use || which means “or” to produce the same output as if a or b are true, change the Console.WriteLine line to the following:

Console.WriteLine(!(a && b));

When run in dotnetfiddle.net this will output the opposite value as ! means not so if something is true it becomes false and if it’s false it becomes true.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s