PD1: Chapter 3: Concerning Variables

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.
  • 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).
  • 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.
  • What is the scope of an instance variable in C#?
    The scope of an instance variable is the complete class.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • What is the default value for numerical datatypes in C#?
    The default value for numerical datatypes is 0.
  • What is the default value for the boolean datatype in C#?
    The default value for the boolean datatype is False.
  • 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.
  • 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.
  • 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.
  • What does it mean that C# is a static typed language?
    It means each variable definition contains the datatype of that variable.
  • What are compile time errors in a statically typed language like C#?
    Errors detected during compilation due to type mismatches.
  • What is the definition of variable initialization?
    Initialization is the assignment of the first value to a variable.
  • What is the difference between initialization and assignment?
    Initialization is the first assignment to a variable, while assignment can occur multiple times.
  • 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.