C# Error CS0196 – A pointer must be indexed by only one value

C# Compiler Error

CS0196 – A pointer must be indexed by only one value

Reason for the Error

You will receive this error when you try to use multiple indices on a pointer in your C# program.

For example, try to compile the below code snippet.

namespace DeveloperPubNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            unsafe
            {
                int* index1 = null;
                int index2 = 0;
                index2 = index1[1, 2];   
            }

        }
    }

}

The above code results with the error CS0196 because the pointer index1 is tried accessing with multiple indices.

Error CS0196 A pointer must be indexed by only one value ConsoleApp3 C:\Users\SenthilBalu\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 11 Active

Solution

To fix the error code CS0196, ensure that your C# code doesn’t have pointers in unsafe code with multiple indices.

namespace DeveloperPubNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            unsafe
            {
                int* index1 = null;
                int index2 = 0;
                index2 = index1[1];   
            }

        }
    }

}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...