C# Programming Flashcards
(35 cards)
Given an example of some possible properties for the class Pet and their data types.
Pet{ Name : string; Age: int; Species: string; Food: string HasFur: boolean
Why do we use classes?
To reduce duplicate code and allows us to quickly make objects that have similar data. It allows us to write class specific code that dictates what should be done with this data.
What two things do classes have?
Data - represented by fields
Behaviours - represented by methods
Declare a class for Snake that has fields: Name as a string and Age as an int. Also write a method called feed that takes no parameters and prints the word 'Gulp!"
public class Snake { private string Name; private int Age; public void Feed () { Console.WriteLine("Gulp!"); } }
How would you initialize an object called Bane of class Dog?
var Bane = new Dog();
What is the purpose of a constructor?
Constructors are a method that are called when the instance of a class is created. It puts the object in an early state with empty fields. Numerals are set to 0, characters to '\0', boolean to false and strings and classes to null.
Why do we use overloading?
Overloading is using the same method name with different parameters or return types. That way we can control how a method deals with data based on the different data that is given to it. E.g Do x if given a name, Do y if given a number etc.
How is the ‘this’ keyword used in C#?
The ‘this’ keyword refers to the current instance of a class or struct. It refers to an object field not a local parameter so in the line:
this.price = price
this.price refers to the price field of an object
price refers to the parameter price of the method.
What are the two main types of visibility?
Public - Member can be reached from anywhere
Private - Can only be reached by members of the same class.
How would you set a read only field for a string Name?
public string Name {get; private set;}
How would you set a write only field for an int Age?
public int Age {private get; set;}
Can you use logic to validate object fields?
Yes you can, you can validate the fields by putting the logic in the set clause. E.g public int Age { get { return age;} set{ if(value<0) {Console.WriteLine("Age is less than zero")} else {age = value;} }
Can you overload a method with the same data types?
E.g one overload takes in two ints and a second overload takes in to ints?
No. Overloads must have different parameters.
How are properties useful in C#?
Properties are used to access private variables that are located elsewhere in the program. For example private string name; is a field public string Name; is a property { get { return name; } set { name = value; } } get retrieves the private field value and set sets the value of Name to the value retrieved from the private field. We can now use Name in our program while name remains inaccessible.
Instead of a property how else could you access field data while maintaining it stays private?
You could create a method that updates data. For example if you had a User class with private strings for email, username, password you could create a method called registerUser that takes in a username, email and password and updates the data in an object.
public void registerUser(string username, string password, string email) { this.username = username; this.password = password; this.email = email; }
What is aggregation?
Aggregation is where we have two separate classes that reference each other. They can exists on their own but can also use data from another class. Therefore we create a reference to one class within another class.
Why do we use OOP?
We use OOP so that teams can work and build large robust programs separately but still have them come together and work well together once assembled.
What are the 4 pillars of OOP?
Encapsulation: Groups related objects and functions together which increases reusability and reduces complexity.
Abstraction: Hides some properties and functions from the outside to make interfacing with the classes/methods simpler and reduces the impact of change on the classes.
Inheritance: Allows us to define a base group of properties and methods for a class that every sub-class has and can use. Reduces the amount of code in a program.
Polymorphism: Allows us to remove long ‘if else’ or ‘switch’ statements. Allows us to give each instance of an object instructions on how to behave in different conditions with different parameters.
What does the notation: public class car:vehicle mean?
It means that car inherits from vehicle
How do we express requirements in OOP design?
Give an example.
User stories are used to express requirements.
E.g. “As a seller, I want to list my item for sale and look at bids for my item”
What was the old system of development?
What is the new more robust system?
Waterfall development was the old way.
Incremental iteration is the new way. We take on small part or system at a time then find requirements -> design system -> implement system -> test system -> refactor
What is refactoring?
Refactoring is the process of modifying code so that it has the same functionality but is ready to facilitate new required functionality.
What are the steps of OOD?
- Identify candidate classes by looking for nouns in the user story e.g Book, Car, Item
- Evaluate each class by given it a responsibility by looking for verbs in user stories e.g Rent, Buy, Sell
- Determine the relationships between classes by looking at what data is needed by each class e,g Renting a book, Buying a car
What are the four relationships between classes in OOP and an example of each?
Has-a = A car has-a windscreen Part-of = A player is part of a team Member-of- A car is a member of the vehicle class Uses = A car uses the road to drive on.