C# Fundamentals Flashcards

1
Q

Value Types

A

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.

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

Reference Types -

A

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

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

Class vs Struct IN C#

A

We use struct to create a value type and class to create a reference type.

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

Access Modifiers

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

Static keyword

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

STRUCT

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

ENUM

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

Immutability

A

Value types are usually immutable. Once you create a value, you cannot change the value.

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

Array

A

Manage a collection of variables

  • they are fixed size
  • they’re always 0 indexed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Methods

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

Fields

A

Fields are variables of a class. They sometimes called the state of a class

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

Properties

A

A property can define a get and/or set accessor

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

Delegates

A

Delegates are used to pass methods as arguments to other methods

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

Events

A

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.

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

Exception

A
  • 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

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

Using

A
  • 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
    }
17
Q

Pillars for OOP

A

Encapsulation
Polymorphism
Inheritance
Abstraction

18
Q

Inheritance

A

Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes

Use : to show inheritance

19
Q

virtual

A

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

20
Q

Overriden

A

You need a virtual method to override a method

21
Q

Polymorphism

A

many shapes
object can behave differently depending on their type
Polymorphism “Is a” type relationship

22
Q

Abstract

A
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.
23
Q

Interface

A
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.
24
Q

IDisposable

A

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
}
25
Q

IEnumerable

A

Exposes an enumerator, which supports a simple iteration over a non-generic collection. supports iteration (foreach)

26
Q

INotifyPropertyChange

A

Raises events when properties changed. Used often in wpf and xaml

27
Q

IComparable

A

compares two objects

28
Q

IEquatable

A

See if two objects are equals

29
Q

IS a relationship

A

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

30
Q

Association relationship

A

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

31
Q

Aggregation relationship

A

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.

32
Q

Composition relationship

A

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.

33
Q

Interface vs. Abstract

A

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