Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/policies/disallow-edits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ configuration:
- payloadType: Pull_Request
- isAction:
action: Opened
- targetsBranch:
branch: main
- or:
- filesMatchPattern:
pattern: xml/Microsoft.Extensions*/*
Expand Down
33 changes: 33 additions & 0 deletions snippets/csharp/System/NullReferenceException/Overview/example3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <Snippet12>
using System;
using System.Collections.Generic;
using System.Collections;
using System.Runtime.Serialization;

public class NullReferenceExample
{
public static void Main()
{
var listType = GetListType();
_ = GetList(listType);
}

private static Type GetListType()
{
return typeof(List<int>);
}

private static IList GetList(Type type)
{
var emptyList = (IList)FormatterServices.GetUninitializedObject(type); // Does not call list constructor
var value = 1;
emptyList.Add(value);
return emptyList;
}
}
// The example displays output like the following:
// Unhandled Exception: System.NullReferenceException: 'Object reference
// not set to an instance of an object.'
// at System.Collections.Generic.List`1.System.Collections.IList.Add(Object item)
// at NullReferenceExample.GetList(Type type): line 24
// </Snippet12>
5 changes: 4 additions & 1 deletion xml/System.Runtime.InteropServices/RuntimeInformation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ The property returns a string that indicates the name of the currently executing

## Remarks

The returned value is intended to represent the actual architecture of the underlying operating system. It is a best effort to ignore the architecture emulation infrastructure that may be involved to run the process. The returned value takes into account emulation built into Windows and macOS operating systems. The returned value does not take into account emulation using QEMU that is typically used on Linux operating system.
The returned value is intended to represent the actual architecture of the underlying operating system. It's a best effort to ignore the architecture emulation infrastructure that might be involved to run the process. The returned value takes into account emulation built into Windows and macOS operating systems. The returned value does not take into account emulation using QEMU that's typically used on Linux.

> [!NOTE]
> The behavior of this property for emulated processes changed, starting in .NET 7. For more information, see [RuntimeInformation.OSArchitecture under emulation](/dotnet/core/compatibility/interop/7.0/osarchitecture-emulation).

]]></format>
</remarks>
Expand Down
6 changes: 6 additions & 0 deletions xml/System/NullReferenceException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ A <xref:System.NullReferenceException> exception is thrown when you try to acces

To address this issue, make sure that the argument passed to the method is not `null`, or handle the thrown exception in a `try…catch…finally` block. For more information, see [Exceptions](/dotnet/standard/exceptions/).

- A list is created without knowing the type, and the list was not initialized. The `GetList` method in the following example throws the exception at the line `emptyList.Add(value)`.

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/example3.cs" id="Snippet12":::

To address this issue, make sure that the list is initialized (one way to do this is to call `Activator.CreateInstance` instead of `FormatterServices.GetUninitializedObject`), or handle the thrown exception in a `try…catch…finally` block. For more information, see [Exceptions](/dotnet/standard/exceptions/).

The following Microsoft intermediate language (MSIL) instructions throw <xref:System.NullReferenceException>: `callvirt`, `cpblk`, `cpobj`, `initblk`, `ldelem.<type>`, `ldelema`, `ldfld`, `ldflda`, `ldind.<type>`, `ldlen`, `stelem.<type>`, `stfld`, `stind.<type>`, `throw`, and `unbox`.

<xref:System.NullReferenceException> uses the HRESULT `COR_E_NULLREFERENCE`, which has the value 0x80004003.
Expand Down