Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Values in C# are classified into different that specify what the values can be used for.
Declaring variables using the var
keyword only works if you initialize the variable with a value, that is, assign a value to it immediately. If you don't, you'll get a syntax error saying "Implicitly-typed variables must be initialized".
var number;
var greeting;
System.Console.WriteLine(number);
System.Console.WriteLine(greeting);
System.Console.WriteLine(number + 2);
Program.cs(5,13): error CS0818: Implicitly-typed variables must be initialized [/home/treehouse/workspace/workspace.csproj]
Program.cs(6,13): error CS0818: Implicitly-typed variables must be initialized [/home/treehouse/workspace/workspace.csproj]
Values in C# are classified into different types that specify what the values can be used for. You can view the type of a value by calling the GetType()
method on it. We haven't covered methods yet, but for now all you need to know is that a method is a collection of code which you can call to get it to do something. You call a method by putting a dot (that is, a period character), the method name, and a pair of parentheses after a value.
For whole numbers, the GetType()
method gives a type of System.Int32
. For strings of text surrounded by double quotes, GetType()
gives a type of System.String
.
System.Console.WriteLine(1.GetType()); // Prints "System.Int32"
System.Console.WriteLine(2.GetType()); // Prints "System.Int32"
System.Console.WriteLine(3.GetType()); // Prints "System.Int32"
System.Console.WriteLine("hello".GetType()); // Prints "System.String"
System.Console.WriteLine("hi".GetType()); // Prints "System.String"
C# has dozens of types built-in.
- Integers, as we mentioned, have a type of
System.Int32
by default. - Floating-point numbers, that is, numbers with a decimal point, have a type of
System.Double
by default. - The special values
true
andfalse
have a type ofSystem.Boolean
. - Strings of text characters surrounded by double quotes, as we mentioned, have a type of
System.String
. - Individual characters of text surrounded by single quotes have a type of
System.Char
.
System.Console.WriteLine(4.GetType()); // Prints "System.Int32"
System.Console.WriteLine(3.1415.GetType()); // Prints "System.Double"
System.Console.WriteLine(true.GetType()); // Prints "System.Boolean"
System.Console.WriteLine("hello".GetType()); // Prints "System.String"
System.Console.WriteLine('z'.GetType()); // Prints "System.Char"
The type of a C# value specifies what you can do with it. You can do math with numbers. Or you can capitalize text...
System.Console.WriteLine(5 - 3);
System.Console.WriteLine("hello".ToUpper());
2
HELLO
But it doesn't make any sense to capitalize a number or do math with a string. And you'll get errors if you try.
System.Console.WriteLine(5.ToUpper());
System.Console.WriteLine("hello" - 3);
Program.cs(5,36): error CS1061: 'int' does not contain a definition for 'ToUpper' and no accessible extension method 'ToUpper' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) [/home/treehouse/workspace/workspace.csproj]
Program.cs(6,34): error CS0019: Operator '-' cannot be applied to operands of type 'string' and 'int' [/home/treehouse/workspace/workspace.csproj]
Those errors are actually a good thing. C# detects that you've made a mistake, and is letting you know about it, so you can fix the problem before users of your program see it. And the only reason C# can detect these kinds of errors is because of types. Types help protect you from making mistakes in your code.
In software development, this is known as type safety: the assurance that when you have a value of a particular type, you can do particular operations on it, and be certain those operations will work. Type safety is an important feature of the C# language.
System.Console.WriteLine(5 - 3);
System.Console.WriteLine("hello".ToUpper());
So now let's look again at that error we got when we declared a variable with the var
keyword, but didn't assign it a value. "Implicitly-typed variables must be initialized..."
var number;
var greeting;
System.Console.WriteLine(number);
System.Console.WriteLine(greeting);
System.Console.WriteLine(number + 2);
Program.cs(5,13): error CS0818: Implicitly-typed variables must be initialized [/home/treehouse/workspace/workspace.csproj]
Program.cs(6,13): error CS0818: Implicitly-typed variables must be initialized [/home/treehouse/workspace/workspace.csproj]
Every variable in C# needs to have a type. When you use the var
keyword, C# will look at the type of the value you're initializing the variable with, and use that as the type of the variable. That's what it means by "implicitly-typed"; the initial value implies that a variable should have a certain type. But if you don't provide an initial value, there's no way to determine what the variable's type should be, and you'll get an error.
var number = 4;
var greeting = "hi";
System.Console.WriteLine(number.GetType());
System.Console.WriteLine(greeting.GetType());
System.Int32
System.String
So how do you declare a variable without assigning an initial value? You give it an explicit type: you specify what type of value the variable should hold. Just write the type name followed by the variable name, and the variable is declared.
System.Int32 number;
System.String greeting;
Then you can assign a value to the variable later on, when you're ready to use it.
System.Int32 number;
System.String greeting;
number = 10;
greeting = "What's up?";
System.Console.WriteLine(greeting);
System.Console.WriteLine(number + 2);
If you want, you can initialize a variable with an explicit type declaration just like you can when using the var
keyword:
System.Int32 number = 10;
System.String greeting = "What's up?";
System.Console.WriteLine(greeting);
System.Console.WriteLine(number + 2);
By the way, we did something a little unusual with the types for our variables, that C# developers don't normally do. When you call the GetType()
method on most integers, you get System.Int32
back. For strings, GetType()
returns System.String
. To make the connection clear, we used System.Int32
and System.String
as the types of our variables.
But typing System.Int32
and System.String
all the time would get really tedious. So C# offers some shortcuts.
- A type of
int
with a lower-case "i" is equivalent toSystem.Int32
. - And a type of
string
with a lower-case "s" is equivalent toSystem.String
.
int number = 10;
string greeting = "What's up?";
System.Console.WriteLine(number.GetType());
System.Console.WriteLine(greeting.GetType());
System.Console.WriteLine(greeting);
System.Console.WriteLine(number + 2);
System.Int32
System.String
What's up?
12
- There are shortcuts for other frequently-used types, too.
- Here's a program that uses variable declarations with explicit types for the same integer, floating-point number, boolean value, string, and character value that we saw earlier.
- You can see we use types of
System.Int32
,System.Double
,System.Boolean
,System.String
, andSystem.Char
for the variables.
System.Int32 wholeNumber = 4;
System.Double pi = 3.1415;
System.Boolean status = true;
System.String greeting = "hello";
System.Char letter = 'z';
System.Console.WriteLine(wholeNumber.GetType());
System.Console.WriteLine(pi.GetType());
System.Console.WriteLine(status.GetType());
System.Console.WriteLine(greeting.GetType());
System.Console.WriteLine(letter.GetType());
System.Int32
System.Double
System.Boolean
System.String
System.Char
Each one of those types has an equivalent shortcut type name. I can replace the type in each variable declaration with its shortcut name, and the program will still work the same:
int wholeNumber = 4;
double pi = 3.1415;
bool status = true;
string greeting = "hello";
char letter = 'z';
System.Console.WriteLine(wholeNumber.GetType());
System.Console.WriteLine(pi.GetType());
System.Console.WriteLine(status.GetType());
System.Console.WriteLine(greeting.GetType());
System.Console.WriteLine(letter.GetType());
C# developers almost never use the full type names for these built-in types in their programs; they use the shortcuts instead. In your code, you should use lower-case shortcut names like int
, double
, bool
, string
, and char
. But don't be surprised when the GetType()
method returns the types System.Int32
, System.Double
, System.Boolean
, System.String
, and System.Char
.
You need to sign up for Treehouse in order to download course files.
Sign up