You are on page 1of 70

16/05/2019

CodeCademy C#
Hello World
Pokemon Go and the Stack Overflow website were built with frameworks that can be run with C#
(Unity and ASP.NET, respectively). C# has you define the type of each data in a program. Similar
to Java.

Hello World Program:

using System;

namespace HelloWorld {
class Program {
static void Main() {
Console.WriteLine("Hello World!"); // to write to console
}
}
}

The command Console.ReadLine() captures text that a user types into the console.
This text can be stored to a variable, usually a string. This variable can then be used to format a
string for output. Code:

using System;

namespace GettingInput
{
class Program
{
static void Main()
{
Console.WriteLine("How old are you?"); // print to console
string input = Console.ReadLine(); // input data
Console.WriteLine($"You are {input} years old!"); // format text
} // using input variable and print to console
}
}

To run interactive code type the command: “dotnet run”

Comments are the same as in java, // for one line, /* and */ for multiline
// single line comment
/*Multi-
Line comment
*/
C# technologies are fast: A software company called Raygun built their application using Node.js,
a framework for JavaScript. When their app started to slow down, they switched to a .NET, a
framework for C#. Their performance skyrocketed. As CEO John Daniel puts it, “using the same
size server, we were able to go from 1,000 requests per second...with Node.js, to 20,000 requests
per second with .NET Core.” They increased throughput by 2,000%.
In 2018, Github ranked C# as the sixth most popular programming language and StackOverflow
ranked it eighth.
C# is employable: Thanks to good design and the popularity of frameworks supporting the
language, C# can get you access to a lot of great jobs. In the 2019 HackerRank Developer Skills
Report, expertise in two C#-compatible frameworks, ASP.NET and .NET Core, were among the
top ten most sought-after by managers.

C# is used to make interactive websites, mobile apps, video games, augmented and virtual reality
(AR and VR), back-end services, and desktop applications.
.NET generally refers to the family of programs and commands that let you make applications with
C#
Data Types and Variables
C# is strongly-typed, so it requires us to specify the data types that we’re using. It is also statically-
types, which means it will check that we used the correct types before the program even runs.
Both language features are important because they help write scalable code with fewer bugs.

Data types used with C#:

Declaring variables is just like in java, where the variable can be initialised and then assigned later
like:
int myAge;
myAge = 32;

Or can be initialised and assigned in one line:


string countryName = “Netherlands”;
3/6 Code:
using System;

namespace Form{
class Program{
static void Main(string[] args){
// Create Variables
string name = "Shadow";
string breed = "Golden Retriever";
int age = 5;
double weight = 65.22;
bool spayed = true;
// Print variables to the console
Console.WriteLine(name);
Console.WriteLine(breed);
Console.WriteLine(age);
Console.WriteLine(weight);
Console.WriteLine(spayed);
} // writeline puts newline at end
}
}

The convention to variables is camelCase, and they should only contain userscores, letters and
digits.

You can’t use Reserved keywords for variables, such as string or static. All statements should end
with a semicolon;

Some data types can be converted into other data types implicitly, but only if no data will be lost.
For example an integer can be converted into a double type as it would not lose any data:
int myInt = 3;
double MyDouble = myInt;
Others will require explicit conversion, which requires a cast operator to convert a data type into
another one, like in Java:
double myDouble = 3.2;
int myInt = (int) myDouble;

It is also possible to convert data types using built-in methods. For most data types, there is a
Convert.To... method, like Convert.ToString and Convert.ToDouble.

5/6 Code:
using System;

namespace FavoriteNumber {
class Program {
static void Main(string[] args) {
Console.Write("Enter your favorite number!: "); // no new line
int faveNumber = Convert.ToInt32(Console.ReadLine());
}
}
}

This code can explicitly convert input string into an int

Working with Numbers


To define a variable with the type decimal, you would write it as follows:
decimal variableName = 489872.76m;
Don’t forget the m character after the number, This tells C# that we’re defining a decimal and not a
double.
To define a variable with the type float, you would write it as follows:
float variableName =0.58f;
Don’t forget the f character after the number, This tells C# that we’re defining a float and not a
double

A float is the fastest to process, as it is 4 bytes, however is the least precise. A double is more
precise than a float but less than a decimal, it is 8 bytes. A decimal is 16 bytes and is used for
financial applications as it is the most precise. The double type is usually the best choice as it is
well rounded.

When to use C# floats:


https://stackoverflow.com/questions/407970/when-to-use-a-float
Short answer: You only have to use a float when you know exactly what you're doing and why.

Long answer: floats (as opposed to doubles) aren't really used anymore outside 3D APIs as far
as I know. Floats and doubles have the same performance characteristics on modern CPUs,
doubles are somewhat bigger and that's all. If in doubt, just use double.

Oh yes, and use decimal for financial calculations, of course.

2/8 Code:
using System;

namespace Numbers {
class Program {
static void Main(string[] args) {
int pizzaShops = 4332;
int totalEmployees = 86928;
decimal revenue = 390819.28;
Console.WriteLine(pizzaShops);
Console.WriteLine(totalEmployees);
Console.WriteLine(revenue);
}
}
}

The type of a result of an operation will depend on the data types:


(5 / 3) // will return a integer (floored)
(5 / 3.0) // will return a double
(5.0 / 3.0) // will return a double

C# follows the order of operations: https://en.wikipedia.org/wiki/Order_of_operations

3/8 Code:
using System;
namespace PlanetCalculations {
class Program{
static void Main(string[] args) {
// Your Age
int userAge = 17;

// Length of years on Jupiter (in Earth years)


double jupiterYears = 11.86;

// Age on Jupiter
double jupiterAge = userAge / jupiterYears;

// Time to Jupiter
double journeyToJupiter = 6.142466;

// New Age on Earth


double newEarthAge = userAge + journeyToJupiter;

// New Age on Jupiter


double newJupiterAge = newEarthAge / jupiterYears;

// Log calculations to console


Console.WriteLine(newEarthAge);
Console.WriteLine(newJupiterAge);
}
}
}

Like in Java ++ means increment by 1 and – means decrement by 1. To increment and decrement
by other numbers use += and -=.
4/8 Code:
using System;
namespace MakingProgress {
class Program {
static void Main(string[] args) {
// declare steps variable
int steps = 0;
// Two steps forward
steps += 2;
// One step back
steps--;
// Print result to the console
Console.WriteLine(steps);
}
}
}

C#’s Modulo operator, %, is the same as in Java and returns a remainder.


5/8 Code:
using System;
namespace ClassTeams {
class Program {
static void Main(string[] args) {
// Number of students
int students = 18;
// Number of students in a group
int groupSize = 3;
// Does groupSize go evenly into students?
Console.WriteLine(students % groupSize);
}
}
}
Some Built-In Methods:
Math.Abs() // finds absolute value of a number
Math.Sqrt() // finds square root of a number, can only take positives
Math.Floor() // round given float, double or decimal down to nearest
whole number
Math.Min() // returns smaller of 2 numbers
Math.Max() // returns larger of 2 numbers

