C# and OOP Flashcards
Printing a line
Console.WriteLine(iValue);
Converting number to string
string strValue = floatValue.ToString();
Casting float to int
int iValue = (int)fValue;
(rounds down)
Converting string to number
string strValue = “13.8”;
float fValue = float.Parse(strValue);
Getting a substring of a string
string.Substring(a, b) gives you the b characters which come after character a
“vge1974”.Substring(2, 4) = “e197”
Conditional statement
if (condition)
{
code
}
else
{
more code
}
Comparison operators
==, !=, <, >, <=, >=
Logical operators
&&, ||, !
“true”, “false”
Expressing x^y
Math.Pow(x, y)
While loop
while (condition)
{
code
}
(can control flow using “break” and “continue”)
Do while loop
do
{
code
}
while (condition)
For loop
for (setup; condition; increment)
{
code
}
Foreach loop
foreach (<type> currentItem in listOfItems)
{
code
}</type>
What is OOP?
A programming framework that facilitates the reuse and extensibility of code. Bits of source code with relevant purposes are grouped together as classes, managed as a hierarchy with inheritance. Objects are instances of a class, which is like a blueprint.
Advantages of OOP
With OOP, source code becomes easier to maintain and follow
Code can be reused through inheritance, improving efficiency and productivity
Security through encapsulation
Drawing classes in UML
Attributes
———–
Methods
The constructor is always there but not listed
Drawing the main program itself in UML
Attributes
———–
Methods
Creating a new object using the constructor
Cow cowName = new Cow(“Moo”, 8);
What is method overloading?
When a method has different declarations for different types of input
What are static functions and variables?
Ones which belong to the class, rather than the instances of the class (the objects). They are usually used to denote functions/variables that are the same across all objects of that class. They can be called without creating an object of such a class. They have to be changed from the class, not the object.
They are underlined in UML.
Example of using a static variable
Student s1 = new Student();
s1.studentName = “Henry”;
Console.WriteLine(s1.studentName); // instance variable
Console.WriteLine(Student.universityName); // static variable
What is encapsulation and why is it useful?
This is where a class keeps all relevant information, but only exposes necessary information to other classes.
Issues avoided by encapsulation: human errors, intentional attacks, classes which are difficult to follow
public vs private vs protected
public: accessible everywhere
private: accessible only within the same class
protected: accessible only within the same class AND child classes
Old encapsulation method using setters and getters
public class Cow
{
private float _fWeight;
private string _strName;
public void SetFWeight(float fWeightIn) { if (fWeightIn > 0.0f) _fWeight = fWeightIn; } public float GetFWeight() { return _fWeight; } }
New encapsulation method using get/set
public class Cow
{
private float _fWeight;
private string _strName;
public float WeightPublic
{
set
{
if (value > 0.0f)
_fWeight = value;
}
get
{
return _fWeight;
}
}
The “this” keyword
Refers to the current instance of the class
What is inheritance and why is it useful?
Inheritance is where a child class can be derived from a parent class to retain and update the variables and methods of the parent class.
The UML symbol is an arrow with a white head.
How to show inheritance in C#?
Syntax:
“public class Cow : Animal”
Using “base”
Allows a child class to add code to its parent’s constructor
public Cow(string strName, float fWeight, bool bMakeMilk) : base(strName, fWeight)
UML association arrow
A class creates an object of another class
UML composition arrow
Black diamond
Type of association, where a class A creates an object B of another class, and B is destroyed when A is destroyed. A is called the container and B is called the content.
UML aggregation arrow
White diamond
Opposite of composition, where the content is not destroyed when the container is destroyed.
Using lists in C#
List<type> ListName = new List<type>();</type></type>
List operations in C#
lstData.Count
lstData.Add(value)
lstData.Remove(value) // removes first occurrence
lstData.RemoveAt(k) // Removes the item at this position
What is abstraction?
Where a parent class designs the inputs and outputs of the methods, leaving the child class to implement the actual contents of the methods
Abstract classes vs abstract methods
Abstract classes are ones which cannot be used to create objects, only to derive new classes using inheritance.
Abstract methods are unfinished methods which only specify input/output values, and are expected to be provided by the derived class. Abstract methods can only be used in abstract classes, but an abstract class does not need to contain abstract methods.
What is an interface?
An EU version of an abstract class, which only has public abstract methods. A child class can implement as many interfaces as needed, as opposed to only being able to inherit one abstract class
Declaration: public interface IDrawable
What is polymorphism?
Polymorphic objects can be treated as different classes, meaning they can be cast to their parent’s class (e.g. a Cow can be cast to an animal)
Polymorphic methods can be overridden or hidden: the system decides which method to call with reference to the class of the object.