More Basics and OOP rules Flashcards

1
Q

Members of ‘Console’ class

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Variable

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Primitive Types:

A

(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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Non-Primitive Types:

A

(string, Classes, Interfaces, Structures, Enumerations)

  • Stores one or more values.
  • Usually contains multiple members.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Default Literals

A

You can get the default value of respective type using the following syntax.

default(type)

Example: default(int) = 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Basic informations to remeber about OOP

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Access Modifiers of Fields

A

private
public
internal
protected
protected private
protected internal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Constant Fields

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Readonly Fields

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Basic information about OOP

A

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’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Encapsulation

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Method Overloading

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Parameter Modifiers

A

Default [No keyword]
ref
out
in
params

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Most impotant facts about methods

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Type Conversion

A

‘Type Conversion’ is a process of convert a value from one type (source type) to another type (destination type).
Eg: int -> long

  1. Implicit Casting
    (from lower-numerical-type to higher-numerical-type)
  2. Explicit Casting
    (from higher-numerical-type to lower-numerical-type)
  3. Parsing / TryParse
    (from string to numerical-type)
  4. Conversion Methods
    (from any-primitive-type to any-primitive-type and also string along with other types such as DateTime, Base64 etc.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Implicit Casting

A

The ‘lower-numerical type’ can be automatically (implicitly) converted into ‘higher-numerical type’.Explicit Casting

can be automaticlly, we can NOT lose data!

17
Q

Explicit Casting

A

We can manually convert a value from one data type to another data type, by specifying the destination data type within brackets, at left-hand-side of the source value.

we need to cast, we DO can lose data

18
Q

Parse

A

The string value can be converted into any numerical data type, by using “Parsing” technique.

Eg: string à int

The source value must contain digits only; shouldn’t contain spaces, alphabets or special characters.

If the source value is invalid, it raises FormatException.

19
Q

TryParse

A

The string value can be converted into any numerical data type, by using “TryParse” technique (same as “parse”); but it checks the source value before attempting to parse.

Eg: string -> int

If the source value is invalid, it returns false; It doesn’t raise any exception in this case.

If the source value is valid, it returns true [indicates conversion is successful]

string input = “123”;
int result;
bool success = int.TryParse(input, out result);

It avoids FormatException.

20
Q

Conversion Methods

A

Conversion method is a pre-defined method, which converts any primitive type (and also ‘string’) to any other primitive type (and also ‘string’).

Eg: string -> int and int -> string

The System.Convert is a class, which contains a set of pre-defined methods.

It raises FormatException, if the source value is invalid.

For each data type, we have a conversion method.

All conversion methods are static methods.

type destinationVariable = Convert.ConversionMethod (SourceValue )

21
Q

Keys about conversion

A

For all the possible cases of ‘implicit casting’ and ‘explicit casting’, it is preferred to use ‘explicit casting’ or ‘conversion methods’ always.

For conversion from ‘string’ to ‘numerical type’, use TryParse, instead of ‘Parse’; as ‘TryParse’ avoids exceptions.

For conversion of value from any-type to any-type, use conversion method.

22
Q

Constructors

A

Special method of class, which contains initialization logic of fields.

Constructor initializes the fields and also contains the additional initialization logic (if any).

23
Q

Rules of Constructors

A

Constructor’s name should be same as class name.

Constructor is recommended to be “public” member or “internal” member;

if it is a “private member”, it can be called within the same class only; so you can create object of a class only inside the same class; but not outside the class.

Constructor can have one or more parameters.

Constructor can’t return any value; so no return type.

A class can have one or more constructors; but all the constructors of the class must have different types of parameters.

24
Q

Instance Constructor

A

Initializes instance fields.

Executes automatically every time when a new object is created for the class.

“private” by default; We can use any of access modifiers.

Can contain any initialization logic, that should be executed every time when a new object is created for the class.

25
Q

Static Constructor

A

Initializes static fields.

Executes only once, i.e. when first object is created for the class or when the class is accessed for the first time during the execution of Main method.

“public” by default; Access modifier can’t be changed.

Can contain any initialization logic, that should be executed only once i.e. when a new object is created for the class.

26
Q

Implicit Constructor (after compilation)

A

If there is a class without constructor, then the constructor automatically provides an empty constructor, while compilation, which initializes nothing. It is called as “Implicit Constructor” or “Default Constructor”.
It is just to satisfy the rule “Class should have a constructor”.

27
Q

Explicit Constructor (While coding)

A

The constructor (parameter-less or parameterized) while is created by the developer is called as “Explicit Constructor”.

In this case, the C# compiler doesn’t provide any implicit constructor.

28
Q

Constructor Overloading

A

Write multiple constructors with same name in the class, with different set of parameters (just like ‘method overloading’).

It is recommended to write a parameter-less constructor in the class, in case of constructor overloading.

29
Q

Object Initializer

A

Special syntax to initialize fields / properties of class, along with creating the object.

Executes after the constructor.

It is only for initialization of fields / properties, after creating object; it can’t have any initialization logic.

Use ‘object initializer’ when:

there is no constructor present in the class; but you want to initialize fields / properties.

(or) there is a constructor; but it is meant for initializing other set of fields, other than the fields that you want to initialize.

30
Q

Keys about constructor

A

‘Instance constructor’ initializes ‘instance fields’; but also can access ‘static fields’.

‘Static constructor’ initializes ‘static fields’; can’t access ‘instance fields’.

Default (empty constructor) is provided automatically by C# compiler, if the developer creates a class without any constructor.

It is always recommended to write a parameter-less constructor first, if you are creating parameterized constructor.

Use ‘object initializer’, if you want to initialize desired fields of an object, as soon as a new object is created.