3. Classes, Records, Structs, and Tuples Flashcards
What does ‘pass by value’ mean?
The actual value of the first variable is copied to the second variable.
What does ‘pass by reference’ mean?
With a reference type the variable holds only the memory location where the object is storied (unlike a value type where the variable holds the actual value). So, when you use the reference variable in code, the variable tells the code where the object is located (unlike a value type which would all give the actual value to the code).
If you copy reference value A to reference value B, only the memory location of what A is pointing to is copied to B. Which means, both A and B now point to the same object.
Where is a reference type variable storied in memory? And where is a value type variable normally storied in memory?
- The reference type variable is storied in the heap.
- The value type variable is normal storied in the stack.
What is boxing? What is unboxing?
Boxing is when a value type (also known as a struct) is wrapped inside a reference type and is storied, along with that reference type, inside the heap.
Unbox is when that value type is unwrapped from that reference type and placed back into the stack.
A record type, created with the record keyword, is considered a value or reference type.
It is a reference type.
(Obviously, an object can be of more than one type. Here a record type is a record, but it is also a reference type.)
What is ‘syntax sugar’?
It is a slang term meaning the something that simply makes coding easier without adding any new fuctionality.
Is a tuple a value type or reference type?
Strangely (I think), it is a value type.
Because behind the scenes it uses a struct type, which is a value type.
What is the key difference between a static member of a class and an instance member?
An instant of a class is a specific implementation of a class. A single class might have many different implementations. The non-static members of each implementation would be independently of the non-static members in other implementations (they can have different values).
But not the static members. The static member of one implementation is the same as the static members of other implementation (meaning, if they have variables, those variables will always be same for all the implementaions).
This is perhaps a bit confusing, but not too hard once you start doing examples.
What is the keyword for creating a static member in a class?
The keyword is: static.
List all the kinds of members a class can have (there are at least eleven)?
(In no particular order of importance because they are all important)
- Fields
- Constants
- Methods
- Properties
- Constructors
- Indexers
- Operators
- Events
- Destructors
- Deconstructors
- Types
What is a field?
A field is a variable normally placed at the very top of a class (not inside a method or other member).
What does the readonly keyword do for a field?
It assures the field’s value can only be changed in the constructor. Once the constructor has ran, and the value of the field is set, the field’s value can no longer be changed.
What is the purpose of a property?
A property has the same purpose as a field; it creates a variable that can be used throughout the class. But a property can contain programming code that will limit the values the property can have.
For example, a variable for a person’s age should not allow for negative ages or ages of large numbers, say over 120 or so.
What are a ‘get’ and a ‘set’?
They are the important part of a property.
- A ‘get’ retrieves the value of the property.
- A ‘set’ can set the value of a property.
Can a property’s get and set have different access levels?
Yes.
Does a property have to have both a get and a set?
No.
You can leave out either the get or the set. By omitting the set, you make the property read only.
How can you constrain a property so its value can only be set in the constructor or with an object initializer?
By using the init keyword instead of the set keyword.
What is a function?
It is not synonymous with a method.
A function includes other members such as indexers, operators, constructors, destructors, and properties. As well as methods.
What are the parts that make up a method?
- Method modifiers (such a public or static).
- The return type (if nothing is returned it would be ‘void’).
- The name of the method.
- Enclosed in parenthese will be the list of parameters the methods nees to operate.
- Finally, comes the main body of the method that is enclosed in curly brackets { } .
- Within the curly brackets are all the code the method requires.