Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The * operator in C# serves three distinct syntactic roles depending on its context: as a binary arithmetic operator for multiplication, as a unary indirection operator for pointer dereferencing, and as a type modifier for declaring pointer types.

Binary Multiplication Operator

As a binary operator, * computes the product of two operands. It is predefined for all built-in numeric types (integer, floating-point, and decimal).
// Syntax
T result = operand1 * operand2;
When applied to integer types, if the resulting value exceeds the bounds of the return type, the behavior depends on the execution context:
  • In a checked context, an OverflowException is thrown.
  • In an unchecked context, the result is truncated by discarding high-order bits.
Floating-point multiplication (float and double) conforms to IEEE 754 arithmetic, natively handling special values such as NaN (Not a Number) and Infinity. Operator Overloading User-defined types (classes and structs) can overload the binary * operator to define custom multiplication mechanics.
public static CustomType operator *(CustomType left, CustomType right)
{
    // Implementation mechanics
    return new CustomType(/* ... */);
}

Unary Indirection Operator

In an unsafe context, the unary * operator performs pointer indirection (dereferencing). It evaluates to the variable located at the memory address held by the pointer operand.
// Syntax
T value = *pointerVariable;
  • The operand must be a pointer type.
  • The operator cannot be applied to a void* pointer. A void* must be explicitly cast to a specific unmanaged pointer type before indirection.
  • Applying the indirection operator to a null or invalid pointer results in undefined behavior, typically manifesting as a memory access violation.

Pointer Type Declaration

Also restricted to unsafe contexts, the * symbol acts as a type modifier to declare a pointer to an unmanaged type.
// Syntax
T* pointerName;
  • T must be an unmanaged type (e.g., primitive numeric types, bool, char, enums, or a struct containing only unmanaged types).
  • In C#, the * is syntactically bound to the underlying type, not the variable name. For example, the declaration int* p1, p2; creates two pointer variables (p1 and p2), unlike C or C++ where p2 would be a standard integer.
Master C# with Deep Grasping Methodology!Learn More