3. Classes, Records, Structs, and Tuples Flashcards

1
Q

What does ‘pass by value’ mean?

A

The actual value of the first variable is copied to the second variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does ‘pass by reference’ mean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Where is a reference type variable storied in memory? And where is a value type variable normally storied in memory?

A
  • The reference type variable is storied in the heap.
  • The value type variable is normal storied in the stack.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is boxing? What is unboxing?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A record type, created with the record keyword, is considered a value or reference type.

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is ‘syntax sugar’?

A

It is a slang term meaning the something that simply makes coding easier without adding any new fuctionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is a tuple a value type or reference type?

A

Strangely (I think), it is a value type.

Because behind the scenes it uses a struct type, which is a value type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the key difference between a static member of a class and an instance member?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the keyword for creating a static member in a class?

A

The keyword is: static.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

List all the kinds of members a class can have (there are at least eleven)?

A

(In no particular order of importance because they are all important)

  1. Fields
  2. Constants
  3. Methods
  4. Properties
  5. Constructors
  6. Indexers
  7. Operators
  8. Events
  9. Destructors
  10. Deconstructors
  11. Types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a field?

A

A field is a variable normally placed at the very top of a class (not inside a method or other member).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the readonly keyword do for a field?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of a property?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are a ‘get’ and a ‘set’?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can a property’s get and set have different access levels?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Does a property have to have both a get and a set?

A

No.

You can leave out either the get or the set. By omitting the set, you make the property read only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can you constrain a property so its value can only be set in the constructor or with an object initializer?

A

By using the init keyword instead of the set keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a function?

A

It is not synonymous with a method.

A function includes other members such as indexers, operators, constructors, destructors, and properties. As well as methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are the parts that make up a method?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the parts that make up the parameters that go into between the parentheses of a method?

A
  • The paramter type (int, float, string, etc).
  • And the parameter name which is used to access the parameter.
  • The coma used to separate two are more paramters.
  • And other modifiers such as params, in, ref, out.
21
Q

What is an expression-bodied method?

A

It’s a simplified way of writing a method. But,

  • The method must have only one statement.
  • The curly brackets and return keyword are not needed.
  • Use the => operator to separate the left fro right side.
22
Q

Identify the type of statement for each bolded line below:

public class Math
{
**public int Value { get; set; }
 public int GetSquare() =\> Value \* Value;
 public static int GetSquareOf(int x) =\> x \* x;**
}
A
  • public int Value { get; set; } is a property
  • public int GetSquare() => Value * Value; an expression-bodied method that is not static
  • public static int GetSquareOf(int x) => x * x; an expression-bodied method that is static.
23
Q

What is meant by a method’s signature?

A

The signature of a method is its name and the number of parameters it has and the types of signatures it has.

A single method can have many variations, meaning that they all have the same name, but the number and types of parameters will differ. This is overloading.

24
Q

Name two things not sufficient to create an overloaded method.

A
  • Different return types.
  • Parameters with different names.

Meaning, you cannot overload a method simply by giving it a different return type or by giving the parameters different names.

25
Q

What is a name argument?

A

All (I think they all do) parameters have names. But when invoking a method you do not need to use those names when supplying arguments because is you place the argument in the propert order the method knows which parameter they belong to.

However, you can, if you want, use the parameter name when supplying arguments. This is optional and can make your code easier to read.

26
Q

What makes an argument optional?

A

If it has a default value.

When the parameters of a method are defined, if those parameters are give default values then the parameter is optional.

27
Q

Can you define more than one optional parameters for a method?

A

Yes, you can define as many as you need.

28
Q

What is the purpose of the params keyword?

A

Easier to show than explain, but I’ll try explaining anyway. The params keyword is always place before the definition of an array type. For example:

public void AnyNumberOfArguments(params int[] data)
{
foreach (var x in data)
{
Console.WriteLine(x);
}
}

(The array type can be any type.)

But when this method is being used, an unlimited number of items (this this example of type int) can be placed as arguments. The compiler will group all the items into an array.

29
Q

What is a constructor?

A

Every class has a constructor. If you do not create one yourself, the compiler will create one.

A constructor is a special method that gets the class started. It will make sure all the basic stuff that needs to be done to make the class work gets done. For example it will make sure all fields are initialized to default values.

A constructor has the same name as the class and it has no return type.

30
Q

Can constructors be overloaded?

A

Yes, they can. And it is very common that they will be.

An overloaded constructor means you have multiple constructors defined, each with the same name, but each have a unique number and types of parameters. (They must each have a different signature.)

31
Q

When does the compiler create a default constructor?

A

When you do not create one.

32
Q

Does a constructor require an access modifier?

A

In almost all cases the answer is yes. To make your class work as expected you will need to add the ‘public’ access modifier.

You can use other access modifiers, such as private or protected, but this would not be normal and you need to understand what your doing and why your doing it.

33
Q

When can a constructor be implementated as a expression body?

A

The term ‘expression body’ make this sound much harder than it is.

If the constructor will consist of just one line, instead writing a bunch of curly braces, you can use the => symbol.

34
Q

What should you do if you have multiple constructor overloads and they have code in common?

A

You should not duplicate the same code in each constructor.

Instead, you should involk a constructor that has the code from the second construtor using the the this keyword.

35
Q

Does it ever make sense to have static constructors?

A

Yes, it often makes very good sense.

Static members of a class can be used even if the class has not been initialized (using the ‘new’ keyword). The static constructor will assure that the static methods are initialized correctly before being used.

36
Q

How often and when will a static constructor be involked?

A

Just once.

It will run before any other part of the class is ran.

37
Q

What is a local function?

A

It’s basically a method inside another method.

38
Q

What are some important restrictions to know about local functions?

A
  • The local function can only be accessed by the method it is inside.
  • Parameters and return types are used in the same way as normal methods.
  • A local function can be implementated with an expression-body or by using the curly brackets.
  • A local method can be static.
39
Q

What are extension methods?

A

They are static methods that extend other types. Extension methods make use of the ‘this’ keyword.

40
Q

What is an anonymous type?

A

It’s a type without a name. It makes use of the ‘var’ and ‘new’ keywords.

41
Q

How do you create an anonymous type?

A

Start with the ‘var’ keyword,

Add a name,

the ‘=’ sign

use the ‘new’ keywords,

and the two curly brackets { },

put something between the curly brackets { }.

42
Q

What are ‘records’?

A

They are reference types that support value semantics.

43
Q

What is the purpose of ‘records’?

A

To reduce the amount of code you have to write.

44
Q

Are ‘records’ immutable or mutable?

A

Trick question. They are both.

45
Q

What are the two kinds of ‘records’?

A
  • Nominal records
  • Positional records
46
Q

What makes a “nominal record” different from an object using the ‘class’ keyword?

A

A ‘record’ can contain all the members of a class. The difference is that a record has some added or different functionality.

47
Q

What makes ‘positional records’ different from ‘nominal records’?

A

They are mostly the same, but ‘positional records’:

  • Have parentheses after the name of the record to specific the members of the record.
  • Have additional methods for deconstruction.
48
Q

How does equality comparison with records work?

A

To understand this, you need to understand the difference between how reference types and value types handle equality.

With reference types, equality compares memory locations. If two reference variables point to the same memory location, then they would equate to true.

For value types it is the actual value of the variable that matters, not memory locations. I variable A is 7 and variable B is also 7, the equality comparison would be true (regardless of where they are stored in memory).

With ‘record types’ this is flipped. A record type is also a reference type, but it compares values like a value type.