C# : Summary Guide with Anime Examples

Olivia
6 min readOct 7, 2021

Guess who’s back with the Anime x Coding examples!

OOP (object-oriented programming) is about creating objects that contain:

  • Data (variable defined directly in the class? => Field)
  • Methods (functions, block of code that runs only when called, can accept parameters)

OOP core concepts:

  • Encapsulation
  • Inheritance
  • Abstraction
  • Polymorphism

Methods

Methods in C# => name of method capitalised with parentheses the end. To run method include semi-colon at the end.

  • Access with the . syntax
  • public methods can only be accessed by objects — public also an access modifier
  • static methods can be accessed without creating an object of the class

Example: A class called Ichigo might have a method called Bankai, which shouts / writes the iconic line:

Ichigo.Bankai(); // Console.WriteLine("BANKAI");

Classes

  • Template for objects
  • use class keyword to create
  • Example 1: FaveAnimes
class FaveAnime
{
// code}

Objects

  • An instance of a class, created from a class
  • keyword: new is to create new object
class FaveAnime
{
int rating;
string genre;


static void Main(string[] args)
{
FaveAnime Gintama = new FaveAnime();
Gintamma.rating = 8;
Gintama.genre = "comedy"
FaveAnime Jujutsu_Kaisen = new FaveAnime();
Jujutsu_Kaisen.rating = 9;
Jujutsu_Kaisen.genre = "Supernatural battle Shonen"
Console.WriteLine(Gintama.genre);
Console.WriteLine(Jujutsu_Kaisen.rating);
}
}
// outputs: "comedy"
// outputs: 9

Constructors

  • method to initialize objects, called when an object of a class is created and can set initial values for fields

class Avatar
{
public string tribe;


public Avatar() // this is the constructor
{
tribe = "water"; // Set the initial value for model
}

static void Main(string[] args)
{
Avatar Katara = new Avatar();
Console. WriteLine(Katara.tribe); // Print the value of model
}
}

// Outputs "water"

Constructors with parameters

  • Used to initialise fields / set values. Saves time.
class Avatar
{
public string tribe;
public string colour;


public Avatar (string tribeName, string tribeColour)
{
tribe = tribeName;
colour = tribeColour;
}

static void Main(string[] args)
{
Avatar Katara = new Avatar("water", "blue");
Console.WriteLine(Katara.tribe + " " + Katara.colour);
}
}


// Outputs: water blue

Access Modifiers

  • public: code is accessible for all classes
  • private: accessible within the same class
  • protected: accessible within the same class, or in class that is inherited

Private

  • Here, the private string ‘TechniqueName’ can only be accessed inside the class CursedTechnique
class CursedTechnique{
private string TechniqueName = "Straw Doll";

static void Main(string[] args)
{
CursedTechniques Nobara = new CursedTechnique();
Console.WriteLine(Nobara.TechniqueName);
}
}
// outputs "Straw Doll"
  • If I had an entirely new class called SailorMoon, I could not access ‘TechniqueName’ inside it
  • There’s no cursed techniques in the anime Sailor Moon, just Jujutsu Kaisen, this should help with visualising why its private.
  • You should get an error similar to:
'SailorMoon.TechniqueName' is inaccessible due to its protection level
The field 'SailorMoon.TechniqueName' is assigned but its value is never used

Public

Forget what I just said, there's going to be a Sailor Moon x Jujutsu Kaisen cross over episode! The characters in Sailor Moon will have access to cursed TechniqueName‘s now and will not flag any errors since its been made public

class CursedTechnique{
public string TechniqueName = "Straw Doll";
}
class SailorMoon
{
static void Main(string[] args)
{
SailorMoon SailorLiv = new SailorMoon();
Console.WriteLine(SailorLiv.TechniqueName);
}
}

And now a new character Sailor Liv (me!) has access to cursed techniques from a totally different show!

Properties: get & set

Sometimes we need to access private fields in different classes without getting the errors discussed above. One way to do this, without changing them to public, is through properties.

Property: combo of variable and method, 2 methods- get & set.

class MHAClass1A
{
private string student; // lowercase field

public string Student // uppercase property, both associated
{
get { return student; } // returns value of student
set { student = value; } // set method
}
}

Can now use the Student property (the uppercase public string) to access and update a new class!

class MHAFavourites
{
static void Main(string[] args)
{
MHAFavourites bestBoy = new MHAFavourites();
bestBoy.Student = "Shoto Todoroki";
Console.WriteLine(bestBoy.Student);
}
}
// output: "Shoto Todoroki"

Inheritance

