C# Syntax C# requires you to declare the variable type at int parrotAge; the same time as the variable name. string parrotStatus = "Alive"; However it does not require you to assign a value to the variable straight away. Once assigned you can use the variable with int parrotAge; other values or variables as shown. string parrotStatus = "Alive"; Note that if you leave the variable ‘null’ when parrotAge = 12; you declare it, you must assign a value to the variable first before being able to carry out System.Console.WriteLine("The parrot is further operations on it. currently " + parrotAge + " and is " + parrotStatus); A variable can be overwritten with a new value parrotAge = parrotAge + 1; at any time. Console.WriteLine(parrotAge); You cannot assign data to a variable of different types. Each variable will only hold the data type defined. As you can see an error is shown in the IDE – as is a string, and we are trying to assign “two” it to an ‘int’. Keywords for C# data types Data Type Range byte 0 .. 255 sbyte -128 .. 127 short -32,768 .. 32,767 ushort 0 .. 65,535 int -2,147,483,648 .. 2,147,483,647 uint 0 .. 4,294,967,295 long -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
C# Syntax int parrotAge; string parrotStatus = "Alive"; C# requires you to declare the variable type at the same time as the variable name. However it does not require you to assign a value to the variable straight away. int parrotAge; string parrotStatus = "Alive"; parrotAge = 12; System.Console.WriteLine("The parrot is currently " + parrotAge + " and is " + parrotStatus); Once assigned you can use the variable with other values or variables as shown. Note that if you leave the variable ‘null’ when you declare it, you must assign a value to the variable first before being able to carry out further operations on it. parrotAge = parrotAge + 1; Console.WriteLine(parrotAge); A variable can be overwritten with a new value at any time. You cannot assign data to a variable of different types. Each variable will only hold the data type defined. As you can see – an error is shown in the IDE as “two” is a string, and we are trying to assign it to an ‘int’. Keywords for C# data types Data Type Range byte 0 .. 255 sbyte -128 .. 127 short -32,768 .. 32,767 ushort 0 .. 65,535 int -2,147,483,648 .. 2,147,483,647 uint 0 .. 4,294,967,295 long -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
398694-programming-techniques-c-teacher-guide.docx