diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 2ef465f8e10f9..abe2661b9fba2 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -343,6 +343,10 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0188.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/feature-version-errors" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0233.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0234.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/assembly-references" @@ -431,10 +435,26 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1502.md", "redirect_url": "/dotnet/csharp/misc/cs1503" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1656.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1683.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/assembly-references" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1708.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1716.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1919.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1691.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/preprocessor-errors" @@ -475,6 +495,10 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1988.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs4004.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs3007.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -543,6 +567,10 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8795.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/partial-types" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8812.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8817.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/partial-types" @@ -1597,6 +1625,14 @@ "source_path_from_root": "/docs/csharp/misc/cs0192.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0193.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0196.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0199.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" @@ -1605,6 +1641,34 @@ "source_path_from_root": "/docs/csharp/misc/cs0206.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0208.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0209.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0210.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0211.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0212.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0213.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0214.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0215.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" @@ -1613,6 +1677,30 @@ "source_path_from_root": "/docs/csharp/misc/cs0216.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0227.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0242.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0244.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0254.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0459.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0821.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0217.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" @@ -2153,6 +2241,14 @@ "source_path_from_root": "/docs/csharp/misc/cs1637.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/iterator-yield" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1641.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1642.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1649.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" @@ -2165,6 +2261,18 @@ "source_path_from_root": "/docs/csharp/misc/cs1655.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1663.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1665.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1666.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/unsafe-code-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1657.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" diff --git a/docs/csharp/language-reference/compiler-messages/cs0233.md b/docs/csharp/language-reference/compiler-messages/cs0233.md deleted file mode 100644 index 7fa6c64201389..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0233.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -description: "Compiler Error CS0233" -title: "Compiler Error CS0233" -ms.date: 07/20/2015 -f1_keywords: - - "CS0233" -helpviewer_keywords: - - "CS0233" -ms.assetid: 75b0123f-2237-43dc-9234-a0f727eee482 ---- -# Compiler Error CS0233 - -'identifier' does not have a predefined size, therefore sizeof can only be used in an unsafe context - - Without [unsafe](../keywords/unsafe.md) context, the [sizeof](../operators/sizeof.md) operator can only be used for types whose size is a compile-time constant. If you are getting this error, use an unsafe context. - -The following example generates CS0233: - -```csharp -// CS0233.cs -using System; -using System.Runtime.InteropServices; - -[StructLayout(LayoutKind.Sequential)] -public struct S -{ - public int a; -} - -public class MyClass -{ - public static void Main() - { - S myS = new S(); - Console.WriteLine(sizeof(S)); // CS0233 - // Try the following instead: - // unsafe - // { - // Console.WriteLine(sizeof(S)); - // } - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs1656.md b/docs/csharp/language-reference/compiler-messages/cs1656.md deleted file mode 100644 index 12912e08fe9dd..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1656.md +++ /dev/null @@ -1,117 +0,0 @@ ---- -description: "Compiler Error CS1656" -title: "Compiler Error CS1656" -ms.date: 07/20/2015 -f1_keywords: - - "CS1656" -helpviewer_keywords: - - "CS1656" -ms.assetid: b5463a12-d685-4dae-9f88-08383e271b7a ---- -# Compiler Error CS1656 - -Cannot assign to 'variable' because it is a 'read-only variable type' - - This error occurs when an assignment to variable occurs in a read-only context. Read-only contexts include [foreach](../statements/iteration-statements.md#the-foreach-statement) iteration variables, [using](../statements/using.md) variables, and [fixed](../statements/fixed.md) variables. To resolve this error, avoid assignments to a statement variable in `using` blocks, `foreach` statements, and `fixed` statements. - -## Example 1 - - The following example generates error CS1656 because it tries to replace complete elements of a collection inside a `foreach` loop. One way to work around the error is to change the `foreach` loop to a [for](../statements/iteration-statements.md#the-for-statement) loop. Another way, not shown here, is to modify the members of the existing element; this is possible with classes, but not with structs. - -```csharp -using System; -using System.Collections; -using System.Collections.Generic; -using System.Text; - -namespace CS1656_2 -{ - - class Book - { - public string Title; - public string Author; - public double Price; - public Book(string t, string a, double p) - { - Title=t; - Author=a; - Price=p; - - } - } - - class Program - { - private List list; - static void Main(string[] args) - { - Program prog = new Program(); - prog.list = new List(); - prog.list.Add(new Book ("The C# Programming Language", - "Hejlsberg, Wiltamuth, Golde", - 29.95)); - prog.list.Add(new Book ("The C++ Programming Language", - "Stroustrup", - 29.95)); - prog.list.Add(new Book ("The C Programming Language", - "Kernighan, Ritchie", - 29.95)); - foreach(Book b in prog.list) - { - // Cannot modify an entire element in a foreach loop - // even with reference types. - // Use a for or while loop instead - if (b.Title == "The C Programming Language") - // Cannot assign to 'b' because it is a 'foreach - // iteration variable' - b = new Book("Programming Windows, 5th Ed.", "Petzold", 29.95); //CS1656 - } - - //With a for loop you can modify elements - //for(int x = 0; x < prog.list.Count; x++) - //{ - // if(prog.list[x].Title== "The C Programming Language") - // prog.list[x] = new Book("Programming Windows, 5th Ed.", "Petzold", 29.95); - //} - //foreach(Book b in prog.list) - // Console.WriteLine(b.Title); - - } - } -} -``` - -## Example 2 - - The following sample demonstrates how CS1656 can be generated in other contexts besides a `foreach` loop: - -```csharp -// CS1656.cs -// compile with: /unsafe -using System; - -class C : IDisposable -{ - public void Dispose() { } -} - -class CMain -{ - unsafe public static void Main() - { - using (C c = new C()) - { - // Cannot assign to 'c' because it is a 'using variable' - c = new C(); // CS1656 - } - - int[] ary = new int[] { 1, 2, 3, 4 }; - fixed (int* p = ary) - { - // Cannot assign to 'p' because it is a 'fixed variable' - p = null; // CS1656 - } - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs1708.md b/docs/csharp/language-reference/compiler-messages/cs1708.md deleted file mode 100644 index 2206ff565af1c..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1708.md +++ /dev/null @@ -1,59 +0,0 @@ ---- -description: "Compiler Error CS1708" -title: "Compiler Error CS1708" -ms.date: 07/20/2015 -f1_keywords: - - "CS1708" -helpviewer_keywords: - - "CS1708" -ms.assetid: df2fc580-2de5-4dd0-9ba3-b41248de5f41 ---- -# Compiler Error CS1708 - -Fixed size buffers can only be accessed through locals or fields - - A new feature in C# 2.0 is the ability to define an in-line array inside of a `struct`. Such arrays can only be accessed via local variables or fields, and may not be referenced as intermediate values on the left-hand side of an expression. Also, the arrays cannot be accessed by fields that are `static` or `readonly`. - - To resolve this error, define an array variable, and assign the in-line array to the variable. Or, remove the `static` or `readonly` modifier from the field representing the in-line array. - -## Example - - The following sample generates CS1708. - -```csharp -// CS1708.cs -// compile with: /unsafe -using System; - -unsafe public struct S -{ - public fixed char name[10]; -} - -public unsafe class C -{ - public S UnsafeMethod() - { - S myS = new S(); - return myS; - } - - static void Main() - { - C myC = new C(); - myC.UnsafeMethod().name[3] = 'a'; // CS1708 - // Uncomment the following 2 lines to resolve: - // S myS = myC.UnsafeMethod(); - // myS.name[3] = 'a'; - - // The field cannot be static. - C._s1.name[3] = 'a'; // CS1708 - - // The field cannot be readonly. - myC._s2.name[3] = 'a'; // CS1708 - } - - static readonly S _s1; - public readonly S _s2; -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs1716.md b/docs/csharp/language-reference/compiler-messages/cs1716.md deleted file mode 100644 index 8ccf9f1aad46d..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1716.md +++ /dev/null @@ -1,49 +0,0 @@ ---- -description: "Compiler Error CS1716" -title: "Compiler Error CS1716" -ms.date: 07/20/2015 -f1_keywords: - - "CS1716" -helpviewer_keywords: - - "CS1716" -ms.assetid: c9e65274-0cc3-41a6-967c-ac1804ecf3ba ---- -# Compiler Error CS1716 - -Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead. - - This error arises in an unsafe code section that contains a fixed-size array declaration similar to a field declaration. Do not use this attribute. Instead, use the keyword `fixed`. - -## Example - - The following example generates CS1716. - -```csharp -// CS1716.cs -// compile with: /unsafe -using System; -using System.Runtime.CompilerServices; - -public struct UnsafeStruct -{ - [FixedBuffer(typeof(int), 4)] // CS1716 - unsafe public int aField; - // Use this single line instead of the above two lines. - // unsafe public fixed int aField[4]; -} - -public class TestUnsafe -{ - static int Main() - { - UnsafeStruct us = new UnsafeStruct(); - unsafe - { - if (us.aField[0] == 0) - return us.aField[1]; - else - return us.aField[2]; - } - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs1919.md b/docs/csharp/language-reference/compiler-messages/cs1919.md deleted file mode 100644 index 6a51106991948..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1919.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -description: "Compiler Error CS1919" -title: "Compiler Error CS1919" -ms.date: 07/20/2015 -f1_keywords: - - "CS1919" -helpviewer_keywords: - - "CS1919" -ms.assetid: 5d1e468b-ddec-4edd-a8b1-9c65c2332778 ---- -# Compiler Error CS1919 - -Unsafe type 'type name' cannot be used in object creation. - - The `new` operator creates objects only on the managed heap. However, you can create objects in unmanaged memory indirectly by using the interoperability capabilities of the language to call native methods that return pointers. - -## To correct this error - -1. Use a safe type in the new object creation expression. For example, use `char` or `int` instead of `char*` or `int*`. - -2. If you must create objects in unmanaged memory, use a Win32 or COM method or else write your own function in C or C++ and call it from C#. - -## Example - - The following example generates CS1919 because a pointer type is unsafe: - -```csharp -// cs1919.cs -// Compile with: /unsafe -unsafe public class C -{ - public static int Main() - { - var col1 = new int* { }; // CS1919 - var col2 = new char* { }; // CS1919 - return 1; - } -} -``` - -## See also - -- [Interoperability](../../advanced-topics/interop/index.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs4004.md b/docs/csharp/language-reference/compiler-messages/cs4004.md deleted file mode 100644 index 01fabcd46276b..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs4004.md +++ /dev/null @@ -1,88 +0,0 @@ ---- -description: "Compiler Error CS4004" -title: "Compiler Error CS4004" -ms.date: 07/01/2024 -f1_keywords: - - "CS4004" -helpviewer_keywords: - - "CS4004" ---- -# Compiler Error CS4004 - -Cannot await in an unsafe context - -## Example - -The following sample generates CS4004: - -```csharp -using System.Threading.Tasks; - -public static class C -{ - public static unsafe async Task ReverseTextAsync(string text) - { - return await Task.Run(() => - { - if (string.IsNullOrEmpty(text)) - { - return text; - } - - fixed (char* pText = text) - { - char* pStart = pText; - char* pEnd = pText + text.Length - 1; - for (int i = text.Length / 2; i >= 0; i--) - { - char temp = *pStart; - *pStart++ = *pEnd; - *pEnd-- = temp; - } - - return text; - } - }); - } -} -``` - -This code generates an error in C# 13 because the `await` is in the `unsafe` block. - -The `ReverseText` method naively uses a background task to asynchronously create a new string in reverse order of a given string. - -## To correct this error - -Separating the unsafe code from the awaitable code corrects this error. One separation technique is creating a new method for the unsafe code and then calling it from the awaitable code. For example: - -```csharp -public static class C -{ - public static async Task ReverseTextAsync(string text) - { - return await Task.Run(() => ReverseTextUnsafe(text)); - } - - private static unsafe string ReverseTextUnsafe(string text) - { - if (string.IsNullOrEmpty(text)) - { - return text; - } - - fixed (char* pText = text) - { - char* pStart = pText; - char* pEnd = pText + text.Length - 1; - for (int i = text.Length / 2; i >= 0; i--) - { - char temp = *pStart; - *pStart++ = *pEnd; - *pEnd-- = temp; - } - - return text; - } - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs8812.md b/docs/csharp/language-reference/compiler-messages/cs8812.md deleted file mode 100644 index f7566041d3cbf..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs8812.md +++ /dev/null @@ -1,52 +0,0 @@ ---- -description: "Compiler Error CS8812" -title: "Compiler Error CS8812" -ms.date: 9/12/2022 -f1_keywords: - - "CS8812" -helpviewer_keywords: - - "CS8812" ---- -# Compiler Error CS8812 - -Cannot convert `&Method` group to non-function pointer type. - -The address of an expression (for example, `&Method`) has no type and thus cannot be assigned to a non-function pointer variable. - -The `&` operator is the [address-of](../operators/pointer-related-operators.md#address-of-operator-) operator, used to return the address of its operand. - -## Example - - The following sample generates CS8812: - -```csharp -// CS8812.cs (6,22) - -unsafe class C -{ - static void Method() - { - void* ptr1 = &Method; - } -} -``` - -## To correct this error - -Explicitly convert the expression to the required type (for example, a `void` `delegate`): - -```csharp -unsafe class C -{ - static void Method() - { - void* ptr1 = (delegate*)&Method; - } -} -``` - -## See also - -- [Shouldn't method addresses be implicitly convertible to void* and thus allowing direct casts to function pointers with mismatched signatures?](https://github.com/dotnet/csharplang/discussions/5720) -- [Casting function pointer directly to void and then a function pointer type causes crash.](https://github.com/dotnet/roslyn/issues/44489) -- [Address-of operator &](../operators/pointer-related-operators.md#address-of-operator-) diff --git a/docs/csharp/language-reference/compiler-messages/unsafe-code-errors.md b/docs/csharp/language-reference/compiler-messages/unsafe-code-errors.md new file mode 100644 index 0000000000000..0e010be079538 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/unsafe-code-errors.md @@ -0,0 +1,194 @@ +--- +title: Resolve errors and warnings related to using unsafe code +description: These compiler errors and warnings indicate errors when you work with `unsafe` code. Unsafe code must be enabled with the `unsafe` keyword. Unsafe code allows constructs that can introduce memory based errors. These warnings and errors explain common misuses. +f1_keywords: + - "CS0193" + - "CS0196" + - "CS0208" + - "CS0209" + - "CS0210" + - "CS0211" + - "CS0212" + - "CS0213" + - "CS0214" + - "CS0227" + - "CS0233" + - "CS0242" + - "CS0244" + - "CS0254" + - "CS0459" + - "CS0821" + - "CS1641" + - "CS1642" + - "CS1656" + - "CS1663" + - "CS1665" + - "CS1666" + - "CS1708" + - "CS4004" + - "CS1716" + - "CS1919" + - "CS8812" + - "CS9123" +helpviewer_keywords: + - "CS0193" + - "CS0196" + - "CS0208" + - "CS0209" + - "CS0210" + - "CS0211" + - "CS0212" + - "CS0213" + - "CS0214" + - "CS0227" + - "CS0233" + - "CS0242" + - "CS0244" + - "CS0254" + - "CS0459" + - "CS0821" + - "CS1641" + - "CS1642" + - "CS1656" + - "CS1663" + - "CS1665" + - "CS1666" + - "CS1708" + - "CS1716" + - "CS4004" + - "CS1919" + - "CS8812" + - "CS9123" +ms.date: 10/21/2025 +ai-usage: ai-assisted +--- +# Resolve errors and warnings in unsafe code constructs + +This article covers the following compiler errors: + + +- [**CS0193**](#pointer-operations-and-dereferencing): *The \* or -> operator must be applied to a data pointer* +- [**CS0196**](#pointer-operations-and-dereferencing): *A pointer must be indexed by only one value* +- [**CS0208**](#pointer-types-and-managed-types): *Cannot take the address of, get the size of, or declare a pointer to a managed type ('type')* +- [**CS0209**](#fixed-buffers): *The type of local declared in a fixed statement must be a pointer type* +- [**CS0210**](#fixed-buffers): *You must provide an initializer in a fixed or `using` statement declaration* +- [**CS0211**](#fixed-buffers): *Cannot take the address of the given expression* +- [**CS0212**](#fixed-buffers): *You can only take the address of an unfixed expression inside of a fixed statement initializer* +- [**CS0213**](#fixed-buffers): *You cannot use the fixed statement to take the address of an already fixed expression* +- [**CS0214**](#unsafe-context-restrictions): *Pointers and fixed size buffers may only be used in an unsafe context* +- [**CS0227**](#unsafe-context-restrictions): *Unsafe code may only appear if compiling with /unsafe* +- [**CS0233**](#pointer-types-and-managed-types): *'identifier' does not have a predefined size, therefore sizeof can only be used in an unsafe context* +- [**CS0242**](#pointer-operations-and-dereferencing): *The operation in question is undefined on void pointers* +- [**CS0244**](#unsafe-context-restrictions): *Neither '`is`' nor '`as`' is valid on pointer types* +- [**CS0254**](#fixed-buffers): *The right hand side of a fixed statement assignment may not be a cast expression* +- [**CS0459**](#fixed-buffers): *Cannot take the address of a read-only local variable* +- [**CS0821**](#fixed-buffers): *Implicitly typed locals cannot be fixed* +- [**CS1641**](#fixed-size-buffers): *A fixed size buffer field must have the array size specifier after the field name* +- [**CS1642**](#fixed-size-buffers): *Fixed size buffer fields may only be members of structs.* +- [**CS1656**](#fixed-buffers): *Cannot assign to 'variable' because it is a 'read-only variable type'* +- [**CS1663**](#fixed-size-buffers): *Fixed size buffer type must be one of the following: `bool`, `byte`, `short`, `int`, `long`, `char`, `sbyte`, `ushort`, `uint`, `ulong`, `float` or `double`* +- [**CS1665**](#fixed-size-buffers): *Fixed size buffers must have a length greater than zero* +- [**CS1666**](#fixed-size-buffers): *You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement.* +- [**CS1708**](#fixed-size-buffers): *Fixed size buffers can only be accessed through locals or fields* +- [**CS1716**](#fixed-size-buffers): *Do not use '`System.Runtime.CompilerServices.FixedBuffer`' attribute. Use the 'fixed' field modifier instead.* +- [**CS1919**](#unsafe-context-restrictions): *Unsafe type 'type name' cannot be used in object creation.* +- [**CS4004**](#unsafe-context-restrictions): *Cannot `await` in an unsafe context* +- [**CS8812**](#function-pointers): *Cannot convert `&Method` group to non-function pointer type.* +- [**CS9123**](#unsafe-context-restrictions): *The '`&`' operator should not be used on parameters or local variables in async methods.* + +## Pointer operations and dereferencing + +- **CS0193**: *The `*` or `->` operator must be applied to a data pointer* +- **CS0196**: *A pointer must be indexed by only one value* +- **CS0242**: *The operation in question is undefined on void pointers* + +To use pointer operations correctly, follow the rules for dereferencing, indexing, and arithmetic operations. For more information, see [Pointer types](../unsafe-code.md#pointer-types) and [Function pointers](../unsafe-code.md#function-pointers). + +- Apply the `*` or `->` operator only to data pointers (**CS0193**). Don't use these operators with nonpointer types or function pointers. Function pointers can't be dereferenced in C#, unlike in C/C++. +- Index pointers with only one value (**CS0196**). Multidimensional indexing isn't supported on pointers. +- Avoid operations that are undefined on void pointers (**CS0242**). For example, don't increment a void pointer because the compiler doesn't know the size of the data being pointed to. + +## Pointer types and managed types + +- **CS0208**: *Cannot take the address of, get the size of, or declare a pointer to a managed type ('type')* +- **CS0233**: *'identifier' does not have a predefined size, therefore sizeof can only be used in an unsafe context* + +To work with pointers and the `sizeof` operator correctly, use unmanaged types and proper contexts. For more information, see [Unmanaged types](../builtin-types/unmanaged-types.md) and the [`sizeof` operator](../operators/sizeof.md). + +- Use pointers only with unmanaged types (**CS0208**). Don't take the address of, get the size of, or declare pointers to managed types. Managed types include reference types and structs that contain reference type fields or properties. +- Use the [`sizeof`](../operators/sizeof.md) operator within an [`unsafe`](../keywords/unsafe.md) context when working with types whose size isn't a compile-time constant (**CS0233**). + +## Fixed buffers + +- **CS0209**: *The type of local declared in a fixed statement must be a pointer type* +- **CS0210**: *You must provide an initializer in a fixed or `using` statement declaration* +- **CS0211**: *Cannot take the address of the given expression* +- **CS0212**: *You can only take the address of an unfixed expression inside of a fixed statement initializer* +- **CS0213**: *You cannot use the fixed statement to take the address of an already fixed expression* +- **CS0254**: *The right hand side of a fixed statement assignment may not be a cast expression* +- **CS0459**: *Cannot take the address of a read-only local variable* +- **CS0821**: *Implicitly typed locals cannot be fixed* +- **CS1656**: *Cannot assign to 'variable' because it is a 'read-only variable type'* + +These errors occur when you use the [`fixed` statement](../statements/fixed.md) incorrectly. The `fixed` statement prevents the garbage collector from relocating a movable variable and declares a pointer to that variable. For more information, see [Unsafe Code and Pointers](../unsafe-code.md). + +To use the `fixed` statement correctly: + +- Declare the variable as a pointer type (**CS0209**). +- Provide an initializer in the `fixed` statement declaration (**CS0210**). +- Take the address only of valid expressions: fields, local variables, and pointer indirection (**CS0211**). Don't take the address of computed expressions like the sum of two variables. +- Use the address-of operator on unfixed expressions only within the `fixed` statement initializer (**CS0212**). +- Don't use a `fixed` statement on already-fixed expressions (**CS0213**). Local variables and parameters in an `unsafe` method are already fixed on the stack. +- Don't use cast expressions on the right side of a `fixed` statement assignment (**CS0254**). +- Don't take the address of read-only local variables (**CS0459**). Variables in `foreach` loops, `using` statements, and `fixed` statements are read-only. +- Use explicit types instead of `var` in `fixed` statements (**CS0821**). +- Don't assign to variables in read-only contexts like `foreach` loops, `using` statements, or `fixed` statements (**CS1656**). + +## Unsafe context restrictions + +- **CS0214**: *Pointers and fixed size buffers may only be used in an unsafe context* +- **CS0227**: *Unsafe code may only appear if compiling with /unsafe* +- **CS0244**: *Neither 'is' nor 'as' is valid on pointer types* +- **CS1919**: *Unsafe type 'type name' cannot be used in object creation* +- **CS4004**: *Cannot await in an unsafe context* +- **CS9123**: *The '&' operator should not be used on parameters or local variables in async methods* + +These errors occur when you use unsafe code constructs without proper unsafe context or when you attempt operations that aren't allowed in unsafe code. For more information, see [Unsafe Code and Pointers](../unsafe-code.md) and the [`unsafe` keyword](../keywords/unsafe.md). + +To use unsafe code correctly: + +- Mark methods, types, or code blocks that use pointers or fixed-size buffers with the `unsafe` keyword (**CS0214**). +- Enable the [**AllowUnsafeBlocks**](../compiler-options/language.md#allowunsafeblocks) compiler option in your project settings when using the `unsafe` keyword (**CS0227**). +- Don't use the [`is`](../operators/type-testing-and-cast.md#the-is-operator) or [`as`](../operators/type-testing-and-cast.md#the-as-operator) operators with pointer types (**CS0244**). These type-testing operators aren't valid for pointers. +- Don't use the `new` operator to create pointer type instances (**CS1919**). To create objects in unmanaged memory, use interop to call native methods that return pointers. +- Keep unsafe code separate from async code (**CS4004**). Create separate methods for unsafe operations and call them from async methods. +- Don't use the address-of operator (`&`) on parameters or local variables in async methods (**CS9123**). The variable may not exist when the async operation completes. + +## Fixed-size buffers + +- **CS1641**: *A fixed size buffer field must have the array size specifier after the field name* +- **CS1642**: *Fixed size buffer fields may only be members of structs* +- **CS1663**: *Fixed size buffer type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double* +- **CS1665**: *Fixed size buffers must have a length greater than zero* +- **CS1666**: *You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement* +- **CS1708**: *Fixed size buffers can only be accessed through locals or fields* +- **CS1716**: *Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead* + +These errors occur when you work with fixed-size buffers. Fixed-size buffers are arrays embedded directly in structs and are primarily used for interop scenarios. For more information, see [Fixed-size buffers](../unsafe-code.md#fixed-size-buffers). + +To declare and use fixed-size buffers correctly: + +- Specify the array size after the field name using a positive integer constant (**CS1641**, **CS1665**). +- Declare fixed-size buffers only in structs, not in classes (**CS1642**). Use a regular array if you need the field in a class. +- Use one of the supported element types: `bool`, `byte`, `short`, `int`, `long`, `char`, `sbyte`, `ushort`, `uint`, `ulong`, `float`, or `double` (**CS1663**). +- Use a `fixed` statement to pin the containing struct before accessing the buffer (**CS1666**). +- Access fixed-size buffers only through locals or fields, not through intermediate expressions (**CS1708**). +- Use the `fixed` field modifier instead of the `System.Runtime.CompilerServices.FixedBuffer` attribute (**CS1716**). + +## Function pointers + +- **CS8812**: *Cannot convert `&Method` group to non-function pointer type* + +To obtain a function pointer, use the address-of operator with an explicit function pointer type cast. Don't use the [address-of operator `&`](../operators/pointer-related-operators.md#address-of-operator-) to assign method groups to `void*` or other non-function pointer types. For more information, see [Function pointers](../unsafe-code.md#function-pointers). diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 5deb522f27944..801dd5dafa144 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -463,140 +463,173 @@ items: - name: Preprocessor errors href: ./compiler-messages/preprocessor-errors.md displayName: > - CS1024, CS1025, CS1027, CS1028, CS1029, CS1030, CS1032, CS1038, CS1040, CS1517, CS1560, CS1576, CS1578, CS1633, CS1634, - CS1635, CS1691, CS1692, CS1694, CS1695, CS1696, CS1709, CS9297, CS9298, CS9299, CS9314 + CS1024, CS1025, CS1027, CS1028, CS1029, CS1030, CS1032, CS1038, CS1040, CS1517, + CS1560, CS1576, CS1578, CS1633, CS1634, CS1635, CS1691, CS1692, CS1694, CS1695, + CS1696, CS1709, CS9297, CS9298, CS9299, CS9314 - name: Feature or version missing href: ./compiler-messages/feature-version-errors.md displayName: > - CS0171, CS0188, CS0843, CS8904, CS1738, CS8022, CS8023, CS8024, CS8025, CS8026, CS8058, CS8059, CS8107, CS8192, CS8302, - CS8303, CS8304, CS8305, CS8306, CS8314, CS8320, CS8370, CS8371, CS8400, CS8401, CS8511, CS8627, CS8630, CS8652, CS8703, - CS8704, CS8706, CS8773, CS8912, CS8919, CS8929, CS8936, CS8957, CS8967, CS9014, CS9015, CS9016, CS9017, CS9058, CS9064, - CS9103, CS9171, CS9194, CS9202, CS9204, CS9240, CS9260, CS9268, CS9269, CS9271 + CS0171, CS0188, CS0843, CS8904, CS1738, CS8022, CS8023, CS8024, CS8025, CS8026, + CS8058, CS8059, CS8107, CS8192, CS8302, CS8303, CS8304, CS8305, CS8306, CS8314, + CS8320, CS8370, CS8371, CS8400, CS8401, CS8511, CS8627, CS8630, CS8652, CS8703, + CS8704, CS8706, CS8773, CS8912, CS8919, CS8929, CS8936, CS8957, CS8967, CS9014, + CS9015, CS9016, CS9017, CS9058, CS9064, CS9103, CS9171, CS9194, CS9202, CS9204, + CS9240, CS9260, CS9268, CS9269, CS9271 - name: Assembly references href: ./compiler-messages/assembly-references.md displayName: > - CS0012, CS0234, CS0246, CS0400, CS1068, CS1069, CS1070, CS1704, CS1683, CS1714, CS1760, CS7008, CS7068, CS7069, - CS7071, CS7079, CS8090, CS8203, CS9286 + CS0012, CS0234, CS0246, CS0400, CS1068, CS1069, CS1070, CS1704, CS1683, CS1714, + CS1760, CS7008, CS7068, CS7069, CS7071, CS7079, CS8090, CS8203, CS9286 - name: Constructor declarations href: ./compiler-messages/constructor-errors.md displayName: > Primary constructors, - CS0514, CS0515, CS0516, CS0517, CS0522, CS0526, CS0568, CS0710, CS0768, CS0824, CS8054, CS8091, CS8358, CS8862, CS8867, - CS8868, CS8878, CS8910, CS8958, CS8982, CS8983, CS9105, CS9106, CS9107, CS9108, CS9109, CS9110, CS9111, CS9112, CS9113, - CS9114, CS9115, CS9116, CS9117, CS9118, CS9119, CS9120, CS9121, CS9122, CS9124, CS9136, CS9179 + CS0514, CS0515, CS0516, CS0517, CS0522, CS0526, CS0568, CS0710, CS0768, CS0824, + CS8054, CS8091, CS8358, CS8862, CS8867, CS8868, CS8878, CS8910, CS8958, CS8982, + CS8983, CS9105, CS9106, CS9107, CS9108, CS9109, CS9110, CS9111, CS9112, CS9113, + CS9114, CS9115, CS9116, CS9117, CS9118, CS9119, CS9120, CS9121, CS9122, CS9124, + CS9136, CS9179 - name: Operator declarations href: ./compiler-messages/overloaded-operator-errors.md displayName: > operator, overloaded operator, user defined operator, - CS0056, CS0057, CS0215, CS0216, CS0217, CS0218, CS0448, CS0552, CS0553, CS0554, CS0555, CS0556, CS0557, CS0558, CS0559, - CS0562, CS0563, CS0564, CS0567, CS0590, CS0660, CS0661, CS0715, CS1037, CS1553, CS1554, CS8930, CS8931, CS9023, CS9024, + CS0056, CS0057, CS0215, CS0216, CS0217, CS0218, CS0448, CS0552, CS0553, CS0554, + CS0555, CS0556, CS0557, CS0558, CS0559, CS0562, CS0563, CS0564, CS0567, CS0590, + CS0660, CS0661, CS0715, CS1037, CS1553, CS1554, CS8930, CS8931, CS9023, CS9024, CS9025, CS9308, CS9310, CS9311, CS9312, CS9313 - name: Parameter / argument mismatch href: ./compiler-messages/parameter-argument-mismatch.md displayName: > - CS0182, CS0591, CS0599, CS0617, CS0633, CS0643, CS0655, CS0839, CS1016, CS1739, CS1740, CS1742, CS1744, CS1746, CS7036, - CS7067, CS8196, CS8324, CS8861, CS8905, CS8943, CS8944, CS8945, CS8948, CS8949, CS8950, CS8951, CS8964, CS8965, CS8966 + CS0182, CS0591, CS0599, CS0617, CS0633, CS0643, CS0655, CS0839, CS1016, CS1739, + CS1740, CS1742, CS1744, CS1746, CS7036, CS7067, CS8196, CS8324, CS8861, CS8905, + CS8943, CS8944, CS8945, CS8948, CS8949, CS8950, CS8951, CS8964, CS8965, CS8966 - name: Reference parameters href: ./compiler-messages/ref-modifiers-errors.md displayName: > ref safety, - CS0192, CS0199, CS0206, CS0631, CS0767, CS1510, CS1605, CS1623, CS1649, CS1651, CS1655, CS1657, CS1741, CS1939, CS1988, - CS7084, CS8166, CS8167, CS8168, CS8169. CS8325, CS8326, CS8327, CS8329, CS8330, CS8331, CS8332, CS8337, CS8338, CS8351, - CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, CS9087, CS9089, CS9091, CS9092, CS9093, - CS9094, CS9095, CS9096, CS9097, CS9101, CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197, CS9198, + CS0192, CS0199, CS0206, CS0631, CS0767, CS1510, CS1605, CS1623, CS1649, CS1651, + CS1655, CS1657, CS1741, CS1939, CS1988, CS7084, CS8166, CS8167, CS8168, CS8169, + CS8325, CS8326, CS8327, CS8329, CS8330, CS8331, CS8332, CS8337, CS8338, CS8351, + CS8373, CS8374, CS8388, CS8977, CS9072, CS9077, CS9078, CS9079, CS9085, CS9086, + CS9087, CS9089, CS9091, CS9092, CS9093, CS9094, CS9095, CS9096, CS9097, CS9101, + CS9102, CS9104, CS9190, CS9191, CS9192, CS9193, CS9195, CS9196, CS9197, CS9198, CS9199, CS9200, CS9201, CS9205, CS9265 - name: "`ref struct` types" href: ./compiler-messages/ref-struct-errors.md displayName: > ref struct, - CS8343, CS8344, CS8345, CS9048, CS9050, CS9059, CS9241, CS9242, CS9243, CS9244, CS9245, CS9246, CS9247, CS9267 + CS8343, CS8344, CS8345, CS9048, CS9050, CS9059, CS9241, CS9242, CS9243, CS9244, + CS9245, CS9246, CS9247, CS9267 - name: Iterator methods href: ./compiler-messages/iterator-yield.md displayName: > yield return, yield break, - CS1622, CS1624, CS1625, CS1626, CS1627, CS1629, CS1631, CS1637, CS4013, CS8154, CS8176, CS9237, CS9238, CS9239 + CS1622, CS1624, CS1625, CS1626, CS1627, CS1629, CS1631, CS1637, CS4013, CS8154, + CS8176, CS9237, CS9238, CS9239 - name: Extension declarations href: ./compiler-messages/extension-declarations.md displayName: > - CS1100, CS1101, CS1102, CS1103, CS1105, CS1106, CS1109, CS1110, CS1113, CS1112, CS1100, CS1101, CS1102, CS1103, - CS1105, CS1106, CS1109, CS1110, CS1112, CS1113, CS1754, CS9281, CS9282, CS9283, CS9284, CS9285, CS9287, CS9288, - CS9289, CS9290, CS9292, CS9293, CS9295, CS9300, CS9301, CS9302, CS9303, CS9304, CS9305, CS9306, CS9309, CS9316, - CS9317, CS9318, CS9319, CS9320, CS9321, CS9322, CS9323, CS9326, CS9329 + CS1100, CS1101, CS1102, CS1103, CS1105, CS1106, CS1109, CS1110, CS1112, CS1113, + CS1743, CS9281, CS9282, CS9283, CS9284, CS9285, CS9287, CS9288, CS9289, CS9290, + CS9292, CS9293, CS9295, CS9300, CS9301, CS9302, CS9303, CS9304, CS9305, CS9306, + CS9309, CS9316, CS9317, CS9318, CS9319, CS9320, CS9321, CS9322, CS9323, CS9326, + CS9329 - name: Partial declarations href: ./compiler-messages/partial-declarations.md displayName: > partial type, partial member, partial method, partial property, partial indexer, - CS0260, CS0261, CS0262, CS0263, CS0264, CS0265, CS0267, CS0282, CS0501, CS0750, CS0751, CS0754, CS0755, CS0756, CS0757, - CS0759, CS0761, CS0762, CS0763, CS0764, CS1067, CS8142, CS8663, CS8795, CS8796, CS8797, CS8798, CS8799, CS8800, CS8817, - CS8818, CS8863, CS8988, CS9248, CS9249, CS9250, CS9251, CS9252, CS9253, CS9254, CS9255, CS9256, CS9257, CS9258, CS9263, - CS9264, CS9266, CS9273, CS9275, CS9276, CS9277, CS9278, CS9279, CS9280 + CS0260, CS0261, CS0262, CS0263, CS0264, CS0265, CS0267, CS0282, CS0501, CS0750, + CS0751, CS0754, CS0755, CS0756, CS0757, CS0759, CS0761, CS0762, CS0763, CS0764, + CS1067, CS8142, CS8663, CS8795, CS8796, CS8797, CS8798, CS8799, CS8800, CS8817, + CS8818, CS8863, CS8988, CS9248, CS9249, CS9250, CS9251, CS9252, CS9253, CS9254, + CS9255, CS9256, CS9257, CS9258, CS9263, CS9264, CS9266, CS9273, CS9275, CS9276, + CS9277, CS9278, CS9279, CS9280 - name: Params modifier href: ./compiler-messages/params-arrays.md displayName: > - CS0225, CS0231, CS0466, CS0674, CS0758, CS1104, CS1611, CS1670, CS1751, CS9218, CS9223, CS9224, CS9225, CS9227, CS9228, - CS9272 + CS0225, CS0231, CS0466, CS0674, CS0758, CS1104, CS1611, CS1670, CS1751, CS9218, + CS9223, CS9224, CS9225, CS9227, CS9228, CS9272 - name: Nullable warnings href: ./compiler-messages/nullable-warnings.md displayName: > - CS8597, CS8598, CS8600, CS8601, CS8602, CS8604, CS8605, CS8607, CS8608, CS8609, CS8610, CS8611, CS8612, CS8613, CS8614, - CS8615, CS8616, CS8617, CS8618, CS8619, CS8620, CS8621, CS8622, CS8623, CS8624, CS8625, CS8628, CS8629, CS8631, CS8632, - CS8634, CS8636, CS8637, CS8639, CS8655, CS8633, CS8643, CS8644, CS8645, CS8762, CS8763, CS8764, CS8765, CS8766, CS8667, - CS8768, CS8670, CS8714, CS8767, CS8769, CS8770, CS8774, CS8776, CS8775, CS8777, CS8819, CS8824, CS8825, CS8847 + CS8597, CS8598, CS8600, CS8601, CS8602, CS8604, CS8605, CS8607, CS8608, CS8609, + CS8610, CS8611, CS8612, CS8613, CS8614, CS8615, CS8616, CS8617, CS8618, CS8619, + CS8620, CS8621, CS8622, CS8623, CS8624, CS8625, CS8628, CS8629, CS8631, CS8632, + CS8634, CS8636, CS8637, CS8639, CS8655, CS8633, CS8643, CS8644, CS8645, CS8762, + CS8763, CS8764, CS8765, CS8766, CS8667, CS8768, CS8670, CS8714, CS8767, CS8769, + CS8770, CS8774, CS8776, CS8775, CS8777, CS8819, CS8824, CS8825, CS8847 - name: Pattern matching warnings href: ./compiler-messages/pattern-matching-warnings.md displayName: CS8509, CS9134, CS9135, CS9335, CS9337 - name: String literal declarations href: ./compiler-messages/string-literal.md displayName: > - CS1009, CS1010, CS1011, CS1012, CS1039, CS8997, CS8998, CS8999, CS9000, CS9001, CS9002, CS9003, CS9004, CS9005, CS9006, + CS1009, CS1010, CS1011, CS1012, CS1039, CS8997, CS8998, CS8999, CS9000, CS9001, + CS9002, CS9003, CS9004, CS9005, CS9006, CS9007, CS9008, CS9009, CS9274, CS9315 - name: Array declarations href: ./compiler-messages/array-declaration-errors.md displayName: > - CS0022, CS0178, CS0248, CS0251, CS0270, CS0611, CS0623, CS0650, CS0719, CS0747, CS0820, CS0826, CS0846, CS1062, CS1063, - CS1064, CS1552, CS1586, CS1920, CS1921, CS1925, CS1950, CS1954, CS3007, CS3016, CS9174, CS9176, CS9185, CS9186, CS9187, + CS0022, CS0178, CS0248, CS0251, CS0270, CS0611, CS0623, CS0650, CS0719, CS0747, + CS0820, CS0826, CS0846, CS1062, CS1063, CS1064, CS1552, CS1586, CS1920, CS1921, + CS1925, CS1950, CS1954, CS3007, CS3016, CS9174, CS9176, CS9185, CS9186, CS9187, CS9188, CS9203, CS9208, CS9209, CS9210, CS9212, CS9213, CS9214, CS9215, CS9222 - name: Inline arrays href: ./compiler-messages/inline-array-errors.md - displayName: CS9125, CS9164, CS9165, CS9166, CS9167, CS9168, CS9169, CS9172, CS9173, CS9180, CS9181, CS9182, CS9183, CS9184, CS9259 + displayName: > + CS9125, CS9164, CS9165, CS9166, CS9167, CS9168, CS9169, CS9172, CS9173, CS9180, + CS9181, CS9182, CS9183, CS9184, CS9259 - name: Lambda expressions href: ./compiler-messages/lambda-expression-errors.md displayName: > - CS0748, CS1621, CS1628, CS1632, CS1673, CS1686, CS1706, CS1989, CS3006, CS8030, CS8175, CS8916, CS8971, CS8972, CS8975, CS9098, - CS9099, CS9100, CS9236 + CS0748, CS1621, CS1628, CS1632, CS1673, CS1686, CS1706, CS1989, CS3006, CS8030, + CS8175, CS8916, CS8971, CS8972, CS8975, CS9098, CS9099, CS9100, CS9236 - name: Overload resolution href: ./compiler-messages/overload-resolution.md displayName: > 'overload resolution', - CS0034, CS0035, CS0111, CS0121, CS0457, CS1007, CS0663, CS1019, CS1020, CS1501, CS1534, CS1535, CS1928, CS1929, - CS8315, CS9261, CS9262 + CS0034, CS0035, CS0111, CS0121, CS0457, CS1007, CS0663, CS1019, CS1020, CS1501, + CS1534, CS1535, CS1928, CS1929, CS8315, CS9261, CS9262 - name: Restrictions on expression trees href: ./compiler-messages/expression-tree-restrictions.md displayName: > - CS0765, CS0831, CS0832, CS0834, CS0835, CS0838, CS0845, CS0853, CS0854, CS0855, CS1944, CS1945, CS1946, CS1951, CS1952, - CS1963, CS2037, CS7053, CS8072, CS8074, CS8075, CS8110, CS8122, CS8143, CS8144, CS8153, CS8155, CS8198, CS8207, CS8382, - CS8514, CS8640, CS8642, CS8790, CS8791, CS8792, CS8810, CS8849, CS8927, CS8952, CS9170, CS9175, CS9307 + CS0765, CS0831, CS0832, CS0834, CS0835, CS0838, CS0845, CS0853, CS0854, CS0855, + CS1944, CS1945, CS1946, CS1951, CS1952, CS1963, CS2037, CS7053, CS8072, CS8074, + CS8075, CS8110, CS8122, CS8143, CS8144, CS8153, CS8155, CS8198, CS8207, CS8382, + CS8514, CS8640, CS8642, CS8790, CS8791, CS8792, CS8810, CS8849, CS8927, CS8952, + CS9170, CS9175, CS9307 - name: Using directive and aliases href: ./compiler-messages/using-directive-errors.md displayName: > - CS0105, CS0138, CS0431, CS0432, CS0440, CS0576, CS0687, CS1529, CS1537, CS7000, CS7007, CS8019, CS8083, CS8085, CS8914, - CS8915, CS8933, CS9055, CS9130, CS9131, CS9132, CS9133, CS9162, CS9163 + CS0105, CS0138, CS0431, CS0432, CS0440, CS0576, CS0687, CS1529, CS1537, CS7000, + CS7007, CS8019, CS8083, CS8085, CS8914, CS8915, CS8933, CS9055, CS9130, CS9131, + CS9132, CS9133, CS9162, CS9163 - name: Source generators href: ./compiler-messages/source-generator-errors.md displayName: > - CS9137, CS9138, CS9139, CS9140, CS9141, CS9142, CS9143, CS9144, CS9145, CS9146, CS9147, CS9148, CS9149, CS9150, CS9151, - CS9152, CS9153, CS9154, CS9155, CS9156, CS9157, CS9158, CS9159, CS9160, CS9161, CS9177, CS9178, CS9206, CS9207, CS9231, + CS9137, CS9138, CS9139, CS9140, CS9141, CS9142, CS9143, CS9144, CS9145, CS9146, + CS9147, CS9148, CS9149, CS9150, CS9151, CS9152, CS9153, CS9154, CS9155, CS9156, + CS9157, CS9158, CS9159, CS9160, CS9161, CS9177, CS9178, CS9206, CS9207, CS9231, CS9232, CS9233, CS9234, CS9235, CS9270 - name: static abstract interface members href: ./compiler-messages/static-abstract-interfaces.md - displayName: CS8920, CS8921, CS8922, CS8923, CS8924, CS8925, CS8926, CS8928, CS8930, 8931, 8932 + displayName: > + CS8920, CS8921, CS8922, CS8923, CS8924, CS8925, CS8926, CS8928, CS8930, CS8931, + CS8932 - name: Thread synchronization href: ./compiler-messages/lock-semantics.md displayName: CS0185, CS9216, CS9217 + - name: Unsafe code + href: ./compiler-messages/unsafe-code-errors.md + displayName: > + CS0193, CS0196, CS0208, CS0209, CS0210, CS0211, CS0212, CS0213, CS0214, CS0227, + CS0233, CS0242, CS0244, CS0254, CS0459, CS0821, CS1641, CS1642, CS1656, CS1663, + CS1665, CS1666, CS1708, CS1716, CS1919, CS4004, CS8812, CS9123 - name: Warning waves href: ./compiler-messages/warning-waves.md displayName: > - CS7023, CS8073, CS8848, CS8880, CS8881, CS8882, CS8883, CS8884, CS8885, CS8886, CS8887, CS8892, CS8897, CS8898, CS8826, + CS7023, CS8073, CS8848, CS8880, CS8881, CS8882, CS8883, CS8884, CS8885, CS8886, + CS8887, CS8892, CS8897, CS8898, CS8826, CS8981 - name: Error messages items: @@ -842,10 +875,6 @@ items: href: ../misc/cs0186.md - name: CS0191 href: ../misc/cs0191.md - - name: CS0193 - href: ../misc/cs0193.md - - name: CS0196 - href: ../misc/cs0196.md - name: CS0198 href: ../misc/cs0198.md - name: CS0200 @@ -858,36 +887,18 @@ items: href: ../misc/cs0204.md - name: CS0205 href: ../misc/cs0205.md - - name: CS0208 - href: ../misc/cs0208.md - - name: CS0209 - href: ../misc/cs0209.md - - name: CS0210 - href: ../misc/cs0210.md - - name: CS0211 - href: ../misc/cs0211.md - - name: CS0212 - href: ../misc/cs0212.md - - name: CS0213 - href: ../misc/cs0213.md - - name: CS0214 - href: ../misc/cs0214.md - name: CS0220 href: ../misc/cs0220.md - name: CS0221 href: ../misc/cs0221.md - name: CS0226 href: ../misc/cs0226.md - - name: CS0227 - href: ../misc/cs0227.md - name: CS0228 href: ../misc/cs0228.md - name: CS0229 href: ./compiler-messages/cs0229.md - name: CS0230 href: ../misc/cs0230.md - - name: CS0233 - href: ./compiler-messages/cs0233.md - name: CS0236 href: ../misc/cs0236.md - name: CS0238 @@ -896,12 +907,8 @@ items: href: ../misc/cs0239.md - name: CS0241 href: ../misc/cs0241.md - - name: CS0242 - href: ../misc/cs0242.md - name: CS0243 href: ../misc/cs0243.md - - name: CS0244 - href: ../misc/cs0244.md - name: CS0245 href: ../misc/cs0245.md - name: CS0247 @@ -910,8 +917,6 @@ items: href: ../misc/cs0249.md - name: CS0250 href: ../misc/cs0250.md - - name: CS0254 - href: ../misc/cs0254.md - name: CS0255 href: ../misc/cs0255.md - name: CS0266 @@ -1040,8 +1045,6 @@ items: href: ../misc/cs0455.md - name: CS0456 href: ../misc/cs0456.md - - name: CS0459 - href: ../misc/cs0459.md - name: CS0460 href: ../misc/cs0460.md - name: CS0462 @@ -1344,8 +1347,6 @@ items: href: ../misc/cs0818.md - name: CS0819 href: ../misc/cs0819.md - - name: CS0821 - href: ../misc/cs0821.md - name: CS0822 href: ../misc/cs0822.md - name: CS0825 @@ -1596,10 +1597,6 @@ items: href: ../misc/cs1639.md - name: CS1640 href: ./compiler-messages/cs1640.md - - name: CS1641 - href: ../misc/cs1641.md - - name: CS1642 - href: ../misc/cs1642.md - name: CS1643 href: ../misc/cs1643.md - name: CS1644 @@ -1614,22 +1611,14 @@ items: href: ../misc/cs1650.md - name: CS1654 href: ../misc/cs1654.md - - name: CS1656 - href: ./compiler-messages/cs1656.md - name: CS1660 href: ../misc/cs1660.md - name: CS1661 href: ../misc/cs1661.md - name: CS1662 href: ../misc/cs1662.md - - name: CS1663 - href: ../misc/cs1663.md - name: CS1664 href: ../misc/cs1664.md - - name: CS1665 - href: ../misc/cs1665.md - - name: CS1666 - href: ../misc/cs1666.md - name: CS1667 href: ../misc/cs1667.md - name: CS1671 @@ -1660,14 +1649,10 @@ items: href: ./compiler-messages/cs1703.md - name: CS1705 href: ./compiler-messages/cs1705.md - - name: CS1708 - href: ./compiler-messages/cs1708.md - name: CS1713 href: ../misc/cs1713.md - name: CS1715 href: ../misc/cs1715.md - - name: CS1716 - href: ./compiler-messages/cs1716.md - name: CS1719 href: ../misc/cs1719.md - name: CS1721 @@ -1724,8 +1709,6 @@ items: href: ../misc/cs1917.md - name: CS1918 href: ../misc/cs1918.md - - name: CS1919 - href: ./compiler-messages/cs1919.md - name: CS1922 href: ../misc/cs1922.md - name: CS1926 @@ -1824,8 +1807,6 @@ items: href: ../misc/cs2035.md - name: CS2036 href: ../misc/cs2036.md - - name: CS4004 - href: ./compiler-messages/cs4004.md - name: CS4008 href: ./compiler-messages/cs4008.md - name: CS4009 @@ -1932,8 +1913,6 @@ items: href: ./compiler-messages/cs8802.md - name: CS8803 href: ./compiler-messages/cs8803.md - - name: CS8812 - href: ./compiler-messages/cs8812.md - name: CS8515 href: ./compiler-messages/cs8515.md - name: CS9036 diff --git a/docs/csharp/misc/cs0193.md b/docs/csharp/misc/cs0193.md deleted file mode 100644 index 6d64f675361e0..0000000000000 --- a/docs/csharp/misc/cs0193.md +++ /dev/null @@ -1,63 +0,0 @@ ---- -description: "Compiler Error CS0193" -title: "Compiler Error CS0193" -ms.date: 05/27/2025 -f1_keywords: - - "CS0193" -helpviewer_keywords: - - "CS0193" -ai-usage: ai-assisted ---- -# Compiler Error CS0193 - -The \* or -> operator must be applied to a data pointer - -The \* or -> operator was used with a nonpointer type or with a function pointer. Function pointers cannot be dereferenced in C#. For more information, see [Pointer types](../language-reference/unsafe-code.md#pointer-types) and [Function pointers](../language-reference/unsafe-code.md#function-pointers). - - The following sample generates CS0193: - -```csharp -// CS0193.cs -using System; - -public struct Age -{ - public int AgeYears; - public int AgeMonths; - public int AgeDays; -} - -public class MyClass -{ - public static void SetAge(ref Age anAge, int years, int months, int days) - { - anAge->Months = 3; // CS0193, anAge is not a pointer - // try the following line instead - // anAge.AgeMonths = 3; - } - - public static void Main() - { - Age MyAge = new Age(); - Console.WriteLine(MyAge.AgeMonths); - SetAge(ref MyAge, 22, 4, 15); - Console.WriteLine(MyAge.AgeMonths); - } -} -``` - -The following example shows that function pointers cannot be dereferenced in C#, unlike in C/C++: - -```csharp -unsafe class FunctionPointerExample -{ - public static void Log() { /* ... */ } - - public static unsafe void Example() - { - delegate* fp = &Log; // pointer to managed function Log() - fp(); // OK; call Log() via function pointer - (*fp)(); // Error; CS0193, function pointers cannot be dereferenced - } -} -``` diff --git a/docs/csharp/misc/cs0196.md b/docs/csharp/misc/cs0196.md deleted file mode 100644 index 30000c7555ddb..0000000000000 --- a/docs/csharp/misc/cs0196.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -description: "Compiler Error CS0196" -title: "Compiler Error CS0196" -ms.date: 07/20/2015 -f1_keywords: - - "CS0196" -helpviewer_keywords: - - "CS0196" -ms.assetid: d361484e-73b8-439a-99c7-714e61d73755 ---- -# Compiler Error CS0196 - -A pointer must be indexed by only one value - - A pointer cannot have multiple indices. For more information, see [Pointer types](../language-reference/unsafe-code.md#pointer-types) - - The following sample generates CS0196: - -```csharp -// CS0196.cs -public class MyClass -{ - public static void Main () - { - int *i = null; - int j = 0; - j = i[1,2]; // CS0196 - // try the following line instead - // j = i[1]; - } -} -``` diff --git a/docs/csharp/misc/cs0208.md b/docs/csharp/misc/cs0208.md deleted file mode 100644 index 51050b5959ecc..0000000000000 --- a/docs/csharp/misc/cs0208.md +++ /dev/null @@ -1,64 +0,0 @@ ---- -description: "Compiler Error CS0208" -title: "Compiler Error CS0208" -ms.date: 07/20/2015 -f1_keywords: - - "CS0208" -helpviewer_keywords: - - "CS0208" -ms.assetid: 03534893-1522-4dab-9822-8b9ed97b3bd0 ---- -# Compiler Error CS0208 - -Cannot take the address of, get the size of, or declare a pointer to a managed type ('type') - - Even when used with the [unsafe](../language-reference/keywords/unsafe.md) keyword, taking the address of a managed object, getting the size of a managed object, or declaring a pointer to a managed type is not allowed. A managed type is: - -- any reference type - -- any struct that contains a reference type as a field or property - - For more information, see [Unmanaged types](../language-reference/builtin-types/unmanaged-types.md). - -## Example - - The following sample generates CS0208: - -```csharp -// CS0208.cs -// compile with: /unsafe - -class myClass -{ - public int a = 98; -} - -struct myProblemStruct -{ - string s; - float f; -} - -struct myGoodStruct -{ - int i; - float f; -} - -public class MyClass -{ - unsafe public static void Main() - { - // myClass is a class, a managed type. - myClass s = new myClass(); - myClass* s2 = &s; // CS0208 - - // The struct contains a string, a managed type. - int i = sizeof(myProblemStruct); //CS0208 - - // The struct contains only value types. - i = sizeof(myGoodStruct); //OK - - } -} -``` diff --git a/docs/csharp/misc/cs0209.md b/docs/csharp/misc/cs0209.md deleted file mode 100644 index 3ea8e7b271540..0000000000000 --- a/docs/csharp/misc/cs0209.md +++ /dev/null @@ -1,48 +0,0 @@ ---- -description: "Compiler Error CS0209" -title: "Compiler Error CS0209" -ms.date: 07/20/2015 -f1_keywords: - - "CS0209" -helpviewer_keywords: - - "CS0209" -ms.assetid: a408a869-02db-414f-97c1-bfb1637f6155 ---- -# Compiler Error CS0209 - -The type of local declared in a fixed statement must be a pointer type - - The variable that you declare in a [fixed statement](../language-reference/statements/fixed.md) must be a pointer. For more information, see [Unsafe Code and Pointers](../language-reference/unsafe-code.md). - - The following sample generates CS0209: - -```csharp -// CS0209.cs -// compile with: /unsafe - -class Point -{ - public int x, y; -} - -public class MyClass -{ - unsafe public static void Main() - { - Point pt = new Point(); - - fixed (int i) // CS0209 - { - } - // try the following lines instead - /* - fixed (int* p = &pt.x) - { - } - fixed (int* q = &pt.y) - { - } - */ - } -} -``` diff --git a/docs/csharp/misc/cs0210.md b/docs/csharp/misc/cs0210.md deleted file mode 100644 index 93cd1b9e11642..0000000000000 --- a/docs/csharp/misc/cs0210.md +++ /dev/null @@ -1,68 +0,0 @@ ---- -description: "Compiler Error CS0210" -title: "Compiler Error CS0210" -ms.date: 07/20/2015 -f1_keywords: - - "CS0210" -helpviewer_keywords: - - "CS0210" -ms.assetid: 9f2ec1b8-6ca4-4147-b004-e3b43e7e8754 ---- -# Compiler Error CS0210 - -You must provide an initializer in a fixed or `using` statement declaration - - You must declare and initialize the variable in a [fixed statement](../language-reference/statements/fixed.md). For more information, see [Unsafe Code and Pointers](../language-reference/unsafe-code.md). - - The following sample generates CS0210: - -```csharp -// CS0210a.cs -// compile with: /unsafe - -class Point -{ - public int x, y; -} - -public class MyClass -{ - unsafe public static void Main() - { - Point pt = new Point(); - - fixed (int i) // CS0210 - { - } - // try the following lines instead - /* - fixed (int* p = &pt.x) - { - } - fixed (int* q = &pt.y) - { - } - */ - } -} -``` - - The following sample also generates CS0210 because the [`using` statement](../language-reference/statements/using.md) has no initializer. - -```csharp -// CS0210b.cs - -using System.IO; -class Test -{ - static void Main() - { - using (StreamWriter w) // CS0210 - // Try this line instead: - // using (StreamWriter w = new StreamWriter("TestFile.txt")) - { - w.WriteLine("Hello there"); - } - } -} -``` diff --git a/docs/csharp/misc/cs0211.md b/docs/csharp/misc/cs0211.md deleted file mode 100644 index 6d2423d69c0c8..0000000000000 --- a/docs/csharp/misc/cs0211.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -description: "Compiler Error CS0211" -title: "Compiler Error CS0211" -ms.date: 07/20/2015 -f1_keywords: - - "CS0211" -helpviewer_keywords: - - "CS0211" -ms.assetid: 720be9a9-b0c1-4391-94e5-4c4027e83036 ---- -# Compiler Error CS0211 - -Cannot take the address of the given expression - - You can take the address of fields, local variables, and indirection of pointers, but you cannot take, for example, the address of the sum of two local variables. For more information, see [Unsafe Code and Pointers](../language-reference/unsafe-code.md). - - The following sample generates CS0211: - -```csharp -// CS0211.cs -// compile with: /unsafe - -public class MyClass -{ - unsafe public void M() - { - int a = 0, b = 0; - int *i = &(a + b); // CS0211, the addition of two local variables - // try the following line instead - // int *i = &a; - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0212.md b/docs/csharp/misc/cs0212.md deleted file mode 100644 index 73c9bc3a69b93..0000000000000 --- a/docs/csharp/misc/cs0212.md +++ /dev/null @@ -1,61 +0,0 @@ ---- -description: "Compiler Error CS0212" -title: "Compiler Error CS0212" -ms.date: 07/20/2015 -f1_keywords: - - "CS0212" -helpviewer_keywords: - - "CS0212" -ms.assetid: 1b8973b8-03c9-42a6-bf61-2e401b89387e ---- -# Compiler Error CS0212 - -You can only take the address of an unfixed expression inside of a fixed statement initializer - - For more information, see [Unsafe Code and Pointers](../language-reference/unsafe-code.md). - - The following sample shows how to take the address of an unfixed expression. The following sample generates CS0212. - -```csharp -// CS0212a.cs -// compile with: /unsafe /target:library - -public class A { - public int iField = 5; - - unsafe public void M() { - A a = new A(); - int* ptr = &a.iField; // CS0212 - } - - // OK - unsafe public void M2() { - A a = new A(); - fixed (int* ptr = &a.iField) {} - } -} -``` - - The following sample also generates CS0212 and shows how to resolve the error: - -```csharp -// CS0212b.cs -// compile with: /unsafe /target:library -using System; - -public class MyClass -{ - unsafe public void M() - { - // Null-terminated ASCII characters in an sbyte array - sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 }; - sbyte* pAsciiUpper = &sbArr1[0]; // CS0212 - // To resolve this error, delete the previous line and - // uncomment the following code: - // fixed (sbyte* pAsciiUpper = sbArr1) - // { - // String szAsciiUpper = new String(pAsciiUpper); - // } - } -} -``` diff --git a/docs/csharp/misc/cs0213.md b/docs/csharp/misc/cs0213.md deleted file mode 100644 index 4d70fa932376f..0000000000000 --- a/docs/csharp/misc/cs0213.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -description: "Compiler Error CS0213" -title: "Compiler Error CS0213" -ms.date: 07/20/2015 -f1_keywords: - - "CS0213" -helpviewer_keywords: - - "CS0213" -ms.assetid: 3c1d55e3-2b84-4c28-8206-ef65869a898c ---- -# Compiler Error CS0213 - -You cannot use the fixed statement to take the address of an already fixed expression - - A local variable in an [unsafe](../language-reference/keywords/unsafe.md) method or a parameter is already fixed (on the stack), so you cannot take the address of either of these two variables in a [fixed](../language-reference/statements/fixed.md) expression. For more information, see [Unsafe Code and Pointers](../language-reference/unsafe-code.md). - -## Example - - The following sample generates CS0213. - -```csharp -// CS0213.cs -// compile with: /unsafe -public class MyClass -{ - unsafe public static void Main() - { - int i = 45; - fixed (int *j = &i) { } // CS0213 - // try the following line instead - // int* j = &i; - - int[] a = new int[] {1,2,3}; - fixed (int *b = a) - { - fixed (int *c = b) { } // CS0213 - // try the following line instead - // int *c = b; - } - } -} -``` diff --git a/docs/csharp/misc/cs0214.md b/docs/csharp/misc/cs0214.md deleted file mode 100644 index adbee89ae61b0..0000000000000 --- a/docs/csharp/misc/cs0214.md +++ /dev/null @@ -1,46 +0,0 @@ ---- -description: "Compiler Error CS0214" -title: "Compiler Error CS0214" -ms.date: 07/20/2015 -f1_keywords: - - "CS0214" -helpviewer_keywords: - - "CS0214" -ms.assetid: be1ef909-a53e-485f-a79b-b1cc56cead15 ---- -# Compiler Error CS0214 - -Pointers and fixed size buffers may only be used in an unsafe context - - Pointers can only be used with the [unsafe](../language-reference/keywords/unsafe.md) keyword. For more information, see [Unsafe Code and Pointers](../language-reference/unsafe-code.md). - - The following sample generates CS0214: - -```csharp -// CS0214.cs -// compile with: /target:library /unsafe -public struct S -{ - public int a; -} - -public class MyClass -{ - public static void Test() - { - S s = new S(); - S * s2 = &s; // CS0214 - s2->a = 3; // CS0214 - s.a = 0; - } - - // OK - unsafe public static void Test2() - { - S s = new S(); - S * s2 = &s; - s2->a = 3; - s.a = 0; - } -} -``` diff --git a/docs/csharp/misc/cs0227.md b/docs/csharp/misc/cs0227.md deleted file mode 100644 index d9f2ec8c16c1a..0000000000000 --- a/docs/csharp/misc/cs0227.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -description: "Compiler Error CS0227" -title: "Compiler Error CS0227" -ms.date: 07/20/2015 -f1_keywords: - - "CS0227" -helpviewer_keywords: - - "CS0227" -ms.assetid: b595a1c9-8204-4ff7-a1d0-258b0b1d6ff7 ---- -# Compiler Error CS0227 - -Unsafe code may only appear if compiling with /unsafe - -If source code contains the [unsafe](../language-reference/keywords/unsafe.md) keyword, then the [**AllowUnsafeBlocks**](../language-reference/compiler-options/language.md#allowunsafeblocks) compiler option must also be used. For more information, see [Unsafe Code and Pointers](../language-reference/unsafe-code.md). - -To set the unsafe option in Visual Studio 2012, click on **Project** in the main menu, select the **Build** pane, and check the box that says "allow unsafe code." - -The following sample, when compiled without **/unsafe**, generates CS0227: - -```csharp -// CS0227.cs -public class MyClass -{ - unsafe public static void Main() // CS0227 - { - } -} -``` - -## See also - -- [C# Compiler Errors](../language-reference/compiler-messages/index.md) diff --git a/docs/csharp/misc/cs0242.md b/docs/csharp/misc/cs0242.md deleted file mode 100644 index 5dd433fe08481..0000000000000 --- a/docs/csharp/misc/cs0242.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Compiler Error CS0242" -title: "Compiler Error CS0242" -ms.date: 07/20/2015 -f1_keywords: - - "CS0242" -helpviewer_keywords: - - "CS0242" -ms.assetid: bc86a5a4-89c1-4de4-a874-4dd4cbf592c2 ---- -# Compiler Error CS0242 - -The operation in question is undefined on void pointers - - Incrementing a void pointer is not allowed. For more information, see [Unsafe Code and Pointers](../language-reference/unsafe-code.md). - - The following sample generates CS0242: - -```csharp -// CS0242.cs -// compile with: /unsafe -class TestClass -{ - public unsafe void Test() - { - void * p = null; - p++; // CS0242, incrementing a void pointer not allowed - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0244.md b/docs/csharp/misc/cs0244.md deleted file mode 100644 index 0f754637d1ee0..0000000000000 --- a/docs/csharp/misc/cs0244.md +++ /dev/null @@ -1,36 +0,0 @@ ---- -description: "Compiler Error CS0244" -title: "Compiler Error CS0244" -ms.date: 07/20/2015 -f1_keywords: - - "CS0244" -helpviewer_keywords: - - "CS0244" -ms.assetid: f10e4479-ed6e-40dc-9fab-914e404d7f84 ---- -# Compiler Error CS0244 - -Neither 'is' nor 'as' is valid on pointer types - - The [is](../language-reference/operators/type-testing-and-cast.md#the-is-operator) and [as](../language-reference/operators/type-testing-and-cast.md#the-as-operator) operators are not valid for use on pointer types. For more information, see [Unsafe Code and Pointers](../language-reference/unsafe-code.md). - - The following sample generates CS0244: - -```csharp -// CS0244.cs -// compile with: /unsafe - -class UnsafeTest -{ - unsafe static void SquarePtrParam (int* p) - { - bool b = p is object; // CS0244 p is pointer - } - - unsafe public static void Main() - { - int i = 5; - SquarePtrParam (&i); - } -} -``` diff --git a/docs/csharp/misc/cs0254.md b/docs/csharp/misc/cs0254.md deleted file mode 100644 index 151b89a516319..0000000000000 --- a/docs/csharp/misc/cs0254.md +++ /dev/null @@ -1,48 +0,0 @@ ---- -description: "Compiler Error CS0254" -title: "Compiler Error CS0254" -ms.date: 07/20/2015 -f1_keywords: - - "CS0254" -helpviewer_keywords: - - "CS0254" -ms.assetid: 85b2ab1e-0011-4f1d-9181-76b9c9f3d914 ---- -# Compiler Error CS0254 - -The right hand side of a fixed statement assignment may not be a cast expression - - The right side of a [fixed](../language-reference/statements/fixed.md) expression may not use a cast. For more information, see [Unsafe Code and Pointers](../language-reference/unsafe-code.md). - - The following sample generates CS0254: - -```csharp -// CS0254.cs -// compile with: /unsafe -class Point -{ - public uint x, y; -} - -class FixedTest -{ - unsafe static void SquarePtrParam (int* p) - { - *p *= *p; - } - - unsafe public static void Main() - { - Point pt = new Point(); - pt.x = 5; - pt.y = 6; - - fixed (int* p = (int*)&pt.x) // CS0254 - // try the following line instead - // fixed (uint* p = &pt.x) - { - SquarePtrParam ((int*)p); - } - } -} -``` diff --git a/docs/csharp/misc/cs0459.md b/docs/csharp/misc/cs0459.md deleted file mode 100644 index 68067b9aa4a86..0000000000000 --- a/docs/csharp/misc/cs0459.md +++ /dev/null @@ -1,46 +0,0 @@ ---- -description: "Compiler Error CS0459" -title: "Compiler Error CS0459" -ms.date: 07/20/2015 -f1_keywords: - - "CS0459" -helpviewer_keywords: - - "CS0459" -ms.assetid: 01b058dd-8d65-4e9d-9de1-d47f9488d22a ---- -# Compiler Error CS0459 - -Cannot take the address of a read-only local variable - - There are three common scenarios in the C# language that generate read-only local variables: `foreach`, `using`, and `fixed`. In each of these cases, you are not allowed to write to the read-only local variable, or to take its address. This error is generated when the compiler realizes you are trying to take the address of a read-only local variable. - -## Example - - The following example generates CS0459 when an attempt is made to take the address of a read-only local variable in a `foreach` loop and in a `fixed` statement block: - -```csharp -// CS0459.cs -// compile with: /unsafe - -class Program -{ - public unsafe void M1() - { - int[] ints = new int[] { 1, 2, 3 }; - foreach (int i in ints) - { - int *j = &i; // CS0459 - } - - fixed (int *i = &_i) - { - int **j = &i; // CS0459 - } - } - - private int _i = 0; -} -``` - -> [!NOTE] -> Roslyn compiler was updated and this compiler error was removed starting with Visual Studio 2017 version 15.5, so the previous code would compile successfully with this version and later. diff --git a/docs/csharp/misc/cs0821.md b/docs/csharp/misc/cs0821.md deleted file mode 100644 index b5bb2a60d09db..0000000000000 --- a/docs/csharp/misc/cs0821.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -description: "Compiler Error CS0821" -title: "Compiler Error CS0821" -ms.date: 07/20/2015 -f1_keywords: - - "CS0821" -helpviewer_keywords: - - "CS0821" -ms.assetid: ef449115-93e8-4fa5-848a-d30dc7f68ddf ---- -# Compiler Error CS0821 - -Implicitly typed locals cannot be fixed - - Implicitly typed local variables and anonymous types are not supported in the `fixed` context. - -## To correct this error - -1. Either remove the `fixed` modifier from the variable or else give the variable an explicit type. - -## Example - - The following code generates CS0821: - -```csharp -class A -{ - static int x; - - public static int Main() - { - unsafe - { - fixed (var p = &x) { } - } - return -1; - } -} -``` - -## See also - -- [Implicitly Typed Local Variables](../programming-guide/classes-and-structs/implicitly-typed-local-variables.md) diff --git a/docs/csharp/misc/cs1641.md b/docs/csharp/misc/cs1641.md deleted file mode 100644 index bee5aba44fb86..0000000000000 --- a/docs/csharp/misc/cs1641.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -description: "Compiler Error CS1641" -title: "Compiler Error CS1641" -ms.date: 07/20/2015 -f1_keywords: - - "CS1641" -helpviewer_keywords: - - "CS1641" -ms.assetid: ba6eab47-c28b-4531-b6a0-6d538b236d19 ---- -# Compiler Error CS1641 - -A fixed size buffer field must have the array size specifier after the field name - - Unlike regular arrays, fixed size buffers require a constant size to be specified at the declaration point. To resolve this error, add a positive integer literal or a constant positive integer and put the square brackets after the identifier. - - The following sample generates CS1641: - -```csharp -// CS1641.cs -// compile with: /unsafe /target:library -unsafe struct S { - fixed int [] a; // CS1641 - - // OK - fixed int b [10]; - const int c = 10; - fixed int d [c]; -} -``` diff --git a/docs/csharp/misc/cs1642.md b/docs/csharp/misc/cs1642.md deleted file mode 100644 index 8feb1766f9844..0000000000000 --- a/docs/csharp/misc/cs1642.md +++ /dev/null @@ -1,38 +0,0 @@ ---- -description: "Compiler Error CS1642" -title: "Compiler Error CS1642" -ms.date: 07/20/2015 -f1_keywords: - - "CS1642" -helpviewer_keywords: - - "CS1642" -ms.assetid: 2efeedf1-1839-485d-8b8c-9045df1951f0 ---- -# Compiler Error CS1642 - -Fixed size buffer fields may only be members of structs. - - This error occurs if you use a fixed size buffer field in a `class`, instead of a `struct`. To resolve this error, change the `class` to a `struct` or declare the field as an ordinary array. - -## Example - - The following sample generates CS1642. - -```csharp -// CS1642.cs -// compile with: /unsafe /target:library -unsafe class C -{ - fixed int a[10]; // CS1642 -} - -unsafe struct D -{ - fixed int a[10]; -} - -unsafe class E -{ - public int[] a = null; -} -``` diff --git a/docs/csharp/misc/cs1654.md b/docs/csharp/misc/cs1654.md index 1f3387e7c176d..2a5336d0603a7 100644 --- a/docs/csharp/misc/cs1654.md +++ b/docs/csharp/misc/cs1654.md @@ -14,7 +14,7 @@ Cannot modify members of 'variable' because it is a 'read-only variable type' This error occurs when you try to modify members of a variable which is read-only because it is in a special construct. - One common area that this occurs is within [foreach](../language-reference/statements/iteration-statements.md#the-foreach-statement) loops. It is a compile-time error to modify the value of the collection elements. Therefore, you cannot make any modifications to elements that are [value types](../language-reference/builtin-types/value-types.md), including [structs](../language-reference/builtin-types/struct.md). In a collection whose elements are [reference types](../language-reference/keywords/reference-types.md), you can modify accessible members of each element, but any try to add or remove or replace complete elements will generate [Compiler Error CS1656](../language-reference/compiler-messages/cs1656.md). + One common area that this occurs is within [foreach](../language-reference/statements/iteration-statements.md#the-foreach-statement) loops. It is a compile-time error to modify the value of the collection elements. Therefore, you cannot make any modifications to elements that are [value types](../language-reference/builtin-types/value-types.md), including [structs](../language-reference/builtin-types/struct.md). In a collection whose elements are [reference types](../language-reference/keywords/reference-types.md), you can modify accessible members of each element, but any try to add or remove or replace complete elements will generate [Compiler Error CS1656](../language-reference/compiler-messages/unsafe-code-errors.md#fixed-buffers). ## Example diff --git a/docs/csharp/misc/cs1663.md b/docs/csharp/misc/cs1663.md deleted file mode 100644 index 86a81f186facc..0000000000000 --- a/docs/csharp/misc/cs1663.md +++ /dev/null @@ -1,29 +0,0 @@ ---- -description: "Compiler Error CS1663" -title: "Compiler Error CS1663" -ms.date: 07/20/2015 -f1_keywords: - - "CS1663" -helpviewer_keywords: - - "CS1663" -ms.assetid: 013f36ac-8925-4cee-9008-54fa7ad1324b ---- -# Compiler Error CS1663 - -Fixed size buffer type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double - - A fixed sized buffer may not be any type other than those listed. To avoid this error, use another type or do not use a fixed array. - -## Example - - The following sample generates CS1663. - -```csharp -// CS1663.cs -// compile with: /unsafe /target:library - -unsafe struct C -{ - fixed string ab[10]; // CS1663 -} -``` diff --git a/docs/csharp/misc/cs1665.md b/docs/csharp/misc/cs1665.md deleted file mode 100644 index 68c6c01f49e89..0000000000000 --- a/docs/csharp/misc/cs1665.md +++ /dev/null @@ -1,28 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS1665" -title: "Compiler Error CS1665" -ms.date: 07/20/2015 -f1_keywords: - - "CS1665" -helpviewer_keywords: - - "CS1665" -ms.assetid: 93d4a4af-23c3-4730-a778-77852e41db4d ---- -# Compiler Error CS1665 - -Fixed size buffers must have a length greater than zero - - This error occurs if a fixed size buffer is declared with a zero or negative size. The length of a fixed size buffer must be a positive integer. - -## Example - - The following sample generates CS1665. - -```csharp -// CS1665.cs -// compile with: /unsafe /target:library -struct S -{ - public unsafe fixed int A[0]; // CS1665 -} -``` diff --git a/docs/csharp/misc/cs1666.md b/docs/csharp/misc/cs1666.md deleted file mode 100644 index d15d10e3c451a..0000000000000 --- a/docs/csharp/misc/cs1666.md +++ /dev/null @@ -1,53 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS1666" -title: "Compiler Error CS1666" -ms.date: 07/20/2015 -f1_keywords: - - "CS1666" -helpviewer_keywords: - - "CS1666" -ms.assetid: 4d62aa9c-71b9-4c6e-8141-2426d20ac243 ---- -# Compiler Error CS1666 - -You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement. - -This error occurs if you use the [fixed-size buffer](../language-reference/unsafe-code.md#fixed-size-buffers) in an expression involving an instance that is not itself fixed. The runtime is free to move the unfixed instance around to optimize memory access, which could lead to errors when using the fixed-size buffer. To avoid this error, use the [`fixed` statement](../language-reference/statements/fixed.md). - -## Example - - The following sample generates CS1666. - -```csharp -// CS1666.cs -// compile with: /unsafe /target:library -unsafe struct S -{ - public fixed int buffer[1]; -} - -unsafe class Test -{ - S field = new S(); - - private bool example1() - { - return (field.buffer[0] == 0); // CS1666 error - } - - private bool example2() - { - // OK - fixed (S* p = &field) - { - return (p->buffer[0] == 0); - } - } - - private bool example3() - { - S local = new S(); - return (local.buffer[0] == 0); - } -} -``` diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index 19935adcc92bd..8e73511c27bc4 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -576,7 +576,6 @@ f1_keywords: - "CS9096" - "CS9097" # C# 12 errors begin here - - "CS9123" # The '&' operator should not be used on parameters or local variables in async methods. (new unsafe file) - "CS9229" # Modifiers cannot be placed on using declarations (using declarations) - "CS9230" # Cannot perform a dynamic invocation on an expression with type 'type'. (dynamic binding) # C# 14 errors begin here