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 }