views:

311

answers:

4

In Visual Studio > Build > Configuration Manager you can choose the target platform.

What does it change?

Is there any other way I can optimize my .NET app when targeting x64 platforms?

+1  A: 

Your C# / VB.Net etc is compiled into the correct IL for the platform, be it x86, x64 or whatever. There are no separate constucts in the language to direct code down either route, so the answer is no.

ck
+2  A: 

It doesn't "optimize" your app for 64-bit platforms. .NET assemblies will contain IL which is platform-independent by definition regardless of that setting. That setting simply flips a flag in the target assembly and makes it run on the specific version of the runtime.

Mehrdad Afshari
@Mehrdad And if I don't set this flag, it will run on the wrong version of the runtime? And it will loose some performance?
Jader Dias
@Jader: AnyCPU will load the 64 bit runtime on 64 bit OS and 32 bit runtime on 32 bit OS (or whatever the parent process is running in, if it's getting loaded in a process). x86 will force it to run in 32 bit CLR even in a 64 bit OS (in WOW64 mode). See http://msdn.microsoft.com/en-us/library/zekwfyz4(VS.80).aspx for more details.
Mehrdad Afshari
The IL code itself is not optimized for 64bit, but because of the greater number of registers in x64, the native code that the JIT compiler generates is potentially faster. The two JIT compilers (x86 and x64) are quite different in terms of their code generation capabilities. There is an interesting MSDN blog on the topic of native code generation from IL code: http://blogs.msdn.com/clrcodegeneration/default.aspx
Pepor
+4  A: 

As already mentioned, the IL in the .NET assembly is platform independent (that's what the Any CPU setting implies). The JIT (just-in-time) compiler of the .NET runtime will compile this platform independent byte code into platform specific native code with the optimization specific to that platform. So there is normally nothing you should worry about.

However, if you explicitly set your project to be build with x64 as the platform target the assembly will no longer run on an x86 runtime (and vice versa for x86 at the platform target). This is useful only if your code has dependencies to native x64/x86 libraries such as in-process COM components.

As Rowland added in a comment, the platform target must not be confused with the bitness of the underlying operating system. .NET assemblies with x86 as the platform target will run on both 32-bit and 64-bit versions of Windows (i.e. as a 32-bit process in WOW64 mode).

In fact, probably the most common scenario to use the platform target setting is when your .NET assembly has a reference to a 32-bit COM component. To be able to execute the assembly on an x64 system, the assembly must be compiled with the x86 flag. On a 64-bit OS and with the Any CPU setting enabled, the runtime would execute the assembly in a 64-bit process and loading the 32-bit COM component into the 64-bit process would fail. Compiling with the x86 flag causes the .NET runtime to execute the assembly in a 32-bit process and thus the COM component can safely be loaded.

0xA3
x86 .NET apps will run on x64, but not vice-versa.
Rowland Shaw
A: 

The default setup for a .NET application is Any CPU which means the assemblies will run on both 32 and 64 bit platforms. You can however specify either 32 bit or 64 bit to require either a 32 or 64 bit runtime.

Remember the IL code in the assemblies are JIT compiled when the application executes. So a Any CPU assembly will run on both 32 and 64 bit without further ado.

Brian Rasmussen