High Performance C# introduction
Read time 3 minutesLast updated 13 days ago
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. | N/A |
| Instance methods of structs. | N/A |
| Unsafe code and pointer manipulation. | N/A |
| Loading from static read-only fields. | For more information, see the documentation on Static read-only fields and static constructors. |
| Regular C# control flows. | |
| N/A |
| N/A |
| Some IL opcodes. | |
| For more information, see the documentation on |
| If an exception happens in Burst, the behavior is different from .NET. In .NET, if an exception occurs inside a |
Strings and | For more information, see the documentation on Support for Unity Profiler markers. |
| Burst only supports simple |
Strings and | Only partially supported. For more information, see the documentation on String support and |
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 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 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 within a method not attributed with :
throwthrow[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
Foreach and While
Burst supports invoking and . However, there is an edge case which is currently unsupported - methods that take one or more generic collection parameters and invoke or on at least one of the collections in the method body. To illustrate, the following example methods exemplify this limitation:
foreachwhileT: IEnumerable<U>foreachwhilepublic static void IterateThroughConcreteCollection(NativeArray<int> list){ foreach (var element in list) { // This works }}public static void IterateThroughGenericCollection<S>(S list) where S : struct, IEnumerable<int>{ foreach (var element in list) { // This doesn't work }}
Note that the uppermost method 's parameter is specified to be a concrete collection type, in this case . Because it's concrete iterating through it inside the method will compile in Burst. In the method below it, however, the parameter is specified to be a generic collection type . Iterating through inside the method will therefore not compile in Burst. It will instead throw the following error:
IterateThroughConcreteCollection()NativeArray<int>IterateThroughGenericCollection()SSCan't call the method (method name) on the generic interface object type (object name). This may be because you are trying to do a foreach over a generic collection of type IEnumerable.
Unsupported C# features in HPC#
HPC# doesn't support the following C# features:
- Catching exceptions in a
catch/try.catch - Storing to static fields except via Shared Static.
- Any methods related to managed objects, for example, string methods.