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];   
            }

        }
    }

}