Object Oriented Programming in C#
What is OOP?
"Object-oriented programming (OOP) is a programming paradigm using ""objects"" - data structures consisting of data fields and methods together with their interactions - to design applications and computer programs. There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation. Other parts include data abstraction, messaging and modularity."
What are access modifiers?
Access modifiers determine the scope of the method or variables that can be accessed from other various objects or classes. There are 5 types of access modifiers , and they are as follows:. Private - only the base class can access Protected - the base class and all of its derived classes can access Public - any class in the namespace can access
What is the difference between an Abstract class and a Concrete class?
An Abstract class cannot be instantiated. It is intended only to be used as a base class for other classes. A Concrete class can be instantiated.
What is the enum type?
An enumerated type is a set of named constants. Ex: enum <enum_name> { enumeration list }; enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
What is an extension method?
An extension method is a way of extending static methods to use it in a derived class like an instance type method. An extension method must reside in a static class and the method it is extending must be static. Ex: public static class StringHandler { public static string InsertSpaces(this string source) then... productName.InsertSpaces();
What is an interface??
An interface is a class that only includes method signatures. The methods are not implemented in the interface. Another class must be created to supply the implementation. Ex: public interface ILoggable string Log();
What is an Entity?
Any object that is defined by a class is referred to as an entity. Ex:
What the AAA method of setting up Unit Tests?
Arrange -> Sets up the test Act -> Contains the code being tested Assert -> Verifies the result of the test
What is a Business Object?
Business object often refers to classes.
Difference between object and class
Class contains methods and properties of an object. Provides definition for a particular type of object. Object is an instance of a class. Object variable holds the state of the object.
What is cohesion?
Cohesion is the degree to which members of the class relate to the purpose of the class. High cohesion means that a feature request will likely only affect a small number of classes, simplifying maintenance.
What is a Nullable Type and how is it defined within C#?
Defined by the ? character (ex: Decimal? in a property name). A nullable type is a value type such as an integer or decimal that allows definition of the value type or a null.
What is method overriding?
Method overriding is a feature that allows sub class to provide implementation of a method that is already defined in the main class. This will overrides the implementation in the superclass by providing the same method name, same parameter and same return type. Syntax: override ToString() is... public override string ToString() { return base.ToString(); }
Can you inherit from more than one class in C#?
No. It does not support multiple inheritance.
Are objects Reference or Value Types?
Objects are reference types, unless the static modifier is used on a property or method, which makes it act as a value type.
What is Constructor Chaining?
One constructor calls another constructor.
What are Design Patterns?
Recurring, reusable solutions to common class and class relationship problems. Example: storing CRUD methods in a Repository class (Repository Pattern).
What is a Sealed class?
Sealed classes are those classes which can not be inherited and thus any sealed class member can not be derived in any other class. This prevents extension and customization.
What is encapsulation?
Surrounding something, not just to keep the contents together, but also to protect those contents. Restricts access to the inner workings of a class or any objects based on that class; this is referred to as information hiding or data hiding.
What does the Abstract keyword do to methods?
The abstract keyword marks a method which must be overridden in the class's derived classes. It does not have it's own implementation and so does not have a statement body. Ex: public abstract bool Validate();
What is a default constructor?
The default constructor is a constructor that doesn't take in data, it simply initializes the data for the other constructors in the class to utilize. It has no parameters. If you don't need to add any default values, aside from those already defined as properties, a default empty constructor will be created for you.
What is coupling?
The degree to which two classes are dependent on each other. Low coupling is always the goal, and that means that changes to one class are not as likely to adversely affect another class, which makes maintenance easier. It also makes is easier to test a class, since it has minimal dependency on other classes.
What is the difference between an implicit and explicit class interface?
The implicit class interface is the set of public properties and methods in a class are automatically included in the class's interface. The explicit class interface is defined with the interface keyword, and does not provide any implementation. It is reuseable across other classes. The explicit interface defines the role an object can play.
What is a class contract?
The publicly exposed methods (or functions) and properties (or fields or attributes) of that class interface along with any comments or documentation that apply to those public methods and properties. The class contract is also called the class interface.
What is an IEnumerable?
The recommended way to return a sequence of data because the results are more flexible for the callers of the method.
What is the difference between value types and reference types?
Value types store their data directly. Reference types store references to their data.
What is a circular reference?
When two different projects reference each other.
What is the YAGNI principal?
You Ain't Gonna Need It. Avoid adding unnecessary features based on potential future needs.
How does encapsulation enable data hiding?
It protects the data by allowing for authorization before GETTING the data, and validation when SETTING the data.
What does a separation of concerns accomplish?
- Minimizes coupling - Maximizes cohesion - Simplifies maintenance - Improves testability Decompose an application into parts with minimal overlap (coupling). Each part is responsible for a separate concern (cohesion).
Techniques for Reuse
1. Collaboration 2. Inheritance 3. Components
What are the 3 basic types of Object Relationships?
1. Collaboration ("uses a") -> Customer Repository uses a Customer 2. Composition ("has a") -> Order has a Customer, Address, and Order Item - Aggregation -> type of composition in which component parts do not exist except as a Composition -> Order Item does not exist without Order 3. Inheritance ("is a") -> Customer is a Government type Customer
What are some examples of .NET interfaces?
1. IDisposable 2. IEquatable 3. IComparable 4. INotifyPropertyChanged 5. IEnumerable 6. IObservable
What are some Object class methods in C#?
1. ToString() -. "System.Object" 2.
3 Basic Layers of Applications
1. User Interface (View) 2. Business Logic (Model) 3. Data Access (Controller)
What is a class constructor?
A class constructor is code that is executed every time an instance of the class is created. It is named with the class name. ( C# shortcut -> ctor )
What is a method signature?
A method signature is the method's name and type of each of its parameters. It does not include it's return type.
What is inheritance-based polymorphism?
A single method can behave differently depending on the type of object that calls it.
What is polymorphism?
A single method, such as a ToString() method, can behave differently depending on the type of object that calls it. It allows you to work with a group of classes in a uniform way. There are different types: 1. Inheritance-based Polymorphism - A base class defines a method, and a derived class overrides that method to define its own unique definition and implementation.
What is the difference between a class and a struct?
A structure is a value type while a class is a reference type. A structure does not support inheritance (but can implement interfaces). A structure cannot have a default constructor.
What are Nullable Types in C#?
A value type that can be made nullable. Ex: Nullable<T> Nullable<string> or string?
What is abstraction?
Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstraction lets you focus on what the object does instead of how it does it. - Simplifying reality - Ignoring extraneous details - Focusing on what is important for a purpose
What is a Collaboration Object Relationship?
Exists when a class uses an instance of another class.
What is a Composition Object Relationship?
Exists when an object is composed of one or more objects from another class. Composite objects often contain references to their component objects as properties.
What are the four main pillars of OOP?
Four main pillars: 1. Abstraction 2. Encapsulation 3. Polymorphism 4. Inheritance
What is the difference between IEnumerable<string> and List<string>?
IEnumerable is an interface which List implements. IEnumerable is a sequence of string whereas List in indexable by an int index, which can add and remove at certain indexes. A List is a specific random-access variable-size collection. IEnumerable has better performance than List.
What is method overloading?
If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program. They should provide variations of the same functionality.
What does the Virtual keyword do?
In a method, a virtual marked method has it's own implementation but allows for the method to be overridden. It does not have to be overridden (unlike abstract methods).
What is inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding. The child class (or derived class) inherits from the parent class (or base class).
How does the static modifier affect a class property or method?
It declares a member that belongs to the class itself, instead of a reference to the class object (or an instance of the class object). It is accessed using the class name, not an object variable.
How does encapsulation enable implementation hiding?
It helps to manage the complexity of a program because only the class needs to understand the implementation details, and the implementation can be changed without affecting the rest of the application.