  • Derived Class — (child)
  • Base Class — (parent)
  • Inherit using a colon derived : parent

I have a great My Hero Academia example for this, since Deku inherits (kind of) his Quirk from All Might

class AllMight // base class (parent) 
{
public string name = "Toshinori Yagi";
public void OneForAll()
{
Console.WriteLine("Super strength");
}
}
class Deku : AllMight
{
public string name = "Izuku Midoriya";
}

What this means that you can now run lines of code similar to Deku.OneForAll or Deku.name which should output "Toshinori Yagi"

This is because the fields and methods from one parent/base class (All Might) were inherited by a child/derived class (Deku) when we did class Deku : AllMight

Sealed & Inheritance

If you don’t like My Hero Academia and wanted to render the whole plot redundant, you could seal the All Might class, so that no one can inherit from him

sealed class AllMight 
{
// code
}
class Deku : AllMight
{
// code
}

Will produce the error:

 ‘Deku’: cannot derive from sealed type ‘AllMight’

Polymorphism & Overriding

Polymorphism = ‘many forms’ and occurs when classes have inherited from one another.

Anime example: Deku inherits One For All, but in the nature of Polymorphism, he uses it a different way to perform different tasks.

Here, AllMight is the Base / Parent class with a method called OneForAll();

class AllMight // base class (parent) 
{
public void OneForAll()
{
Console.WriteLine("Super strength!");
}
}

If we had 3 other classes named Deku, Todoroki and Bakugo, and each of them inherited from the class AllMight to also use OneForAll, but in a slightly modified way, this would fail.

class Todoroki : AllMight
{
public void OneForAll()
{
Console.WriteLine("Super icy hot!");
}
}
class Bakugo: AllMight
{
public void OneForAll()
{
Console.WriteLine("Super angry!");
}
}

We’re not going to get ‘Super icy hot!” and “Super angry!” despite editing (polymorphing) the OneForAll method, we’re going to get the original “super strength” output. — Why? All Might is superior. The base class methods will always override derived (child) classes if using the same name, so despite our edits to OneForAll() we are stuck with “Super strength”.

How to overcome this: use the keyword virtual in the base class, and keyword override for each derived class

class AllMight // base class (parent) 
{
public virtual void OneForAll()
{
Console.WriteLine("Super strength!");
}
}
class Todoroki : AllMight
{
public override void OneForAll()
{
Console.WriteLine("Super icy hot!");
}
}

Now this code will work, and we will have the output ‘Super icy hot!’

Abstraction

Abstract classes and methods

Abstraction = hiding certain details / information from the user and only showing what they need to know.

Abstract classes: cannot use them to create new objects, can only be used through inheritance

Abstract method: can only be used in an abstract class, does not have a body of code, body is provided by the derived (inherited / parent) class

Enums

A group of constants — unchangeable.

enum Quint_Quintuplets {Ichika,
Nino,
Miku,
Yotsuba,
Itsuki
}

use enum keyword, seperate the enum items with a comma. Use the .syntax to access the enum items

Quintessential_Quintuplets bestGirl = Quint_Quintuplets.MikuConsole.WriteLine(bestGirl)// outputs the truth: Miku.

Enum values

First item = value 0.

To get an integer value from an item, convert the item to an int

static void Main(string[] args)
{
int numberedQuint = (int) Quint_Quintuplets.Nino;
Console.WriteLine(numberedQuint);
}
// outputs 1

Assign your own value to enum items and the following items will update accordingly

enum Quint_Quintuplets {Ichika,
Nino,
Miku = 6,
Yotsuba, // will be 7
Itsuki, // will be 8
}

Enum Switch Statement

enum Quint_Quintuplets {Ichika,
Nino,
Miku,
Yotsuba,
Itsuki
}static void Main(string[] args)
{
Quint_Quintuplets name = Quint_Quintuplets.Miku;
switch(name)
{
case Quint_Quintuplets.Miku:
Console.WriteLine("Best girl");
break;
case Quint_Quintuplets.Ichika:
Console.WriteLine("Worst girl");
break;
case Quint_Quintuplets.Yotsuba:
Console.WriteLine("3rd best girl");
break;
}
}
// output will be: "Best girl"

--

--

Olivia

From Law Graduate to Fullstack Developer / Professional Googler. Join me as I blog about the highs & lows of learning to code and navigating my new career path