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