C# Programming Flashcards
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; }