D
D
Dmitry2017-01-31 22:11:33
ASP.NET
Dmitry, 2017-01-31 22:11:33

What does the type of a variable mean and what does it give?

Hello! I correctly understand the meaning of this phrase , an object of the User class with the IUser type is created? What does this type of IUser give - define the available members for the object? I ask this because, I don’t understand that, Object is the base class for all types, but for the IUser interface, Object is not base, and the user variable has the IUser type, so why does the user variable have methods from Object available?
IUser user = new User();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Vitrenko, 2017-01-31
@Vestail

In C#, a class(User, Object) defines an implementation and a type at the same time. The interface(IUser) defines only the type. One object can have multiple types. In turn, one type can have several implementations.
The object you created has at least three types: User, IUser, and Object.
Because all classes in C# are inherited from Object (which means that all objects have its type), the compiler knows about this in advance and therefore makes it possible to call methods declared in Object from a variable of any type.

J
John_Nash, 2017-02-01
@John_Nash

1) correct
2) object - basic for all objects (of any type), no exceptions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question