Professional Documents
Culture Documents
References
References
2024, 7:57 PM
Cheatsheets / Learn C#
C# Reference Types
C# Object Reference
In C#, an object may be referenced by any type // Woman inherits from Human, which
in its inheritance hierarchy or by any of the
inherits from Animal, and it implements
interfaces it implements.
IPerson:
class Human : Animal
class Woman : Human, IPerson
about:srcdoc Page 1 of 13
19.01.2024, 7:57 PM
C# Polymorphism
about:srcdoc Page 2 of 13
19.01.2024, 7:57 PM
// This is a Book!
// This is a Novel
C# Upcasting
string s = "Hi";
Object o = s;
about:srcdoc Page 3 of 13
19.01.2024, 7:57 PM
C# Downcasting
about:srcdoc Page 4 of 13
19.01.2024, 7:57 PM
C# Null Reference
Console.WriteLine(mc == null);
// Prints true.
C# Value Types
about:srcdoc Page 5 of 13
19.01.2024, 7:57 PM
C# Comparison Type
In C#, the type of comparison performed with // int is a value type, so == uses
the equality operator ( == ), differs with
value equality:
reference and value types.
When two value types are compared, they are int num1 = 9;
compared for value equality. They are equal if int num2 = 9;
they hold the same value.
Console.WriteLine(num1 == num2);
When two reference types are compared, they
are compared for referential equality. They are // Prints true
equal if they refer to the same location in
memory.
// All classes are reference types, so
== uses reference equality:
WorldCupTeam japan = new
WorldCupTeam(2018);
WorldCupTeam brazil = new
WorldCupTeam(2018);
Console.WriteLine(japan == brazil);
// Prints false
// This is because japan and brazil
refer to two different locations in
memory (even though they contain
objects with the same values):
C# Override
class MainClass {
public static void Main (string[]
about:srcdoc Page 6 of 13
19.01.2024, 7:57 PM
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");
}
}
// If we wanted bcdc.Method1() to
invoked BaseClass.Method1(), then we
about:srcdoc Page 7 of 13
19.01.2024, 7:57 PM
C# String Comparison
In C#, string is a reference type but it can //In this example, even if s and t are
be 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);
about:srcdoc Page 8 of 13
19.01.2024, 7:57 PM
In C#, string types are immutable, which // Two examples demonstrating how
means they cannot be changed after they are
immutablility determines string
created.
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 "
about:srcdoc Page 9 of 13
19.01.2024, 7:57 PM
C# Empty String
// Unassigned:
string s3;
// Null:
string s4 = null;
about:srcdoc Page 10 of 13
19.01.2024, 7:57 PM
C# Object Class
In C#, the base class of all types is the // When you write this code:
Object class. Every class implicitly inherits
class Dog {}
this class.
When you create a class with no inheritance, C# // C# assumes you mean:
implicitly makes it inherit from Object . class Dog : Object {}
about:srcdoc Page 11 of 13
19.01.2024, 7:57 PM
In C#, the Object class includes definitions Object obj = new Object();
for these methods: ToString() ,
Console.WriteLine(obj.ToString());
Equals(Object) , and 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());
}
about:srcdoc Page 12 of 13
19.01.2024, 7:57 PM
C# ToString() Method
Print Share
about:srcdoc Page 13 of 13