Hello.
In this video, we're going to talk about Question 15, what is the difference between type of and the
type?
Both type of keyword and the type of method are used to get the information about some type.
The differences between them are that the type of takes, the name of the type we want to inspect.
So we must know the type before type of is resolved at compile time.
Good type is a method that must be executed on an object.
Because of that, it is resolved at one time.
This method comes from the system object based class, so it is available in any object in C-sharp both
type of and get type method return an object of the type class this object hosts or information about
the type.
For example, it contains the type name list of the constructors.
The base type implemented interfaces, et cetera.
All right.
Let's see type of and grid type in practice.
If I know of the type already and they only wanted to get the type object for some reason, I can use
the type of keyword.
Here are used the type of was the base type, but here I executed the good type method on an object.
In both cases, the result is the type object containing full information about the type called base.
We don't always know the type at computation time, so we can't always use the type of keyword.
Let's consider this method.
This object can be anything, and it will only be known at random.
What it is?
Exactly.
That's why I can't use type of here because I wouldn't know what to pass as the parameter.
I must use the gift type formatted.
Let's see the print type named method in action.
As you can see, it's printer, the actual type of those objects.
The important thing to understand is that grid type always returns the actual type of an object.
Let's see an example.
Even if this variable is of type base, I assigned an object of type direct to it, it is possible because
derived inherits from base, the good type method printer, the actual type, we actually have seen
this before in the print type method, even if it took a parameter of system, object type, it printer,
the actual type of the given object.
Remember, does the get type method belongs to the system object type so it can be called on an object
in C-sharp?
Let's summarize both type of and forget that method return a type object which holds the information
about the type type of things, the name of a type, and it gets resolved at compilation time.
The type method is called upon an object, so it is resolved at random.
Both type of and the good type method are parts of the reflection mechanism, which we learn about in
the next lecture.
If this topic is touched during the interview, you can be asked What is the purpose of the gut type
method?
This method requires the type object which holds or information about the type of an object it was called
on.
The type object contains Phoenix like type name, list of constructors attributes, the best type,
etc. One is the goods type method defined.
It is defined in the system object type, which is a base type for all types in C sharp.
This is why we can call the get type method on objects of a type.
All right, thanks for watching, and I'll see you in the next lecture where we will discuss reflection.