Save
SEMESTER 01
PLATFORM DEV THEORY
PD1: Chapter 3: Concerning Variables
Save
Share
Learn
Content
Leaderboard
Share
Learn
Created by
Soeroe
Visit profile
Cards (21)
What is the definition of a variable in C# according to the C# language reference?
A variable is defined in the C# language reference:
C#6.0
draft specification.
View source
What are the two types of variables in C#?
Instance variable
: declared outside any method, belongs to the instance of the class.
Local variable
: defined inside a code block (like a method).
View source
Why is it important to distinguish between instance and local variables?
Because a variable is either an instance variable or a local variable,
never both
,
never neither
.
View source
What is the scope of an instance variable in C#?
The scope of an
instance
variable is the
complete
class.
View source
How does the scope of a local variable differ from that of an instance variable?
A local variable is known only within its defining
method
, while an instance variable is known throughout the entire class.
View source
What is the significance of the textual position of a local variable's definition?
A local variable can only be used after its definition, making the textual position
important.
View source
What happens when a local variable has the same name as an instance variable?
The local variable
shadows
the instance variable within the common scope.
View source
How does the compiler distinguish between a local variable and an instance variable with the same name?
The compiler refers to the closest
encapsulating definition
, which is the local variable.
View source
What must be done to refer to an instance variable when a local variable with the same name exists?
You must prefix the instance variable name with "
this
." to refer to it.
View source
What is the lifetime of a local variable in a method?
The lifetime of a local variable is from the
execution start
to the
execution end
of that method.
View source
How does the default value assignment differ between local and instance variables?
A local variable does not get a default value unless
explicitly
assigned, while an instance variable gets a default value when defined without an explicit value.
View source
What is the default value for numerical datatypes in C#?
The default value for numerical datatypes is 0.
View source
What is the default value for the boolean datatype in C#?
The default value for the boolean datatype is False.
View source
What are the guidelines for naming instance and local variables in C#?
Instance variable names must start with an
underscore
.
Local variable names are not allowed to start with an underscore.
View source
What is the rule of thumb for choosing between instance and local variables?
Use a local variable when possible, and an instance variable only when a local variable is not sufficient.
View source
What are the implications of using instance variables in terms of code complexity?
Unnecessary instance variables introduce extra complexity.
Each code update must consider all instance variables.
More instance variables increase
potential errors
.
View source
What does it mean that C# is a static typed language?
It means each variable definition contains the datatype of that variable.
View source
What are compile time errors in a statically typed language like C#?
Errors
detected during compilation due to type
mismatches.
View source
What is the definition of variable initialization?
Initialization is the assignment of the first value to a variable.
View source
What is the difference between initialization and assignment?
Initialization is the first assignment to a variable, while assignment can occur multiple times.
View source
What are the key takeaways from Chapter 3 regarding variables?
Instance and
local
variables are not the same.
A variable has an associated
scope
, different from its
lifetime
.
Some variables have a
default value
.
Choosing between
instance
and local variables influences code quality.
View source