More Basics and OOP rules Flashcards
Members of ‘Console’ class
void Write( value ):
It receives a value as parameter and displays the same value in Console (Command-Prompt window).
void WriteLine( value ):
It receives a value as parameter and displays the same value in Console and also moves the cursor to the next line, after the value.
void ReadKey( ):
It waits until the user presses any key on the keyboard.
It makes the console window wait for user’s input.
void Clear( ):
It clears (make empty) the console window.
After clearing the screen, you can display output again, using Write( ) or WriteLine( ) methods.
string ReadLine( ):
It accepts a string value from keyboard (entered by user) and returns the same
It always returns the value in “string” type only.
Even numbers (digits) are treated as strings.
Variable
Variable is a named memory location in RAM, to store a particular type of value, during the program execution.
All Variables will be stored in Stack.›For every method call, a new “Stack” will be created.
The variable’s value can be changed any no. of times.
The variables must be declared before its usage.
The variables must be** initialized before reading its value.**
Variable’s data type should be specified while declaring the variable; it can’t be changed later.
The stack (along with its variables) will be deleted automatically, at the end of method execution.
Primitive Types:
(sbyte, byte, short, ushort, int, uint, long, ulong, float, double, decimal, char, bool)
- Strictly stores single value.
- Primitive Types are basic building blocks of non-primitive types.
Non-Primitive Types:
(string, Classes, Interfaces, Structures, Enumerations)
- Stores one or more values.
- Usually contains multiple members.
Default Literals
You can get the default value of respective type using the following syntax.
default(type)
Example: default(int) = 0
Basic informations to remeber about OOP
Object is a programmatic representation of a person or thing.
All objects are created based on classes; stored in ‘heap’.
For each application execution, a new heap will be created (and only one).
All reference variables (local variables of methods) are stored in stack. For each method call, a new stack will be created.
Method is a collection of statements to perform some operation / calculation.
Class supports two access modifiers: ‘internal’ and ‘public’.
Class supports four modifiers: ‘static’, ‘abstract’, ‘sealed’ and ‘partial’.
Objects stores actual data (group of fields) & can access methods of class.
A reference variable stores address of an (only one) object.
Access Modifiers of Fields
private
public
internal
protected
protected private
protected internal
Constant Fields
Constant Fields are like static fields, that are common to all objects of the class.
We can’t change the value of constant field.
Constant Fields are accessible with class name [not with object].
Constant Fields are not stored in the object; will not be stored anywhere.
Constant Fields will be replaced with its value, while compilation; so it will not be stored anywhere in memory.
Constant Fields must be initialized, in line with declaration (with a literal value only).
Constants can also be declared as ‘local constants’ (in a method).
AccessModifier const type FieldName = value;
Readonly Fields
Readonly Fields are like instance fields, that is stored in every object, individually.
We can’t change the value of readonly field.
Readonly Fields are accessible with reference variable [with object].
Readonly Fields must be initialized, either “in-line with declaration” [or] “in the constructor”.
AccessModifier readonly DataType FieldName = value;
Basic information about OOP
Fields are variables that are declared in the class; but stored in objects.
Access modifiers of fields: private, protected, private protected, internal, protected internal, public
Modifiers of fields: static, const, readonly
Instance fields are individual for each object; Static fields are common (one-time) for all objects.
Constants must be initialized along with declaration; Readonly fields must be initialized either ‘along with declaration’ or in ‘instance constructor’.
Encapsulation
Encapsulation (enkapsulacja) w C# to jedna z podstawowych zasad programowania obiektowego (OOP), która polega na ukrywaniu szczegółów implementacji klasy i umożliwieniu dostępu do danych oraz metod tylko poprzez zdefiniowane interfejsy.
Modyfikatory dostępu
Pola prywatne i właściwości publiczne
Ukrywanie implementacji
Benefits:
Modularity
Hiding implementation details
Data Integrity
Implemented using:
Private fields &
Public properties or public methods
Method Overloading
Writing multiple methods with same name in the same class, with different parameters.
Caller would have several options, while calling a method.
Difference between parameters of all the methods that have same name, is MUST.
MethodName( )
MethodName( int )
MethodName( string )
MethodName( int, string )
MethodName( string , int)
MethodName( string , string, int)
etc.
Parameter Modifiers
Default [No keyword]
ref
out
in
params
Most impotant facts about methods
Method is a part of class, that contains collection of statements to do some process.
Access modifiers of Methods: private, protected, private protected, internal, protected internal, public.
Modifiers of Methods: static, virtual, abstract, override, new, partial, sealed
For each method call, a new stack will be created; all local variables and parameters of the method will be stored in that stack; will be deleted automatically at the end of method execution.
In instance methods, the ‘this’ keyword refers to ‘current object, based on which the method is called’.
Instance methods can access & manipulate instance fields & static fields; Static methods can access only static fields.
But static method can create an object for the class; then access instance fields through that object.
Using named arguments , you can change order of parameters while calling the method.
Method Overloading is ‘writing multiple methods with same name in the same class with different set of parameters’.
The ‘ref’ parameter is used to receive value into the method and also return some value back to the method caller; The ‘out’ parameter is only used to return value back to the method caller; but not for receiving value into the method.
Type Conversion
‘Type Conversion’ is a process of convert a value from one type (source type) to another type (destination type).
Eg: int -> long
- Implicit Casting
(from lower-numerical-type to higher-numerical-type) - Explicit Casting
(from higher-numerical-type to lower-numerical-type) - Parsing / TryParse
(from string to numerical-type) - Conversion Methods
(from any-primitive-type to any-primitive-type and also string along with other types such as DateTime, Base64 etc.)