C# Fundamentals Flashcards
Value Types
Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself.
No pointers, no references.
Many built-in primitives are values types such as Int32, DateTime, double, float.
How to create my own value type? use struct
When we call a method we always pass parameters by value. It doesn’t matter if the variable is a reference variable or type variable, we always pass parameters by value unless specified otherwise.
Reference Types -
It’s a pointer, an address that lead to where the object can be found in memory.
You can have many object point the same reference
Ex: Classes, Interface, delegate
Class vs Struct IN C#
We use struct to create a value type and class to create a reference type.
Access Modifiers
Access modifiers are keywords used to specify the declared accessibility of a member or a type. PUBLIC: Access is not restricted. PROTECTED: Access is limited to the containing class or types derived from the containing class. INTERNAL: Access is limited to the current assembly. PROTECTED INTERNAL: Access is limited to the current assembly or types derived from the containing class. PRIVATE: Access is limited to the containing type.
Static keyword
An static member can be accessed without its class being instantiate. ex. Console.WriteLine(); WriteLine is a static method, we don't need to instantiate Console in order to use static.
STRUCT
One way to create a value type is to create a struct. you want to write a class by default. Structs are meant for special cases. Struct do best when they contain a small data.
ENUM
The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. ex: public enum PayrollType { contractor = 1, Salaried, Executive, Hourly, }
Immutability
Value types are usually immutable. Once you create a value, you cannot change the value.
Array
Manage a collection of variables
- they are fixed size
- they’re always 0 indexed
Methods
- Methods define behavior
- every method has a return type
- every method has zero or more parameters
params keyword allow you to specify the number of parameters - Every method has a signature
Name of method + parameters (type and modifiers are significant)
Fields
Fields are variables of a class. They sometimes called the state of a class
Properties
A property can define a get and/or set accessor
Delegates
Delegates are used to pass methods as arguments to other methods
Events
An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object.
Exception
- Exceptions provide type safe and structured error handling in .NET
try and catch try { // try this code } catch (exception ex ) { // if the code in the try block throw Exception ex, then show message that is // in the catch code bracket }
Finally
Using
- using can be used to pull a namespace
ex: using System.IO;
- using (expression) { // The using expression will make sure that // it's called Dispose on the method and class it }
Pillars for OOP
Encapsulation
Polymorphism
Inheritance
Abstraction
Inheritance
Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes
Use : to show inheritance
virtual
When the virtual keyword is used, C# allows the method to be invoked will determined at runtime base on the types of the objects
Without the virtual keyword is used, the method to be invoked will determined at runtime based on the types of the variables
Overriden
You need a virtual method to override a method
Polymorphism
many shapes
object can behave differently depending on their type
Polymorphism “Is a” type relationship
Abstract
Definition: a class that has one or more abstract members. An abstract member is a member that is defined, but not implemented. We cannot create an instance of an abstract class. Therefore we must have child classes that implement these members. Abstract method has the virtual keyword implied.
Interface
Definition: Interface defines a contract consist of public members such as (properties, Methods, Events and indexers). When a class implements an interface, it makes a commitment to fulfill the contract. To fulfills the contract the class simply provides implementations for all of the class members. An interface can inherit from another interface.
IDisposable
Provides a mechanism for releasing unmanaged resources. Release resources (files, connections)
using (expression) { // The using expression will make sure that // it's called Dispose on the method and class it }
IEnumerable
Exposes an enumerator, which supports a simple iteration over a non-generic collection. supports iteration (foreach)
INotifyPropertyChange
Raises events when properties changed. Used often in wpf and xaml
IComparable
compares two objects
IEquatable
See if two objects are equals
IS a relationship
In an “is a” relationship, the derived class is clearly a kind of the base class. For example, a class named Manager represents an “is a” relationship with a base class named Employe because a manager is an employer
Association relationship
Requirement 2 is an interesting requirement (Manager uses a swipe card to enter XYZ premises). In this requirement, the manager object and the swipe card object use each other but they have their own object life time. In other words, they can exist without each other. The most important point in this relationship is that there is no single owner
Aggregation relationship
The third requirement from our list (Manager has workers who work under him) denotes the same type of relationship like association but with a difference that one of them is an owner. So as per the requirement, the Manager object will own Worker objects.
The child Worker objects can not belong to any other object. For instance, a Worker object cannot belong to a SwipeCard object.
But… the Worker object can have its own life time which is completely disconnected from the Manager object. Looking from a different perspective, it means that if the Manager object is deleted, the Worker object does not die.
This relationship is termed as an “Aggregation” relationship.
Composition relationship
The last two requirements are actually logically one. If you read closely, the requirements are as follows:
Manager has the responsibility of ensuring that the project is successful.
Manager’s salary will be judged based on project success.
Below is the conclusion from analyzing the above requirements:
Manager and the project objects are dependent on each other.
Interface vs. Abstract
Abstract classes
- May contain implementation code
- A class may inherit from a single base class
- Members have access modifiers
- May contain fields, properties, constructors, destructors, methods, events and indexers
Abstract classes
- May not contain implementation code
- A class may inherit any number of interfaces
- Members are automatically public
- May only contain fields, properties, methods, events and indexers