‘auto’ in C++ is the same as ‘var’ in C#

auto in C++ is the same as var in C#. Auto keyword is defined in upcoming C++ standard C++0x

The news is, that latest Microsoft Visual Studio C++ 2010 have support for this keyword. auto keyword has a new meaning – ‘declares a variable whose type is deduced from the initialization expression in its declaration’.

Old C++

std::vector intArray;
intArray.push_back(10);
intArray.push_back(20);
intArray.push_back(30);
std::vector::const_iterator iterator = intArray.begin();

New C++

std::vector intArray;
intArray.push_back(10);
intArray.push_back(20);
intArray.push_back(30);
auto iterator = intArray.begin();

As you see, ‘std::vector::const_iterator iterator’ becomes ‘auto iterator’. No more unnecessary typing.

The same concept in C# is available from version 3.0. Variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. Reference.

P.S. Open source world are using auto keyword for at least a year. Support for auto keyword was added to GCC beginning from version 4.4.0