Kang-Jun Heo Kang-Jun Heo - 10 days ago 7
C# Question

Differences between C# "var" and C++ "auto"

I'm learning C++ now because I need to write some low level programs.

when I learned about "auto" keyword, it reminds me "var" keyword, from C#.

So, What are differences of C# "var" and C++ "auto"? It seems same as see their function...

*not totally, but I understood! thank you for commented, answered everyone!

*I deleted my comment from the answer because I saw the message "avoid message 'thank you!'".

Answer

They are equivalent. They both allow you not to specify the type of a variable yourself, but the variable stays strongly-typed. The following lines are equivalent in c#:

var i = 10; // implicitly typed  
int i = 10; //explicitly typed  

And the following lines are equivalent in c++:

auto i = 10;
int i = 10;

However, you should keep in mind that in c++ the correct type of an auto variable is determined using the rules of template argument deduction for a function call.

Comments