Professional Documents
Culture Documents
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.
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
}
}
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.
Declaring variables is just like in java, where the variable can be initialised and then assigned later
like:
int myAge;
myAge = 32;
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());
}
}
}
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.
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.
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);
}
}
}
3/8 Code:
using System;
namespace PlanetCalculations {
class Program{
static void Main(string[] args) {
// Your Age
int userAge = 17;
// Age on Jupiter
double jupiterAge = userAge / jupiterYears;
// Time to Jupiter
double journeyToJupiter = 6.142466;
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);
}
}
}
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));
}
}
}
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));
}
}
}
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 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);
}
}
}
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.");
}
}
}
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}");
}
}
}
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;
}
}
}
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);
}
}
}
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");
}
}
}
}
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!");
}
}
}
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());
}
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);
}
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;
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;
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;
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;
int[] ratings = { 5, 4, 4, 3, 3, 5, 5, 4 };
}
}
}
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;
int[] ratings = { 5, 4, 4, 3, 3, 5, 5, 4 };
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;
int[] ratings = { 5, 4, 4, 3, 3, 5, 5, 4 };
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;
int[] ratings = { 5, 4, 4, 3, 3, 5, 5, 4 };
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);
}
}
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" };
Code 6/8:
using System;
namespace ComparingLoops{
class Program{
static void Main(string[] args){
string[] websites = { "twitter", "facebook", "gmail" };
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);
}
}
}
namespace BasicClasses{
class Forest{
}
}
using System;
namespace BasicClasses{
class Program{
static void Main(string[] args){
Forest f = new Forest();
}
}
}
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;
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;
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;
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);
}
}
}
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
// CONSTRUCTORS
// PROPERTIES
//...
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; }
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;}
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;}
Code 5/9:
using System;
namespace LearnInheritance{
class Truck : Vehicle, IAutomobile{
public double Weight
{ get; }
Code 6/9:
using System;
namespace LearnInheritance{
class Vehicle{
public string LicensePlate
{ get; private set; }
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; }
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;
}
using System;
namespace LearnInheritance{
class Vehicle{
public string LicensePlate
{ get; private set; }
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; }
using System;
namespace LearnInheritance{
class Bicycle : Vehicle{
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; }
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");
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();
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; }
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());
}
}
}
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
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!";
Code 1/10:
Static Members