3.4 KiB
HPC# overview
Burst uses a high performance subset of C# called High Performance C# (HPC#).
Supported C# features in HPC#
HPC# supports most expressions and statements in C#. It supports the following:
Supported feature | Notes |
---|---|
Extension methods. | |
Instance methods of structs. | |
Unsafe code and pointer manipulation. | |
Loading from static read-only fields. | For more information, see the documentation on Static read-only fields and static constructors. |
Regular C# control flows. | if else switch case for while break continue |
ref and out parameters |
|
fixed statements |
|
Some IL opcodes. | cpblk initblk sizeof |
DLLImport and internal calls. |
For more information, see the documentation on DLLImport and internal calls. |
try and finally keywords. Burst also supports the associated IDisposable patterns, using and foreach . |
If an exception happens in Burst, the behavior is different from .NET. In .NET, if an exception occurs inside a try block, control flow goes to the finally block. However, in Burst, if an exception happens inside or outside a try block, the exception throws as if any finally blocks do not exist. |
Strings and ProfilerMarker . |
For more information, see the documentation on Support for Unity Profiler markers. |
throw expressions. |
Burst only supports simple throw patterns, for example, throw new ArgumentException("Invalid argument") . When you use simple patterns like this, Burst extracts the static string exception message and includes it in the generated code. |
Strings and Debug.Log . |
Only partially supported. For more information, see the documentation on String support and Debug.Log . |
Burst also provides alternatives for some C# constructions not directly accessible to HPC#:
- Function pointers as an alternative to using delegates within HPC#
- Shared static to access static mutable data from both C# and HPC#
Exception expressions
Burst supports throw
expressions for exceptions. Exceptions thrown in the editor can be caught by managed code, and are reported in the console window. Exceptions thrown in player builds will always cause the application to abort. Thus with Burst you should only use exceptions for exceptional behavior. To ensure that code doesn't end up relying on exceptions for things like general control flow, Burst produces the following warning on code that tries to throw
within a method not attributed with [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
:
Burst warning BC1370: An exception was thrown from a function without the correct [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")] guard. Exceptions only work in the editor and so should be protected by this guard
Unsupported C# features in HPC#
HPC# doesn't support the following C# features:
- Catching exceptions
catch
in atry
/catch
. - Storing to static fields except via Shared Static
- Any methods related to managed objects, for example, string methods.