6/8 Code:
using System;
namespace LowestNumber {
class Program {
static void Main(string[] args) {
// Starting variables
int numberOne = 12932;
int numberTwo = -2828472;
// Use built-in methods and save to variable
double numberOneSqrt = Math.Floor(Math.Sqrt(numberOne));
// Use built-in methods and save to variable
double numberTwoSqrt = Math.Floor(Math.Sqrt(Math.Abs(numberTwo)));
// Print the lowest number
Console.WriteLine(Math.Min(numberOneSqrt, numberTwoSqrt));
}
}
}

More Built-In methods:


Math.Pow() // returns a specified number raised to the specified power
Math.Ceiling() // rounds given float, double or decimal up to nearest
whole number

7/8 Code:
using System;
namespace DocumentationHunt{
class Program{
static void Main(string[] args){
double numberOne = 6.5;
double numberTwo = 4;
// Raise numberOne to the numberTwo power
Console.WriteLine(Math.Pow(numberOne, numberTwo));
// Round numberOne up
Console.WriteLine(Math.Ceiling(numberOne));
// Find the larger number between numberOne and numberTwo
Console.WriteLine(Math.Max(numberOne, numberTwo));
}
}
}

Working with Text


To include quotes in a string you use an escape sequence. An escape sequence places a
backslash \ before the inner quotation marks, so the program doesn’t read them accidentally as
the end of the sequence.
string withSlash = "Ifemelu said, \"Hello!\"";
To create a new line use the character combination \n:
string newLine = "Ifemelu walked \n to the park.";

Code 2/8:
using System;
namespace PrideAndPrejudice{
class Program{
static void Main(string[] args){
// First string variable
string firstSentence = "It is a truth universally acknowledged,
that a single man in possession of a good fortune, must be in want of a
wife.";
// Second string variable
string firstSpeech = "\"My dear Mr. Bennet,\" said his lady to him
one day, \"have you heard that Netherfield Park is let at last?\"";
// Print variable and newline
Console.WriteLine(firstSentence);
Console.WriteLine("\n");
Console.WriteLine(firstSpeech);
}
}
}

String concatenation is when we combine strings using the addition symbol, +.


