Learn C# - Learn C# - References Cheatsheet - Codecademy
Learn C# - Learn C# - References Cheatsheet - Codecademy
Cheatsheets / Learn C#
C# Reference Types
In C#, classes and interfaces are reference types. SportsCar sc = new SportsCar(100);
Variables of reference types store references to their
SportsCar sc2 = sc;
data (objects) in memory, and they do not contain the
data itself. sc.SpeedUp(); // Method adds 20
An object of type Object , string , or dynamic is Console.WriteLine(sc.Speed); // 120
also a reference type.
Console.WriteLine(sc2.Speed); // 120
C# Object Reference
In C#, an object may be referenced by any type in its // Woman inherits from Human, which
inheritance hierarchy or by any of the interfaces it
inherits from Animal, and it implements
implements.
IPerson:
class Human : Animal
class Woman : Human, IPerson
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 1/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 2/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
C# Polymorphism
class Book
{
public virtual string Stringify()
{
return "This is a Book!;
}
}
// This is a Book!
// This is a Novel
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 3/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
C# Upcasting
In C#, upcasting is creating an inherited superclass or // In this case, string inherits from
implemented interface reference from a subclass
Object:
reference.
string s = "Hi";
Object o = s;
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 4/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
C# Downcasting
In C#, downcasting is creating a subclass reference // Dog inherits from Pet. An implicit
from a superclass or interface reference.
downcast throws a compile-time error:
Downcasting can lead to runtime errors if the
superclass cannot be cast to the specified subclass. Pet pet = new Pet();
Dog dog = pet;
Account a = new Account();
// error CS0266: Cannot implicitly
CustomerAccount ca = a;
convert type `Pet` to `Dog`. An explicit
// error CS0266: Cannot implicitl
conversion exists (are you missing a
cast?)
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 5/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
C# Null Reference
Console.WriteLine(mc == null);
// Prints true.
C# Value Types
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 6/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
C# Comparison Type
In C#, the type of comparison performed with the // int is a value type, so == uses value
equality operator ( == ), differs with reference and
equality:
value types.
When two value types are compared, they are int num1 = 9;
compared for value equality. They are equal if they int num2 = 9;
hold the same value.
Console.WriteLine(num1 == num2);
When two reference types are compared, they are
compared for referential equality. They are equal if // Prints true
they refer to the same location in memory.
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 7/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
C# Override
In C#, the override modifier allows base class // In the below example,
references to a derived object to access derived
DerivedClass.Method1() overrides
methods.
In other words: If a derived class overrides a member of BaseClass.Method1(). bcdc is a BaseClass-
its base class, then the overridden version can be type reference to a DerivedClass value.
accessed by derived references AND base references.
Calling bcdc.Method1() invokes
DerivedClass.Method1().
class MainClass {
public static void Main (string[] args)
{
BaseClass bc = new BaseClass();
DerivedClass dc = new DerivedClass();
BaseClass bcdc = new DerivedClass();
bc.Method1();
dc.Method1();
bcdc.Method1();
}
}
class BaseClass
{
public virtual void Method1()
{
Console.WriteLine("Base -
Method1");
}
}
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 8/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
// Base - Method1
// Derived - Method1
// Derived - Method1
C# String Comparison
In C#, string is a reference type but it can be //In this example, even if s and t are
compared by value using == .
not referentially equal, they are equal
by value:
string s = "hello";
string t = "hello";
// b is true
bool b = (s == t);
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 9/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
In C#, string types are immutable, which means they // Two examples demonstrating how
cannot be changed after they are created.
immutablility determines string behavior.
In both examples, changing one string
variable will not affect other variables
that originally shared that value.
//EXAMPLE 1
string a = "Hello?";
string b = a;
b = "HELLLLLLLO!!!!";
Console.WriteLine(b);
// Prints "HELLLLLLLO!!!!"
Console.WriteLine(a);
// Prints "Hello?"
//EXAMPLE 2
string s1 = "Hello ";
string s2 = s1;
s1 += "World";
System.Console.WriteLine(s2);
// Prints "Hello "
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 10/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
C# Empty String
// Unassigned:
string s3;
// Null:
string s4 = null;
C# Object Class
In C#, the base class of all types is the Object class. // When you write this code:
Every class implicitly inherits this class.
class Dog {}
When you create a class with no inheritance, C#
implicitly makes it inherit from Object . // C# assumes you mean:
class Dog : Object {}
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 11/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
In C#, the Object class includes definitions for these Object obj = new Object();
methods: ToString() , Equals(Object) , and
Console.WriteLine(obj.ToString());
GetType() .
// The example displays the following
output:
// System.Object
Console.WriteLine("mybase: Type
is {0}", myBase.GetType());
Console.WriteLine("myDerived: Type is
{0}", myDerived.GetType());
Console.WriteLine("object o =
myDerived: Type is {0}", o.GetType());
Console.WriteLine("MyBaseClass b =
myDerived: Type is {0}", b.GetType());
}
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 12/13
10-02-2024 16:04 Learn C#: Learn C#: References Cheatsheet | Codecademy
C# ToString() Method
When a non-string object is printed to the console with Random r = new Random();
Console.WriteLine() , its ToString() method is
called.
// These two lines are equivalent:
Console.WriteLine(r);
Console.WriteLine(r.ToString());
Print Share
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references/cheatsheet 13/13