Methods and Classes Flashcards

1
Q

How do you set a default value for a parameter in a method?

A

Set the parameter equal to something in the parenthesis.

static void main(string greeting = “Hello”)
{
Console.WriteLine(greeting);
}

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

What are Named Arguments?

A

Named arguments are a way to create parameters in a method where the order of the params doesn’t matter. You use the key : value syntax for this.

static void MyMethod(string child1, string child2, string child3)
{
Console.WriteLine(“The youngest child is: “ + child3);
}

static void Main(string[] args)
{
MyMethod(child3: “John”, child1: “Liam”, child2: “Liam”);
}

You can also specify which param to call.

static void Main(string[] args)
{
MyMethod(“child3”);
}

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

What is Method Overloading?

A

Method Overloading is where multiple methods can have the same name with different parameters. This allows the method to behave differently based on the arugments used when it’s called.

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

What is a Constructor?

A

A special method used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created. It can be used to set initial values for fields.

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

The constructor name must match the class name, true or false?

A

True. The constructor also doesn’t have a return type. All classes have a constructor by default. If you don’t create one, the language will create one for you, but you won’t be able to initialize values.

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

What are the four Access Modifiers?

A

Public, Private, Protected, and Internal.

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

What does the Public Access Modifier do?

A

Makes the data accessible to all classes.

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

What does the Private Access Modifier do?

A

Makes the code accessible only inside of the class.

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

What does the Protected Access Modifier do?

A

Makes the code accessible only inside of the class and any class derived from it.

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

What does the Internal Access Modifier do?

A

The code is only accessible within its own assembly, but not from another assembly.

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

What are the two combinations of Access Modifiers

A

Protected internal and private protected

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

What is the default Access Modifier?

A

Private

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

What keyword do you use if you don’t want other classes to inherit from a class?

A

Sealed

sealed class Vehicle 
{
  ...
}
class Car : Vehicle 
{
  ...
}

Error message: ‘Car’: cannot derive from sealed type ‘Vehicle’

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

What keyword is used on a base class method to allow it to be overridden?

A

Virtual

public virtual void animalSound()
{
Console.WriteLine(“The animal makes a sound”);
}

You can also use the abstract keyword.

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

What keyword is used on a derived class method to allow it to be overridden?

A

Override

public override void animalSound()
{
Console.WriteLine(“The dog says: bow wow”);
}

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

What is an Abstract Class

A

A restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

abstract class Animal 
{
  public abstract void animalSound();
  public void sleep() 
  {
    Console.WriteLine("Zzz");
  }
}
17
Q

What is an Interface?

A

A completely “abstract class”, which can only contain abstract methods and properties (with empty bodies).

// interface
interface Animal 
{
  void animalSound(); // interface method (does not have a body)
  void run(); // interface method (does not have a body)
}
18
Q

By default, members of an interface are all abstract and which access modifier?

A

Public