Mike Christensen
Mike Christensen

Reputation: 91724

What is the proper terminology for each type of an identifier?

Take the following code:

IFoo foo = new FooImplementation();

The identifier foo has two types:

  1. IFoo - This is the type the compiler will enforce. I will only be able to call methods that are part of the IFoo contract, otherwise I'll get a compiler error.
  2. FooImplementation - This is the type as known by the runtime. I can downcast foo to a FooImplementation at runtime, and then call non-IFoo methods of FooImplementation.

My question: What is the proper terminology for these two types. I could swear in school we were taught that IFoo is the identifier's static type and FooImplementation is its dynamic type, but after much searching on Google I can't seem to find any reference to this.

Upvotes: 7

Views: 215

Answers (3)

Eric Lippert
Eric Lippert

Reputation: 660533

I agree with Mike Z. The usual terminology in C# is "compile time type" and "runtime type".

"Static type" and "dynamic type" are entirely reasonable terms but I would avoid them in the context of C#. "Static type" could too easily be confused with "static class", a class which can only contain static methods. And "dynamic type" can too easily be confused with the dynamic type feature added to C# 4.

Upvotes: 5

Mike Zboray
Mike Zboray

Reputation: 40838

I would call IFoo and FooImplementation the compile-time and run-time types, respectively. This language is used by C# spec, for example, when talking about virtual methods (section 1.6.6.4):

When a virtual method is invoked, the run-time type of the instance for which that invocation takes place determines the actual method implementation to invoke. In a nonvirtual method invocation, the compile-time type of the instance is the determining factor.

Upvotes: 7

Justin Niessner
Justin Niessner

Reputation: 245499

The first is the Declared Type. The second is the Concrete Type.

...at least that's what I call them.

Upvotes: 4

Related Questions