Knowledge Check - Variables
1. Name the three things all variables have.
All C# variables have a name, a type, and a value. The name is how you refer to it in code, and tell it apart from another variable. The type indicates what type of data can be stored in the variable. The value is the contents of the variable.
2. True/False. All variables must always be declared before being used.
True. You can only use a variable on or after the line it is used.
3. Can you redeclare a variable?
No. You can only declare a variable once. However, you can assign values to variables as many times as you want.
4. Which of the following are legal C# variable names? answer
, 1stValue
, value1
, $message
, delete-me
, delete_me
, PI
.
answer
, value1
, delete_me
and PI
all follow the rules. A variable cannot begin with a
number, so 1stValue
is not legal. A variable cannot contain the $
symbol, so $message
is
not legal. A variable cannot contain a -
, so delete-me
is not legal.