Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to the C# "var" keyword in C++/CLI?

In C#, I like the var keyword for situations like this:

var myList = new List<MyType>(); 

Is there any equivalent in C++/CLI, or do I have to repeat the type name everytime just like this:

List<MyType ^>^ myList = gcnew List<MyType ^>(); 

Could not find an explicit statement in the docs or by Google so far. I am using Visual Studio 2008.

Addendum from 2022: as the accepted answer states correctly, today there is the auto keyword. We are now using this for years, and it works flawlessly for both managed and unmanaged types.

like image 758
Doc Brown Avatar asked Jan 05 '10 17:01

Doc Brown


People also ask

Is there an equivalent to in for C?

No, there is no direct alternative for in in C, and one cannot exist.

What is the C equivalent of a class?

There is nothing equivalent to classes . Its a totally different paradigm. You can use structures in C. Have to code accordingly to make structures do the job.

What is the equivalent of string in C?

There is no string type in C . You have to use char arrays.

Is there VAR in C?

The best thing I can do is using var , but the var keyword doesn't exist in C.


1 Answers

In Visual Studio 2008 there is no such equivalent. However with Visual Studio 2010 you can use the auto keyword to implement var like semantics in C++. I know this works with non-managed C++ and I'm fairly certain it works for C++/CLI as well.

like image 142
JaredPar Avatar answered Sep 24 '22 19:09

JaredPar