DevDiary



Car.cs

Object Constructor

The car class has three fields and a constructor.

In the main program, a car object is instantiated and the parameters for the fields are passed in.

The console is used to show these values.



Instantiate Car Object

Console: Display Car Object Details



Quote.cs

Instance and Static Method

The quote class contains two methods.

Sartre() is an instance method and Wittgenstein() is a static method.

They are both void because the methods do not return a value.

An instance of the quote class is created and then the method is called on that instance.

The static method doesn't need to be called on an instance of the class.

The console displays the contents of the method: quotes.



Call Instance and Static Method

Console: Display Quotes



MathOps.cs

Method Overloading

The MathOps class contains

Multiple methods can have the same name as long as the number and/or type of parameters are different.

A method signature is a unique identification of a method for the C# compiler.

The signature consists of a method name and the type and kind (value, reference, or output) of each of its formal parameters.

Method signature does not include the return type.



Call Method

Console: Display Results of Method



Author.cs

Inheritance

In this example there is a base class (Author) and a derived class (Book).

The Book class inherits from the Author class.

This means that the Book class (derived class) can access the fields, proprties and methods defined in Author.

The console shows that an instance of the Book class can access the name field defined in the Author class.



Create Instance of Book Class

Console: Access Fields



Frog.cs

Create Array of Frog Objects



Console: Displays Mean Frog Tongue Length

Calculation

Six frog objects are created with different tongue lengths.

The GetAverageTongueLength static method is called and the array is passed in.

(4 + 3 + 6 + 2 + 4 + 5) / 6

24 / 6 = The average legth is 4.



Person.cs

Fields and Properties



Polygon.cs

Square: Polygon



Program

Console



Sequence Detector

Repeat Detector: Sequence Detector



Program

Console



Animal.cs

Abstract Base Class

This example is an abstract base class.

For an abstract property or method, every derived class must overide it.

The boolean property UnihemisphericSleep has a protected set accessor.

This means that code inside the class or in it's derrived classes can access it.

Note that the property NoOfEyes is not abstract which means it doesn't need to be defined in the derrived classes yet instances of derrived classes like dog and dolphin still have this property.



Dog Class

Dolphin Class



Program

Console



C# Interface Rules

Interface can contain declarations of method, properties, indexers, and events.

Interface cannot include private, protected, or internal members.

All the members are public by default.

Interface cannot contain fields, and auto-implemented properties.

C# Interface Rules

Classes can have multiple interfaces, but classes cannot inherit multiple classes.

Classes inheriting from more than one class is known as multiple-inheritance.

C# does not allow multiple-inheritance

Below shows an example of an Interface (IPikachu.cs).



IPokemon (Interface)

Pikachu.cs



Program

Console



FizzBuzz Algorithm

Print First 31 Numbers



Pass 31 to FizzBuzz Method

FizzBuzz Rules

If the number is a multiple of 3 it is replaced with the string "FIZZ".

If the number is a multiple of 5 it is replaced with the string "BUZZ".

If the number is a multiple of 3 AND 5, it is replaced with the string "FIZZ BUZZ".



Fib(): Calculate Fibonacci Numbers

calcFib(): Memoization



Pass 34 to "Fib" Method

Calculate Fibonacci Numbers

If n < 2, Fib() returns the the vaue of n.

If n > 2, Fib() creates an empty array of doubles and passes n and the array to the method calcFib().

If n < 2, calcFib() returns the the vaue of n.

If the desired value exists in the array already, calcFib returns the value.

If the value needs to be calculated calcFib() performs a recursive calculation such as calcFib(3) = calcFib(2) + calcFib(1).

It also saves the calculated value in the array so it can be looked up later, if needed.



Bubble Sort: Call method

Bubble Sort: Pass array to sort2 method



Bubble Sort: For-Loop and If

Bubble Sort: Calculation

For the 1st cycle, the value "4" and the array "4, 2, 3, 1" is passed in.

The for-loop executes 4 times.

The first and second elements of the array are compared, "4, 2, 3, 1" and if the number on the left is larger the elements are swapped.

In this case it is true so the array becomes, "2, 4, 3, 1"

This effectively moves the largest number in the array from the left to the right resulting in the array, "2, 3, 1, 4"

When the 2nd cycle of "sort2" runs the the value "3" and the array "2, 3, 1, 4" is passed in.

After this for-loop the array becomes "2, 1, 3, 4", essentially moving the second largest number in the array to the right.

The process is continued until the array becomes, "1, 2, 3, 4".



Bubble Sort: Print Array

Bubble Sort: DRY Principle

This static method takes in an array but doesn't return a value.

It just prints the array to the console.

I created it because the code was repeating.



Bubble Sort: 4, 2, 3, 1

Bubble Sort: 3, 6, 2, 5



If Statement

Ternary Operator



Lambda Expression

Definition

C# delegates are similar to pointers to functions, in C or C++.

A delegate is a reference type variable that holds the reference to a method.

The reference can be changed at runtime.

Delegates are especially used for implementing events and the call-back methods.

All delegates are implicitly derived from the System.



Lambda Expression

Example