Study Flashcards
(63 cards)
What is a generic type?
A type of which represents a placeholder for any data type you choose when creating an instance of the class.
E.g. Here’s a simple example using List<T> in C# to illustrate generics:</T>
List<int> numbers = new List<int>();</int></int>
So you can see that we use an int alternatively we could create:
List<string> words = new List<string>();</string></string>
Why are generics useful?
- Generics are useful because they enable code re-usability
- Generics ensure that when you specify a type (like List<int>), the compiler checks that you only use that type. This means errors, like adding a string to a list of integers, are caught before your program runs.</int>
What is Encapsulation in programming?
Putting everything to do with an Object in one place. E.g. Car has brake pedals, steering wheel, wheels etc inside the car body. (class)
What is Polymorphism?
Base classes like Animal has a function make animal sound. We create Pig and Dog class, due to polymorphism they can call make an animal sound as they are an animal. You can change how this inherited function behaves using the OVERRIDE keyword but you need to use VIRTUAL when defining the Animal classes function you want to override.
e.g.
virtual would allow the child class to override it but you have to use override on the child classes function that you’re inheriting
class Animal // Base class (parent)
{
public VIRTUAL void animalSound()
{
Console.WriteLine(“The animal makes a sound”);
}
}
class Pig : Animal // Derived class (child)
{
public OVERRIDE void animalSound()
{
Console.WriteLine(“The pig says: wee wee”);
}
}
Without override the Pig would output “The animal makes a sound” when calling animalSound() method.
What is data abstraction?
Hint abstract
Process of hiding certain details, so you can have an abstract class Animal as the base with the Abstract function (they will never be created or called) for the sole purpose to force the child classes to define their own.
Hide Details: You only see the important features, not the messy inner workings.
Common Blueprint: It forces every similar object to have certain methods, so they all work in a predictable way.
Easier Changes: If you change how something works behind the scenes, you don’t have to change the code that uses it.
What is LINQ in C#
A built-in query language for C# that lets you query collections using readable syntax.
var filteredUsers = from user in users
where user.Id == desiredId
select user;
What is the benefit of using LINQ?
Makes it easy to filter, sort, and transform data within your code.
What is an async method? Why is it good?
An async method runs tasks without blocking the main thread,
Simplifies asynchronous programming by letting you write code that waits for tasks to complete without blocking the main thread.
Improves responsiveness
What does the await keyword do?
await keyword tells the method to wait for a task to finish without freezing the application.
Are arrays fixed size in C#
Yes they are. Use a list instead. Otherwise you’ll have to use the 2 pointer technique and we don’t like that one.
What is Single Responsibility Principle?
It means a class or module should focus on one specific task—grouping similar tasks together and keeping unrelated ones separate—so if its purpose changes, only that part of the code needs updating.
Example:
Instead of one class handling both user authentication and email notifications, create separate classes—one for authentication and one for sending emails.
This way, if you need to change authentication, you don’t affect the email logic.
What is the Open Closed Principle?
It means that a class or module should be designed to allow new features to be added without changing its existing code, keeping the system stable while making it easy to extend.
It means a class is “open” to new features but “closed” to changes.
Example: In a drawing app, have a common “draw()” method in a Shape interface.
Each shape (Circle, Square, etc.) implements its own draw().
Adding a new shape means creating a new class—no changes to existing code.
In a drawing app, define a Shape interface with a draw() method.
To add a new shape like Triangle, create a new Triangle class that implements Shape instead of modifying the existing drawing code.
What is an interface?
It’s a contract that specifies methods a class must implement, without defining how they’re done. For example, a Shape interface might require a draw() method, and each shape (like Circle or Square) provides its own version of draw().
How do interfaces support the Open/Closed Principle?
Interface = Remote Rules (Needs Power button)
* TV (Old Code) uses any remote with that button.
* Make New Remote (New Code) with Power button.
* Result: TV doesn’t change!
* ✅ Add New Remote (Open)
* ❌ Don’t Change TV (Closed)
What is the Liskov Substitution Principle?
It means that objects of a subclass should be replaceable with objects of the superclass without breaking the program. Subclasses must honor the contract of their parent classes so they work seamlessly in any context expecting the base class.
So a CEO doesn’t have a manager, so they don’t have all of the attributes a Employee or a Manager would for example thus we use Interfaces like IEmployee IManaged IManager to break it down. Need to practice this one I think. Goal is to learn to use Inheritance correctly.
What is the Interface Segregation Principle?
It means don’t force a class to implement methods it doesn’t need. Instead, split large interfaces into smaller, specific ones so that clients only depend on what they use. You can then combine these as required so that when you create objects you have access to the attributes you need to set.
What is the Dependency Inversion Principle?
Depend on ABSTRACTIONS (roles/interfaces),
not concrete DETAILS. Lets you SWAP parts.
Instead of THIS inside your class:
self.notifier = EmailNotifier() # BAD! Hardcoded!
Do THIS (pass the notifier IN):
# Somewhere else: notifier = EmailNotifier() OR SmsNotifier()
worker = Worker(notifier) # GOOD! Flexible!
What is a component in react?
A reusable piece of UI that returns JSX. (Javascript XML)
const Hello = () => {
return <h1>Hello, world!</h1>;
}
// Functional component (Preferred)
const Hello = () => <h1>Hello, world!</h1>;
What is Javascript XML in React?
Definition: JSX is HTML-like syntax in JavaScript that lets you build React components.
Key Points: Write UI in a familiar, declarative style.
Use curly braces { } to embed JavaScript.
const Greeting = () => {
const name = “Alice”;
return <h1>Hello, {name}!</h1>;
};
What are Props in ReactJS?
Read-only inputs passed from a Parent component down to a Child component.
(Like function arguments for components).
// Child component: receives & uses props
function Greeting(props) {
// Access data via props object (e.g., props.name)
return <h1>Hello, {props.name}!</h1>;
}
// Parent component: defines & passes data via props
function App() {
const user = “Bob”; // Data to pass down
return (
<div>
{/* Pass data to Greeting via the ‘name’ prop */}
<Greeting name={user} />
</div>
);
}
/* Output:
<h1>Hello, Bob!</h1>
*/
What is State in React?
Definition:
State gives component’s memory (for data) that can change and affect the UI when updated.
Usage:
Managed with useState in functional components
e.g. Show Password Visibility
const [show, setShow] = useState(false); // State: false means hidden
<input type={show ? “text” : “password”} />
// When state is updated (onClick)
// TRUE type = text
// FALSE type = password
<button onClick={() => setShow(!show)} > {show ? “Hide” : “Show”}
</button>
What is Event Handling in React?
Definition:
React uses camelCase event names (e.g., onClick, onChange) and expects functions instead of strings.
Key Points:
\+ Uses synthetic events for cross-browser consistency. \+ Often used to update state or handle user interactions.
// Button click updates state
<button onClick={() => setCount(count + 1)}>Increment</button>
// Input change updates state
<input onChange={e => setText(e.target.value)} value={text} />
What is Conditional Rendering in React?
Definition:
Displaying elements based on conditions using JavaScript inside JSX.
Examples:
Using a ternary operator:
{isLoggedIn ? <Dashboard></Dashboard> : <LoginPage></LoginPage>}
Using logical AND:
{errorMessage && <div className="error">{errorMessage}</div>}
What are Lists and Keys used for in React?
Lists:
Render arrays of data into JSX elements using methods like Array.map().
Keys:
Provide a unique identifier for each element in a list to help React track changes efficiently.
Example:
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>