0
0
C Sharp (C#)programming~5 mins

Getting type information at runtime in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of getting type information at runtime in C#?
It helps you find out what kind of object you have while the program is running. This is useful when you don't know the type in advance and want to make decisions based on the object's type.
Click to reveal answer
beginner
Which C# keyword or method returns the runtime type of an object?
The GetType() method returns the exact type of an object at runtime.
Click to reveal answer
intermediate
How do you compare the runtime type of an object to a known type in C#?
Use obj.GetType() == typeof(SomeType) to check if the object's runtime type matches SomeType.
Click to reveal answer
intermediate
What is the difference between GetType() and typeof() in C#?
GetType() is called on an object instance to get its runtime type. typeof() is used with a type name to get the Type object representing that type at compile time.
Click to reveal answer
beginner
How can you get the name of an object's type at runtime?
Call obj.GetType().Name to get the simple name of the object's type as a string.
Click to reveal answer
Which method do you use to get the runtime type of an object in C#?
ATypeOfObject()
Btypeof()
CGetType()
DGetRuntimeType()
What does typeof(int) return in C#?
AThe Type object representing the int type
BThe runtime type of an int variable
CThe string "int"
DAn instance of int
How do you check if an object is exactly of type string at runtime?
Aobj is string
Bobj.GetType() == typeof(string)
Cobj.GetType().Name == "string"
Dobj == string
Which property gives you the simple name of a type in C#?
AType.FullName
BType.ToString()
CType.SimpleName
DType.Name
If you want to get the full namespace and name of a type, which property do you use?
AType.FullName
BType.Namespace
CType.Name
DType.ToString()
Explain how to get and compare the runtime type of an object in C#.
Think about how you find out an object's type and then check if it matches a known type.
You got /4 concepts.
    Describe the difference between GetType() and typeof() in C#.
    One works on objects, the other works on type names.
    You got /4 concepts.