forum

Home / DeveloperSection / Forums / How can var know of an undefined type?

How can var know of an undefined type?

Royce Roy 2216 03-Feb-2014

How can the implicit type variable var know a type that is not defined in the scope (using using)?

Example:

This is ok

public class MyClass
{
    public void MyMethod       
    {
        var list = AStaticClass.GetList();
    }
}

But this is not ok

public class MyClass
{
    public void MyMethod       
    {
        List<string> list = AStaticClass.GetList();
    }
}

In the last code snippet I have to add using System.Collections.Generic; for it to work.

How does this work?


c# c# 
Updated on 03-Feb-2014

Can you answer this question?


Answer

1 Answers

Liked By