Skip to main content

How to Resolve Error "CS0193: The * or -> operator must be applied to a pointer" in C#

The Compiler Error CS0193 is a syntax error related to Unsafe Code and Memory Pointers. The message reads: "The * or -> operator must be applied to a pointer".

C# is a managed language, meaning the runtime handles memory addresses for you. While C# supports pointers (like C or C++) within unsafe blocks, it strictly distinguishes between Variables (managed values/references) and Pointers (unmanaged memory addresses). This error occurs when you try to use pointer-specific operators (* for dereferencing or -> for member access) on a regular variable that is not defined as a pointer type.

This guide explains the difference between managed variables and pointers and how to fix your operator usage.

Understanding the Operators

In C#, these two operators are reserved strictly for pointer types (e.g., int*, void*, MyStruct*) inside an unsafe context.

  • * (Dereference Operator): Reads or writes the value at the memory address held by a pointer.
  • -> (Pointer Member Access Operator): Accesses a member of a struct via a pointer. It is shorthand for (*ptr).Member.

If you apply these to a standard int, struct, or class instance, the compiler raises CS0193 because those variables hold values or managed references, not raw memory addresses.

Scenario 1: Using * (Dereference) on a Value

This error often happens when porting C/C++ code to C# or when a developer accidentally types * instead of multiplying.

Example of error

Trying to "dereference" a standard integer variable.

public class Program
{
static void Main()
{
int number = 42;

// ⛔️ Error CS0193: The * or -> operator must be applied to a pointer.
// 'number' is an 'int', not an 'int*'. You cannot dereference a value.
int value = *number;
}
}

Solution

If you are writing standard C#, simply use the variable directly.

public class Program
{
static void Main()
{
int number = 42;

// ✅ Correct: Just use the variable name.
int value = number;
}
}

Advanced: Using Actual Pointers

If you explicitly intended to use unsafe pointers, you must declare the variable as a pointer type and use the & (address-of) operator.

public class Program
{
// Requires 'AllowUnsafeBlocks' in .csproj
unsafe static void Main()
{
int number = 42;

// ✅ Correct: 'ptr' is defined as 'int*', so '*' can be applied to it.
int* ptr = &number;
int value = *ptr;
}
}

Scenario 2: Using -> (Arrow) on a Struct or Class

Developers coming from C++ often use the arrow operator -> to access members of an object. In C#, even though classes are "Reference Types" (references are pointers under the hood), the language abstracts this away. You almost always use the Dot Operator (.).

Example of error

Using -> on a standard struct or class instance.

public struct Point
{
public int X;
public int Y;
}

public class Program
{
static void Main()
{
Point p = new Point();
p.X = 10;

// ⛔️ Error CS0193: The * or -> operator must be applied to a pointer.
// 'p' is a struct value, not a pointer.
int currentX = p->X;
}
}

Solution: Use the Dot Operator

Replace -> with ..

public class Program
{
static void Main()
{
Point p = new Point();

// ✅ Correct: Use '.' for standard member access
int currentX = p.X;
}
}
note

When is -> valid? The arrow operator is only valid if p is declared as Point* (a pointer to a Point) inside an unsafe block. For 99% of C# applications, you should use ..

Conclusion

CS0193 is a reminder that you are using low-level C syntax in a high-level C# context.

  1. Check the Variable: Is it defined as Type*? If not, you cannot use * or ->.
  2. Fix Dereferencing: Remove the * to access the value of a standard variable.
  3. Fix Access: Change the -> arrow to a . dot to access properties and methods of objects and structs.