Console.WriteLine("Your favorite musician is " + yourFaveMusician + "
and mine is " + myFaveMusician + ".");
If we want to concatenate a string with something that is another data type, C# will implicitly
convert that value to a string.

String Interpolation enables us to insert our variable values and expressions in the middle of a
string, without having to worry about spaces and punctuation.
string yourFaveMusician = "David Bowie";
string myFaveMusician = "Solange";
Console.WriteLine($"Your favorite musician is {yourFaveMusician} and
mine is {myFaveMusician}.");
string variables are surrounded by braces.

4/8 Code:
using System;
namespace StoryTime{
class Program{
static void Main(string[] args){
// Declare the variables
string beginning = "Once upon a time,";
string middle = "The kid climbed a tree";
string end = "Everyone lived happily ever after.";
// Interpolate the string and the variables
string story = $"{beginning} {middle} {end}";
// Print the story to the console
Console.WriteLine(story);
}
}
}

Built-In Methods to get information about strings:


.Length // to get length of string
.IndexOf() // to find position of a first instance of a specific
character or substring, returns -1 if not found, and 0 if empty string.

5/8 Code:
using System;
namespace PasswordCheck {
class Program {
static void Main(string[] args) {
// Create password
string password = "a92301j2add";
// Get password length
int passwordLength = password.Length;
// Check if password uses symbol
int passwordCheck = password.IndexOf("!");
// Print results
Console.WriteLine($"The user password is {password}. It's length
is {passwordLength} and it receives a {passwordCheck} check.");
}
}
}

.Substring() // to return a substring of a string keeping original


intact, if 1 argument then continue to end of string.

Bracket notation is a style of syntax that uses brackets and an integer value to identify a particular
value in a collection, like in a string.

6/8 Code:
using System;
namespace NameGrab{
class Program{
static void Main(string[] args){
// User Name
string name = "Farhad Hesam Abbasi";
// Get first letter
int charPosition = name.IndexOf("F");
char firstLetter = name[charPosition];
// Get last name
charPosition = name.IndexOf("Abbasi");
string lastName = name.Substring(charPosition);
// Print results
Console.WriteLine($"{firstLetter}. {lastName}");
}
}
}

Change case of our strings using .ToUpper() and .ToLower(). Returns new string does not modify
original.

7/8 Code:
using System;
namespace MovieScript{
class Program{
static void Main(string[] args){
// Script line
string script = "Close on a portrait of the HANDSOME PRINCE -- as
the BEAST'S giant paw slashes it.";
// Get camera directions
int charPosition = script.IndexOf("Close");
int length = "Close on".Length;
string cameraDirections = script.Substring(charPosition, length);
// Get scene description
charPosition = script.IndexOf("a portrait");
string sceneDescription = script.Substring(charPosition);
// Make camera directions uppercase
cameraDirections = cameraDirections.ToUpper();
// Make scene description lowercase
sceneDescription = sceneDescription.ToLower();
// Print results
Console.WriteLine($"{cameraDirections}\n{sceneDescription}");
}
}
}

Understanding Logic in C# 16:54


In C#, we can represent Boolean values using the bool data type. Booleans, unlike numbers or
strings, can only have two values: true and false.

To define a value as a Boolean, you define the data type as bool. Then write the variable name
and set it equal to the value, either true or false. We use words true and false to represent
Boolean values.

2/6 Code:
using System;
namespace BooleanDataTypes{
class Program{
static void Main(string[] args){
bool answerOne = true;
bool answerTwo = false;
}
}
}

Comparison operators in C# are the same as in Java.

3/6 Code:
using System;
namespace ComparisonOperators{
class Program{
static void Main(string[] args){
double timeToDinner = 4;
double distance = 95;
double rate = 30;
double tripDuration = distance / rate;
bool answer = tripDuration <= timeToDinner;
Console.WriteLine(answer);
}
}
}

Logical operators are the same in C# as in Java, &&, ||, !.


5/6 Code:
using System;
namespace LogicalOperators{
class Program{
static void Main(string[] args){
bool beach = true;
bool hiking = false;
bool city = true;
bool yourNeeds = beach && city;
bool friendNeeds = beach || hiking;
bool tripDecision = yourNeeds && friendNeeds;
Console.WriteLine(tripDecision);
}
}
}

Conditional Statements
The if else if else statement is the same as in java
2/7 Code:
using System;
namespace IfStatement{
class Program{
static void Main(string[] args){
int socks = 6;
if (socks <= 3){
Console.WriteLine("Time to do laundry!");
}
}
}
}

3/7 Code:
using System;
namespace IfElseStatement{
class Program{
static void Main(string[] args){
int people = 12;
string weather = "bad";
if (people<=10 && weather == "nice"){
Console.WriteLine("SaladMart");
} else {
Console.WriteLine("Soup N Sandwich");
}
}
}
}

4/7 Code:
using System;
namespace ElseIfStatement{
class Program{
static void Main(string[] args){
int guests = 6;
if (guests==6){
Console.WriteLine("Catan");
} else if (guests==3){
Console.WriteLine("Innovation");
} else if (guests==0){
Console.WriteLine("Solitaire");
}
}
}
}

The switch statement is the same as in Java


5/7 Code:
using System;
namespace SwitchStatement{
class Program{
static void Main(string[] args){
Console.WriteLine("Pick a genre");
string genre = Console.ReadLine();
switch (genre){
case "Drama":
Console.WriteLine("Citizen Kane");
break;
case "Comedy":
Console.WriteLine("Duck Soup");
break;
case "Adventure":
Console.WriteLine("King Kong");
break;
case "Horror":
Console.WriteLine("Psycho");
break;
case "Science Fiction":
Console.WriteLine("2001: A Space Odyssey");
break;
default:
Console.WriteLine("No movie found");
break;
}
}
}
}

Ternary operator is the same as in Java


6/7 Code:
using System;
namespace TernaryOperator{
class Program{
static void Main(string[] args){
int pepperLength = 4;
string message = (pepperLength>=3.5) ? "ready!" : "wait a little
longer" ;
Console.WriteLine(message);
}
}
}

Method Calls and Input


A method is a reusable set of instructions that perform a specific task. Developers use methods to
make their code organized, maintainable, and repeatable.
Code 2/10:
using System;
namespace CallAMethod{
class Program{
static void Main(string[] args){
string msg = "Yabba dabba doo!";
Math.Min(2,3);
Console.WriteLine(msg);
msg.Substring(0, 1);
}
}
}

Code 3/10:
using System;
namespace CaptureOutput{
class Program{
static void Main(string[] args){
string designer = "Anders Hejlsberg";
int indexOfSpace = designer.IndexOf(" ");
string secondName = designer.Substring(indexOfSpace);
Console.WriteLine(secondName);
}
}
}

In C#, it’s convention to use PascalCase to name your method. The name starts with an
uppercase letter and each word following begins with an uppercase as well. It’s not required in C#,
but it makes your code easier to read for other developers.
Code 4/10:
using System;
namespace DefineAMethod{
class Program{
static void Main(string[] args){
VisitPlanets();
}
static void VisitPlanets(){
Console.WriteLine("You visited many new planets...");
}
}
}

Code 5/10:
using System;
namespace DefineParameters{
class Program{
static void Main(string[] args){
VisitPlanets(3);
VisitPlanets(0);
VisitPlanets(154);
}
static void VisitPlanets(int numberOfPlanets){
Console.WriteLine($"You visited {numberOfPlanets} new
planets...");
}
}
}

A parameter’s scope is within its method, which means that the name (message in this case) is
only valid within its method. If the parameter name is used outside the method, it has no meaning,
so it throws an error.
Code 6/10:
using System;
namespace ANoteOnParameters{
class Program{
static void Main(string[] args){
VisitPlanets(3);
VisitPlanets(4);
VisitPlanets(5);
Console.WriteLine(numberOfPlanets);
}
static void VisitPlanets(int numberOfPlanets){
Console.WriteLine($"You visited {numberOfPlanets} new
planets...");
}
}
}

Code 7/10:
using System;
namespace OptionalParameters{
class Program{
static void Main(string[] args){
VisitPlanets(3);
VisitPlanets(4);
VisitPlanets(5);
VisitPlanets();
}
static void VisitPlanets(int numberOfPlanets = 0){
Console.WriteLine($"You visited {numberOfPlanets} new
planets...");
}
}
}

Code 8/10:
using System;
namespace NamedArguments{
class Program{
static void Main(string[] args){
VisitPlanets();
VisitPlanets(numberOfPlanets: 2);
VisitPlanets(numberOfPlanets: 2, name: "asdf");
}
static void VisitPlanets(
string adjective = "brave",
string name = "Cosmonaut",
int numberOfPlanets = 0,
double gForce = 4.2) {
Console.WriteLine($"Welcome back, {adjective} {name}.");
Console.WriteLine($"You visited {numberOfPlanets} new
planets...");
Console.WriteLine($"...while experiencing a g-force of {gForce}
g!");
}
}
}

Methods can be overloaded as in Java


Code 9/10:
using System;
namespace MethodOverloading{
class Program{
static void Main(string[] args){
NamePets("FADS", "adsf");
NamePets("dkafsj", "fdajs","dfaks");
NamePets();
}
static void NamePets(string n1, string n2){
Console.WriteLine($"Your pets {n1} and {n2} will be joining your
voyage across space!");
}
static void NamePets(string n1, string n2, string n3){
Console.WriteLine($"Your pets {n1}, {n2} and {n3} will be joining
your voyage across space!");
}
static void NamePets(){
Console.WriteLine("Aw, you have no spacefaring pets :(");
}
}
}

Method Output
Code 2/7:
using System;
namespace Return{
class Program{
static void Main(string[] args){
Console.WriteLine(DecoratePlanet("Jupiter"));
}
static string DecoratePlanet(string s){
return $"*.*.* Welcome to {s} *.*.*";
}
}
}

Code 3/7:
using System;
namespace ReturnErrors{
class Program{
static void Main(string[] args){
Console.WriteLine(DecoratePlanet("Mars"));
Console.WriteLine("Is Pluto really a dwarf...?");
Console.WriteLine(IsPlutoADwarf());
Console.WriteLine("Then how many planets are there in the
galaxy...?");
Console.WriteLine(CountThePlanets());
}

static string DecoratePlanet(string planet){


return $"*..*..* Welcome to {planet} *..*..*";
}

static bool IsPlutoADwarf(){


bool answer = true;
return answer;
}

static string CountThePlanets(){


return "8 planets, usually";
}
}
}

Using Out
Code 4/7:
using System;
namespace OutParameters{
class Program{
static void Main(string[] args){
string ageAsString = "102";
string nameAsString = "Granny";
int ageAsInt;
bool outcome;
outcome = Int32.TryParse(ageAsString, out ageAsInt);
Console.WriteLine(outcome);
Console.WriteLine(ageAsInt);
int nameAsInt;
bool outcome2;
outcome2 = Int32.TryParse(nameAsString, out nameAsInt);
Console.WriteLine(outcome2);
Console.WriteLine(nameAsInt);
}
}
}

Code 5/7:
using System;
namespace UsingOut{
class Program{
static void Main(string[] args){
string statement = "AKJDF";
Whisper(statement, out bool flag);
}
static string Whisper(string phrase, out bool wasWhisperCalled){
wasWhisperCalled = true;
return phrase.ToLower();
}
}
}

Code 6/7:
using System;
namespace OutErrors{
class Program{
static void Main(string[] args){
string statement = "GARRRR";
bool marker;
string murmur = Whisper(statement, out marker);
Console.WriteLine(murmur);
}

static string Whisper(string phrase, out bool wasWhisperCalled){


wasWhisperCalled = true;
return phrase.ToLower();
}
}
}

Code 7/7:
using System;
namespace ReviewMethodOutput{
class Program{
static void Main(string[] args){
// Define variables
string destination = "Neptune";
string galaxyString = "8";
int galaxyInt;
string welcomeMessage;
bool outcome;

// Call DecoratePlanet() and TryParse() here


welcomeMessage = DecoratePlanet(destination);
outcome = Int32.TryParse(galaxyString, out galaxyInt);
// Print results
Console.WriteLine(welcomeMessage);
Console.WriteLine($"Parsed to int? {outcome}: {galaxyInt}");

// Define a method that returns a string


static string DecoratePlanet(string planet){
return $"*..*..* Welcome to {planet} *..*..*";
}

// Define a method with out


static string Whisper(string phrase, out bool wasWhisperCalled){
wasWhisperCalled = true;
return phrase.ToLower();
}
}
}

Alternate Expressions
Expression-bodied definitions are for one-line methods, they work with any return type including
void. Can only be used when a method contains one expression.
Code 2/6:
static double DaysToRotations(double days) => days / 365;

static void Welcome(string planet) =>


Console.WriteLine($"Welcome to {planet}!");

We can use a method’s name like a variable and we can pass this variable to another method, like
Array.Exists(). Array.Find() takes an array and a method as arguments. It calls the method on
each element of the array and returns the first element for which the method returns true.
Code 3/6:
// Call Array.Find() and
// Pass in the array and method as arguments
string firstLongAdjective = Array.Find(adjectives,IsLong);

Lambda expressions are great for when you need to pass a method as an argument. It is an
anonymous method, has no name. Lambda expressions with one expression use the fat arrow but
not curly braces and no semicolon. Lambda expression with more than one expression use curly
braces and semicolons.
Code 4/6:
bool makesContact = Array.Exists(spaceRocks, (string s) => s ==
"meteorite");

We can remove the parameter type if it can be inferred and we can remove the parenthesis if
there is one parameter.
Code 5/6:
bool makesContact = Array.Exists(spaceRocks, s => s == "meteorite");

Arrays
Arrays order our data in a specific linear sequence.
In C#, arrays are a collection of values that all share the same data type. Use the new keyword
when initializing the array.
Code 2/8:
sing System;

namespace BuildingArrays{
class Program{
static void Main(string[] args){
string[] summerStrut;
summerStrut = new string[] {"Juice","Missing U", "Raspberry
Beret", "New York Groove", "Make Me Feel", "Rebel Rebel", "Despacito",
"Los Angeles"};
int[] ratings = new int[] {1,2,3,4,5,1,2,3};
}
}
}

Code 3/8:
using System;

namespace ArrayLength{
class Program{
static void Main(string[] args){

string[] summerStrut;

summerStrut = new string[] { "Juice", "Missing U", "Raspberry


Beret", "New York Groove", "Make Me Feel", "Rebel Rebel", "Despacito",
"Los Angeles" };

if (summerStrut.Length == 8){
Console.WriteLine("summerStrut Playlist is ready to go!");
} else if (summerStrut.Length > 8){
Console.WriteLine("Too many songs!");
} else {
Console.WriteLine("Add some songs!");
}

}
}
}

Code 4/8:
using System;

namespace AccessingArrays{
class Program{
static void Main(string[] args){
string[] summerStrut;

summerStrut = new string[] { "Juice", "Missing U", "Raspberry


Beret", "New York Groove", "Make Me Feel", "Rebel Rebel", "Despacito",
"Los Angeles" };

int[] ratings = { 5, 4, 4, 3, 3, 5, 5, 4 };

Console.WriteLine($"You rated the song {summerStrut[1]}


{ratings[1]} stars.");

}
}
}

Once we create an array, the size of it is fixed. However values can be changed/modified.
When we create the array with a known length but no known values, the array stores a default
value (0 for int, null for string).
Code 5/8:
using System;

namespace EditingArrays{
class Program{
static void Main(string[] args){
string[] summerStrut;

summerStrut = new string[] { "Juice", "Missing U", "Raspberry


Beret", "New York Groove", "Make Me Feel", "Rebel Rebel", "Despacito",
"Los Angeles" };

int[] ratings = { 5, 4, 4, 3, 3, 5, 5, 4 };

summerStrut[7] = "I Like It";

ratings[7] = 1;

}
}
}
There are many built in methods to use with arrays:
https://docs.microsoft.com/en-us/dotnet/api/system.array?view=netframework-4.8
Array.Sort() sorts an array
Array.IndexOf() returns the index of the first occurrence of a value
Array.Find() searches a one-dimensional array for a specific value or set of values that match a
certain condition and returns the first occurrence in the array.
Code 6/8:
using System;

namespace BuiltInMethods{
class Program{
static void Main(string[] args){
string[] summerStrut;

summerStrut = new string[] { "Juice", "Missing U", "Raspberry


Beret", "New York Groove", "Make Me Feel", "Rebel Rebel", "Despacito",
"Los Angeles" };

int[] ratings = { 5, 4, 4, 3, 3, 5, 5, 4 };

int firstThreeStarSong = Array.IndexOf(ratings, 3);


Console.WriteLine($"Song number {++firstThreeStarSong} is rated
three stars");

string songName = Array.Find(summerStrut, song => song.Length >


10);
Console.WriteLine($"The first song that has more than 10
characters in the title is {songName}.");

Array.Sort(summerStrut);
Console.WriteLine(summerStrut[0]);
Console.WriteLine(summerStrut[7]);
}
}
}
Code 7/8:
using System;

namespace DocumentationHunt{
class Program{
static void Main(string[] args){
string[] summerStrut;

summerStrut = new string[] { "Juice", "Missing U", "Raspberry


Beret", "New York Groove", "Make Me Feel", "Rebel Rebel", "Despacito",
"Los Angeles" };

int[] ratings = { 5, 4, 4, 3, 3, 5, 5, 4 };

string[] summerStrutCopy = new string[summerStrut.Length];


Array.Copy(summerStrut, summerStrutCopy,8);
Console.WriteLine(summerStrutCopy[0]);

Array.Reverse(summerStrut);
Console.WriteLine(summerStrut[0]);
Console.WriteLine(summerStrut[7]);

Array.Clear(summerStrut, 0, 8);
Console.WriteLine(summerStrut[0]);
}
}
}
Loops
Code 2/8:
using System;

namespace WhileLoop{
class Program{
static void Main(string[] args){
int emails = 20;

while (emails>0){
emails--;
Console.WriteLine("Deleting an email");
Console.WriteLine($"{emails} emails left");
}
Console.WriteLine("INBOX ZERO ACHIEVED");
}
}
}

Code 3/8:
using System;

namespace DoWhileLoop{
class Program{
static void Main(string[] args){
bool buttonClick = true;

do {
Console.WriteLine("BLARRRRRRRRR");
} while (!buttonClick);
Console.WriteLine("Time for a five minute break.");
}
}
}

Code 4/8:
using System;

namespace ForLoop{
class Program{
static void Main(string[] args){
for (int i=1; i<17; i++){
CreateTemplate(i);
}
}

static void CreateTemplate(int week){


Console.WriteLine($"Week {week}");
Console.WriteLine("Announcements: \n \n \n ");
Console.WriteLine("Report Backs: \n \n \n");
Console.WriteLine("Discussion Items: \n \n \n");
}
}
}

The foreach loop is used to iterate over collections, such as an array. These can be used with data
structures similar to an array. Often foreach loops are called collection loops. Use when you need
to perform a task for every item in a list or when the order needs to be maintained.
Code 5/8:
using System;

namespace ForEachLoop{
class Program{
static void Main(string[] args){
string[] todo = { "respond to email", "make wireframe", "program
feature", "fix bugs" };

foreach (string item in todo){


CreateTodoItem(item);
}
}

static void CreateTodoItem(string item){


Console.WriteLine($"[] {item}");
}
}
}

Code 6/8:
using System;

namespace ComparingLoops{
class Program{
static void Main(string[] args){
string[] websites = { "twitter", "facebook", "gmail" };

foreach(string website in websites){


Console.WriteLine(website);
}
}
}
}

Code 7/8:
using System;

namespace JumpStatements{
class Program{
static void Main(string[] args){
bool buttonClick = true;
int count=0;
do{
Console.WriteLine("BLARRRRR");
count++;
if (count>=3){
break;
}
} while(!buttonClick);
}
}
}

Basic Classes and Objects


The code for a class is usually put into a file of its own. This makes it easier to debug.
Code 2/12:
using System;

namespace BasicClasses{
class Forest{
}
}

using System;

namespace BasicClasses{
class Program{
static void Main(string[] args){
Forest f = new Forest();
}
}
}

A class can have fields


Code 3/12:
using System;

namespace BasicClasses{
class Forest{
public string name;
public int trees;
public int age;
public string biome;
}
}

using System;

namespace BasicClasses{
class Program{
static void Main(string[] args){
Forest f = new Forest();
f.name = "Far";
f.trees = 1349;
f.age = 3000;
f.biome = "Boreal Forest";
Console.WriteLine(f.name);
}
}
}
Properties are another type of class member used to define what values are valid and what are
not for fields. Each property is like a spokesperson for a field: it controls the access to that field. A
property is made up of a get and set method. It’s common to name a property with the title-cased
version of its field’s name, e.g. age and Age. The set method uses the keyword value, which
represents the value we assign to the property.
Using: https://stackoverflow.com/questions/7867377/checking-if-a-string-array-contains-a-value-and-if-so-getting-
its-position
Code 4/12:
using System;
namespace BasicClasses{
class Program{
static void Main(string[] args){
Forest f = new Forest();
f.Name = "Congo";
f.Trees = 0;
f.age = 0;
f.biome = "Tropical";
Console.WriteLine(f.Name);
}
}
}

using System;
namespace BasicClasses{
class Forest{
public string name; // field
public string Name { // property
get {return name;}
set {name = value;}
}
public int trees;
public int Trees {
get{ return trees;}
set{ trees = value;}
}
public int age;
public string biome;
public string Biome{
get{return biome;}
set{
biome =
Array.Exists(new string[] {"Tropical","Temperate","Boreal"},
element => element == value)
? value : "Unknown";
}
}
}
}
There is a shorthand for writing the getters and setters for the properties, called an automatic
property. With this you don’t have to define the field separately or write out the get and set
methods. A hidden field is defined in the background for us.
Code 5/12:
using System;

namespace BasicClasses{
class Forest{
public int age;
public string biome;

public string Name


{ get; set;}
public int Trees
{ get; set;}

public string Biome{


get { return biome; }
set {
if (value == "Tropical" ||
value == "Temperate" ||
value == "Boreal") {
biome = value;
}
else{
biome = "Unknown";
}
}
}
}
}
We can use access modifiers public and private to change how a member can be accessed.
Public-can be accessed by any class
Private-can only be accessed by code in the same class.
They are necessary to encapsulate our classes. Best practice to make fields private and
properties public. Class members are defaulted to private and classes public.
Code 6/12:
using System;

namespace BasicClasses{
class Program{
static void Main(string[] args){
Forest f = new Forest();
f.Name = "Congo";
f.Trees = 0;
f.age = 0;
f.Biome = "Desert";

Console.WriteLine(f.Name);
Console.WriteLine(f.Biome);
}
}
}

using System;

namespace BasicClasses{
class Forest{
public int age;
private string biome;

public string Name


{ get; set; }
public int Trees
{ get; set; }

public string Biome{


get { return biome; }
set{
if (value == "Tropical" ||
value == "Temperate" ||
value == "Boreal"){
biome = value;}
else{
biome = "Unknown";
}
}
}
}
}
If we don’t want programs to set the value of the property then we cannot include a set method,
which produces an error if we try to set the value, or we can make set private, which produces an
error if we try to set the value outside of its class. Generally we prefer approach 2 because it
allows other methods in the class to set the value.
Code 7/12:
using System;

namespace BasicClasses{
class Program{
static void Main(string[] args){
Forest f = new Forest();
f.Name = "Congo";
f.Trees = 0;
f.age = 0;
f.Biome = "Desert";
f.Age = 2; // produces error as setting from outside class
Console.WriteLine(f.Name);
Console.WriteLine(f.Biome);
}
}
}

using System;

namespace BasicClasses{
class Forest{
public int age;
private string biome;

public string Name


{ get; set; }
public int Trees
{ get; set; }
public string Biome{
get { return biome; }
set{
if (value == "Tropical" ||value == "Temperate" ||
value == "Boreal"){
biome = value;
}
else{
biome = "Unknown";
}
}
}
public int Age{get; private set;}
}
}
A method is a named block of code. Methods are used to define behaviour.
Code 8/12:
public int Grow(){
Trees+=30;
Age+=1;
return Trees;
}

public int Burn(){


Trees-=20;
Age+=1;
return Trees;
}

A constructor is used to initialize the values of an object. Used whenever we instantiate an object
with the new keyword. A constructor is automatically created that takes no parameters when we
do not write a constructor explicitly.
Code 9/12:
public Forest(string name, string biome){
Name = name;
Biome = biome;
Age = 0;
}

using System;

namespace BasicClasses{
class Program{
static void Main(string[] args){
Forest f = new Forest("Congo","Tropical");
f.Trees = 0;
Console.WriteLine(f.Name);
Console.WriteLine(f.Biome);
}
}
}

Use this. To refer to the current instance of a class


Code 10/12:
public Forest(string name, string biome){
this.Name = name;
this.Biome = biome;
Age = 0;
}
Constructors, like other methods, can be overloaded. However this can mean more chance for
errors due to duplicate code. We can use default arguments for default values-useful for C# 4.0 or
later. We can also use : this(), which refers to another constructor in the same class. Useful for old
C# programs (before 4.0) and when your second constructor has additional functionality.
This() refers to another constructor in the current class.
Code 11/12:
public Forest(string name, string biome){
this.Name = name;
this.Biome = biome;
Age = 0;
}

public Forest(string name) : this(name, "Unknown"){


Console.WriteLine("Warning: biome defaulted to Unknown");
}

using System;

namespace BasicClasses{
class Program{
static void Main(string[] args){
Forest f = new Forest("Congo", "Tropical");
f.Trees = 0;
Console.WriteLine(f.Name);
Console.WriteLine(f.Biome);
Forest f2 = new Forest("Rendlesham");
Console.WriteLine(f2.Biome);
}
}
}

Code 12/12:
using System;

namespace BasicClasses{
class Program{
static void Main(string[] args){
Forest f = new Forest("Amazon");
Console.WriteLine(f.Trees);
f.Grow();
Console.WriteLine(f.Trees);
}
}
}
Static Members
Static-facts and behaviours associated with the class itself.

Just add static after the access modifier to make it static. Accessed from class not instance.
Code 2/8:
// excerpt:
// FIELDS

public int age;


private string biome;
private static int forestsCreated;

// CONSTRUCTORS

public Forest(string name, string biome)


{
this.Name = name;
this.Biome = biome;
Age = 0;
ForestsCreated +=1;
}

public Forest(string name) : this(name, "Unknown")


{ }

// PROPERTIES

public static int ForestsCreated {


get;
private set;
}

//...

A static method can only access other static members.


Code 3/8:
private static string treeFacts;
public static string TreeFacts{get;}
public static void PrintTreeFacts(){
Console.WriteLine(TreeFacts);
}
A static constructor is run once before a class is used. The static constructor is run when either of
these two events occur: before an object is made from the type or before a static member is
accessed. We typically use a static constructor to set values to static fields and properties. A static
constructor does not accept an access modifier.
Code 4/8:
// CONSTRUCTORS

static Forest(){
treeFacts = "Forests provide a diversity of ecosystem services
including:\r\n aiding in regulating climate.\r\n purifying water.\r\n
mitigating natural hazards such as floods.\n";
ForestsCreated = 0;
}

using System;

namespace StaticMembers{
class Program{
static void Main(string[] args){
Forest.PrintTreeFacts()
}
}
}

A static class cannot be instantiated, so you would only want to do this if you are making a utility or
library, like Math or Console. These classes are static because they are just tools.
Code 5/8:
using System;

namespace StaticMembers{
class Program{
static void Main(string[] args){
Console.WriteLine(Math.PI);
Console.WriteLine(Math.Abs(-32));
}
}
}
Static errors

Each time we run dotnet run, the main() method is called. We can include arguments on the
command line like dotnet run arg1 arg2 arg3.
Code 7/8:
using System;

namespace ApplyingClasses{
class Program{
static void Main(string[] args){
if (args.Length > 0){
string mainPhrase = String.Join(" and ", args);
Console.WriteLine($"My favorite fruits are {mainPhrase}!");
}
}
}
}

Code 8/8:
using System;
namespace StaticMembers{
class Program{
static void Main(string[] args){
Console.WriteLine(Forest.ForestsCreated);
Forest f1 = new Forest("asf","asdfkj");
Forest f2 = new Forest("asf","asdfkj");
Console.WriteLine(Forest.ForestsCreated);
}
}
}
Interfaces
C# is type-safe as it helps us catch bugs at one of the earliest stages: the type checking stage.
Interfaces are sets of actions and values that describe how a class can be used.
It is convention to have every interface start with ‘I’.
Code 2/8:
using System;

namespace LearnInterfaces
{
interface IAutomobile {
string LicensePlate {get;};
double Speed {get;};
int Wheels {get;};
void Honk();
}
}

Code 3/8:
using System;

namespace LearnInterfaces{
class Sedan : IAutomobile {
public double Speed {get;};
public string LicensePlate {get;};
public int Wheels {get;};
public void Honk() {
Console.WriteLine("Honk");
};
}
}
Interfaces cannot specify two types of members that are commonly found in classes.
Code 4/8:
using System;

namespace LearnInterfaces{
class Sedan : IAutomobile{
public string LicensePlate
{ get; }

public double Speed


{ get; set;}

public int Wheels


{ get; }

public void Honk(){


Console.WriteLine("HONK!");
}

public Sedan(double speed){


Speed = speed;
LicensePlate = Tools.GenerateLicensePlate();
Wheels = 4;
}

public void SpeedUp() {


this.Speed += 5;
}

public void SlowDown() {


this.Speed -= 5;
}
}
}
Code 5/8:
using System;

namespace LearnInterfaces{
class Truck : IAutomobile{
public double Speed{get;};
public string LicensePlate {get;};
public int Wheels {get;};
public void Honk() {
Console.WriteLine("Honk");
};
}
}
Code 6/8:
using System;

namespace LearnInterfaces{
class Truck : IAutomobile{
public string LicensePlate
{ get; set;}

public double Speed


{ get; set;}

public int Wheels


{ get; set;}

public double Weight


{ get; set;}

public void Honk(){


Console.WriteLine("HONK!");
}

public Truck(double speed, double weight) {


Speed = speed;
Weight = weight;
LicensePlate = Tools.GenerateLicensePlate();
Wheels = (weight < 400) ? 8 : 12;
}

public void SpeedUp(){


Speed += 5;
}

public void SlowDown() {


this.Speed -= 5;
}
}
}
Code 7/8:
using System;

namespace LearnInterfaces{
class Program{
static void Main(string[] args){
Sedan sedan1 = new Sedan(60);
Sedan sedan2 = new Sedan(70);
Truck truck = new Truck(45,500);
Console.WriteLine($"{sedan1.Speed},{sedan1.Wheels},
{sedan1.LicensePlate}");
Console.WriteLine($"{sedan2.Speed},{sedan2.Wheels},
{sedan2.LicensePlate}");
Console.WriteLine($"{truck.Speed},{truck.Wheels},
{truck.LicensePlate}");
sedan2.SpeedUp();
sedan1.SpeedUp();
truck.SpeedUp();
Console.WriteLine($"{sedan1.Speed},{sedan2.Speed},{truck.Speed}");
}
}
}

Interfaces are useful to guarantee certain functionality across multiple classes. Can have multiple
Interfaces.

Inheritance
In inheritance one class inherits the members of another class. A class can only inherit from one
superclass. You can extend a superclass and implement an interface with the same syntax.
Code 3/9:
sing System;

namespace LearnInheritance{
class Vehicle {
public string LicensePlate {get;};
public double Speed {get; private set;};
public int Wheels {get;};
public void Honk() {};
public void SpeedUp() {};
public void SlowDown() {};
}
}

using System;

namespace LearnInheritance{
class Sedan : Vehicle, IAutomobile{
public Sedan(double speed){
Speed = speed;
LicensePlate = Tools.GenerateLicensePlate();
Wheels = 4;
}
}
}

A protected member can be accessed by the current class and any class that inherits from it.
Code 4/9:
using System;

namespace LearnInheritance{
class Vehicle{
public string LicensePlate
{ get; protected set;}

public double Speed


{ get; protected set; }

public int Wheels


{ get; protected set;}

public void SpeedUp(){


Speed += 5;
}

public void SlowDown(){


Speed -= 5;
}

public void Honk(){


Console.WriteLine("HONK!");
}
}
}

Code 5/9:
using System;
namespace LearnInheritance{
class Truck : Vehicle, IAutomobile{
public double Weight
{ get; }

public Truck(double speed, double weight){


Speed = speed;
LicensePlate = Tools.GenerateLicensePlate();
Weight = weight;

if (weight < 400){


Wheels = 8;
}
else{
Wheels = 12;
}
}
}
}
We can refer to a superclass inside a subclass with the base keyword. We can call the superclass
constructor.

Code 6/9:
using System;
namespace LearnInheritance{
class Vehicle{
public string LicensePlate
{ get; private set; }

public double Speed


{ get; private set; }

public int Wheels


{ get; protected set; }

public void SpeedUp(){


Speed += 5;
}

public void SlowDown(){


Speed -= 5;
}

public void Honk(){


Console.WriteLine("HONK!");
}

public Vehicle(double speed){


Speed = speed;
LicensePlate = Tools.GenerateLicensePlate();
}
}
}

using System;

namespace LearnInheritance{
class Sedan : Vehicle, IAutomobile{
public Sedan(double speed) : base(speed){
Wheels = 4;
}
}
}

using System;

namespace LearnInheritance{
class Truck : Vehicle, IAutomobile{
public double Weight
{ get; }

public Truck(double speed, double weight) : base(speed) {


Weight = weight;

if (weight < 400){


Wheels = 8;
}else{
Wheels = 12;
}
}
}
}

We can override inherited methods. In a superclass we can mark a method as virtual if we think
this member might be overridden in subclasses. In the subclass we mark the member as override.
We can also just define a new member with the same name. The inherited member still exists but
is hidden by the member in the subclass, which is confusing so we stick with the virtual and
override approach.
Code 7/9:
using System;

namespace LearnInheritance{
class Bicycle : Vehicle {
public Bicycle(double speed) : base(speed) {
Speed = speed;
Wheels = 2;
}

public override void SpeedUp() {


Speed += 5;
if (Speed > 15) Speed = 15;
}

public override void SlowDown() {


Speed -= 5;
if (Speed < 0) Speed = 0;
}
}
}

using System;

namespace LearnInheritance{
class Vehicle{
public string LicensePlate
{ get; private set; }

public double Speed


{ get; protected set; }

public int Wheels


{ get; protected set; }

public Vehicle(double speed){


Speed = speed;
LicensePlate = Tools.GenerateLicensePlate();
}

public virtual void SpeedUp(){


Speed += 5;
}

public virtual void SlowDown(){


Speed -= 5;
}

public void Honk(){


Console.WriteLine("HONK!");
}
}
}

Abstract members have no implementation in the superclass but must be implemented in all
subclasses. If one member of a class is abstract then the class itself can’t exist as an instance. So
the entire vehicle class must be abstract. The abstract member must be implemented in each
subclass.
Code 8/9:
using System;

namespace LearnInheritance{
abstract class Vehicle{
public string LicensePlate
{ get; private set; }

public double Speed


{ get; protected set; }

public int Wheels


{ get; protected set; }

public Vehicle(double speed){


Speed = speed;
LicensePlate = Tools.GenerateLicensePlate();
}

public virtual void SpeedUp(){


Speed += 5;
}

public virtual void SlowDown(){


Speed -= 5;
}

public void Honk(){


Console.WriteLine("HONK!");
}

public abstract string Describe();


}
}

using System;
namespace LearnInheritance{
class Bicycle : Vehicle{

public Bicycle(double speed) : base(speed){


Wheels = 2;
}

public override void SpeedUp(){


Speed += 5;

if (Speed > 15){


Speed = 15;
}
}

public override void SlowDown(){


Speed -= 5;

if (Speed < 0){


Speed = 0;
}
}

public override string Describe(){


return $"This Bicycle is moving on {Wheels} wheels at {Speed}
km/h, with license plate {LicensePlate}.";
}
}
}

using System;

namespace LearnInheritance{
class Sedan : Vehicle, IAutomobile{
public Sedan(double speed) : base(speed){
Wheels = 4;
}

public override string Describe(){


return $"This Sedan is moving on {Wheels} wheels at {Speed} km/h,
with license plate {LicensePlate}.";
}
}
}

using System;
namespace LearnInheritance{
class Truck : Vehicle, IAutomobile{
public double Weight
{ get; }

public Truck(double speed, double weight) : base(speed){


Weight = weight;

if (weight < 400){


Wheels = 8;
}else{
Wheels = 12;
}
}
public override string Describe(){
return $"This Truck is moving on {Wheels} wheels at {Speed} km/h,
with license plate {LicensePlate}.";
}
}
}

using System;

namespace LearnInheritance{
class Program{
static void Main(string[] args){
Sedan s = new Sedan(60);
Truck t = new Truck(45, 500);
Bicycle b = new Bicycle(10);
Console.WriteLine(s.Describe());
Console.WriteLine(t.Describe());
Console.WriteLine(b.Describe());
}
}
}

Reference Fundamentals
Classes are reference types. When we create a new instance of a class and store it in a variable,
the variable is a reference to the object. An object can have multiple references. If changes are
made to that object, then they will be reflected in both references to it.
Code 2/11:
using System;

namespace LearnReferences{
class Program{
static void Main(string[] args){
Diary dy1 = new Diary(5);
Diary dy2 = dy1;
dy2.Flip();
Console.WriteLine($"{dy1.CurrentPage} {dy2.CurrentPage}");
}
}
}
Value-type variables hold data while reference-type variables refer to a place in memory.
All primitive types are value types.
Code 4/11:
using System;

namespace LearnReferences{
class Program{
static void Main(string[] args){
Book bookLocation = new Book();
Book sameBookLocation = bookLocation;
bool falseValue = false;
bool anotherFalseValue = falseValue;
}
}
}
When we compare values types with ==, the C# compiler performs a value comparison.
When we compare reference types with ==, the C# compiler performs a referential comparison,
checking if two variables refer to the same memory location.
Code 5/11:
using System;

namespace LearnReferences{
class Program{
static void Main(string[] args){
Book b1 = new Book();
Book b2 = b1;
Console.WriteLine(b1 == b2);
}
}
}
When referencing an object as an implementation it can only use the implementation’s
functionality. You can also reference it as a superclass but it can only use its functionality.
Example:
Dissertation diss = new Dissertation(50);
Book bdiss = diss;
Console.WriteLine(diss.Title);
Console.WriteLine(bdiss.Title);
diss.Define();
// This causes an error as there is no Define() method in Book class
bdiss.Define();
Code 6/11:
using System;

namespace LearnReferences{
class Program{
static void Main(string[] args){
Dissertation diss = new Dissertation();
IFlippable fdiss = diss;
Book bdiss = diss;
fdiss.CurrentPage = 42;
bdiss.Stringify();
Console.WriteLine(fdiss == bdiss);
}
}
}

Code 7/11:
using System;

namespace LearnReferences{
class Program{
static void Main(string[] args){
Dissertation diss1 = new Dissertation(32, "Anna Knowles-Smith",
"Refugees and Theatre");
Dissertation diss2 = new Dissertation(19, "Lajos Kossuth", "Shiny
Happy People");
Diary dy1 = new Diary(48, "Anne Frank", "The Diary of a Young
Girl");
Diary dy2 = new Diary(23, "Lili Elbe", "Man into Woman");

Book[] books = new Book {diss1, diss2, dy1, dy2};


foreach (Book b in books) {
Console.WriteLine($"{b.Title}");
}
}
}
}
Polymorphism describes the concept that objects of different types can be accessed through the
same interface. Each type can provide its own, independent implementation of this interface.
A method from a superclass can be overridden and will stay overridden even if the object is
referenced as the superclass.
Book bk = new Book();
Book bdiss = new Dissertation(); // Stringify method overridden
Console.WriteLine(bk.Stringify()); // This is a Book reference!
Console.WriteLine(bdiss.Stringify());//This is a Dissertation reference!
Polymorphism is the ability in programming to present the same interface for differing data types.
Code 8/11:
namespace LearnReferences{
class Diary : Book, IFlippable{
public int CurrentPage
{ get; set; }

public Diary(int page = 0) : base(){


CurrentPage = page;
}

public Diary(int page, string author, string title) : base(author,


title){
CurrentPage = page;
}

public void Flip(){


CurrentPage++;
}

public string SpillSecret(){


return "OMG kerry loves kris <3";
}

public override string Stringify() {


return "This is a Diary object!";
}
}
}
namespace LearnReferences{
class Book{
public string Author
{ get; private set; }

public string Title


{ get; private set; }

public Book(string author = "Unknown", string title = "Untitled"){


Author = author;
Title = title;
}

public virtual string Stringify(){


return "This is a Book object!";
}
}
}

using System;

namespace LearnReferences{
class Program{
static void Main(string[] args){
Book b1 = new Book();
Book b2 = new Diary();
Console.WriteLine(b1.Stringify());
Console.WriteLine(b2.Stringify());
}
}
}
Upcasting is referring to objects with a reference of their own type, an inherited type, or an
implemented interface. Downcasting is referencing an object by a subclass. Not every downcast is
possible in C#. If a subclass has a method or member that is incompatible with a superclass then
you must explicitly downcast using parentheses. In many cases the downcast will still fail. There
are multiple ways to deal with downcasting, including the as and is operators. Learn about them in
the Microsoft C# programming Guide:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions
Upcasting can be done implicitly, downcasting cannot.
Code 9/11:
using System;

namespace LearnReferences{
class Program{
static void Main(string[] args){
Dissertation diss = new Dissertation();
Diary dy = new Diary();

Book bdiss = diss;


Book bdy = dy;
}
}
}

A reference to no object is called either a null reference or unassigned. We can use it to show that
a reference is missing, create a reference variable without defining it, or initialise an empty array.
You can only compare a null reference if it is explicitly labelled null.
The C# compiler prevents future issues down the road by raising an error the first time an
unassigned variable is used.
Code 10/11:
using System;

namespace LearnReferences{
class Program{
static void Main(string[] args){
Book b = null;
Console.WriteLine($"{b}");
Console.WriteLine(b == null);
}
}
}
The Object Class
object is the only type of reference that can be used for all objects. All classes are derived from
Object. Every type inherits from Object.
Code 2/6:
using System;

namespace TheObjectClass{
class Program{
static void Main(string[] args){
Book bk = new Book();
Object obk = bk;
Diary dy = new Diary(38);
Object ody = dy;
int i = 9;
Object oi = i;

}
}
}
Object has a few useful members that are accessible by every type. Important ones:
Equals(Object), GetType(), ToString().
Full list: https://docs.microsoft.com/en-us/dotnet/api/system.object?view=netframework-4.8
Code 3/6:
using System;

namespace TheObjectClass{
class Program{
static void Main(string[] args){
Book b = new Book();
Diary d = new Diary(38);
Random r = new Random();
int i = 9;
Object[] objArr = {b,d,r,i};
foreach(Object o in objArr) {
Console.WriteLine(o.GetType());
}
}
}
}

The Equals() and ToString() methods in Object are virtual, so they can be overridden.
Code 4/6:
namespace TheObjectClass{
class Book{
public string Author
{ get; private set; }

public string Title


{ get; private set; }

public Book(string author = "Unknown", string title = "Untitled"){


Author = author;
Title = title;
}

public virtual string Stringify(){


return "This is a Book object!";
}

public override string ToString(){


return $"{Title} is authored by {Author}";
}
}
}

using System;

namespace TheObjectClass{
class Program{
static void Main(string[] args){
Book bk = new Book("Ta-Nehisi Coates", "Between the World and
Me");
Console.WriteLine(bk.ToString());
}
}
}

Console.WriteLine() uses ToString() which is defined in Object.


Code 5/6:
namespace TheObjectClass{
class Diary : Book, IFlippable{
public int CurrentPage{ get; set; }

public Diary(int page = 0) : base(){


CurrentPage = page;
}

public Diary(int page, string author, string title) : base(author,


title){
CurrentPage = page;
}

public void Flip(){


CurrentPage++;
}

public string SpillSecret(){


return "OMG kerry loves kris <3";
}

public override string Stringify() {


return "This is a Diary object!";
}

public override string ToString(){


return "Surprise!";
}
}
}

using System;

namespace TheObjectClass{
class Program{
static void Main(string[] args){
Diary diaryObj = new Diary(1,"CJ","Page 1 title");
Console.WriteLine(diaryObj);
}
}
}

https://docs.microsoft.com/en-us/dotnet/api/system.object?view=netframework-4.8#remarks

String, The Exception


Strings are technically reference types, but in action it’s a bit complicated. In C# String and string
are equivalent. Its value is stored as a collection of char objects. A string reference will always
point to the original object. Comparing strings with the equality operator (==) performs a value, not
referential, comparison. Strings are immutable, they cannot be changed after they are created.
Any modifications returns a new string object.
Code 2/5:
using System;

namespace StringTheException{
class Program{
static void Main(string[] args){
string a = "immutable";
string b = "immutable";
Console.WriteLine(a==b);
Object c = new Object();
Object d = new Object();
Console.WriteLine(c==d);
}
}
}
String references can be null, unassigned or empty.
 unassigned means that the programmer did not give the variable any value
 null means that the programmer intentionally made the variable refer to no object
 an empty string signifies a piece of text with zero characters. This is often used to represent a
blank text field. It can be represented by "" or String.Empty
Use String.Empty or “” instead of null to avoid NullreferenceException errors.
Code 3/5:
using System;

namespace StringTheException{
class Program{
static void Main(string[] args){
Console.Write("User i demand that you give input: ");
string inp = Console.ReadLine();
if (String.IsNullOrEmpty(inp)) {
Console.WriteLine("You didn't enter anything!");
} else{
Console.WriteLine("Thank you for your submission!");
}
}
}
}
String is a class as it has methods and properties like Length or Contains(). It also has static
properties like Empty or methods like IsNullOrEmpty().
Code 4/5:
using System;

namespace StringTheException{
class Program{
static void Main(string[] args){
string lyrics =
"Dollie, Dollie, bo-bollie,\n" +
"Banana-fana fo-follie\n" +
"Fee-fi-mo-mollie\n" +
"Dollie!";

// Call `Replace()` here


Console.WriteLine(lyrics.Replace("ollie", "ana"));
}
}
}
Title

Code 1/10:
Static Members

You might also like