Luke Hankins
Luke Hankins

Reputation: 596

C#: Symbols in front of Variable

I've seen code that has &, *, and ^ in front of variable names, but searching online only brings up the meaning for @. What do the original 3 mean? Are there any others like this?

3 Snippets:

static bool FName(array<double, 2> ^matrix)

pin_ptr<const double> p_pdMatrix = &matrix[0, 0];

sum += *(pdRoot + c + j * nSize)

Secondary: How would you search for the meaning for simple operators such as these? The above is probably a duplicate, but my searches reveal nothing.

Upvotes: 2

Views: 624

Answers (2)

tmaj
tmaj

Reputation: 34947

& and * have multiple meanings in c#; including a double meaning when used as operators.

For information on how to use them as operators you can go to https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/

Option 1. Logical operators

&, * and ^ can all be used as logical operators. For example:

    // Logical exclusive-OR


    // When one operand is true and the other is false, exclusive-OR 
    // returns True.
    bool b1 = true;
    bool b2 = false;
    Console.WriteLine(b1 ^ b2);
    // When both operands are false, exclusive-OR returns False.
    bool b1 = false;
    bool b2 = false;
    Console.WriteLine(b1 ^ b2);

    // When both operands are true, exclusive-OR returns False.
    bool b1 = true;
    bool b2 = true;
    Console.WriteLine(b1 ^ b2);

Option 2. Pointers

& and * can be used with pointers in c#.

From * Operator (C# Reference):

also serves as the dereference operator, which allows reading and writing to a pointer.

From & Operator (C# Reference)

The unary & operator returns the address of its operand (requires unsafe context).

There are some examples with both * and & in the Pointer types (C# Programming Guide)

// Normal pointer to an object.
int[] a = new int[5] { 10, 20, 30, 40, 50 };
// Must be in unsafe code to use interior pointers.
unsafe
{
    // Must pin object on heap so that it doesn't move while using interior pointers.
    fixed (int* p = &a[0])
    {
        // p is pinned as well as object, so create another pointer to show incrementing it.
        int* p2 = p;
        Console.WriteLine(*p2);

        ...more here...

How to search :)

I just put c# ^ in my fav search engine and got to the docos.

Here's the proof :)

enter image description here

Upvotes: 1

Kobi
Kobi

Reputation: 137997

These are used in C# for Unsafe Code and Pointers. They work pretty similarly to C++ pointers, but usually also require pinning the object in memory to avoid the garbage collector moving them.
They are unary operators (i.e. they do something on the value), unlike @ which is only used for explicit parsing of reserved words as variable names.

Here's a quick example from unsafe (C# Reference)

// compile with: -unsafe

class UnsafeTest
{
   // Unsafe method: takes pointer to int:
   unsafe static void SquarePtrParam(int* p)
   {
      *p *= *p;
   }

   unsafe static void Main()
   {
      int i = 5;
      // Unsafe method: uses address-of operator (&):
      SquarePtrParam(&i);
      Console.WriteLine(i);
   }
}
// Output: 25

All of these have additional meaning as safe operators - bitwise xor, bitwise and, and multiplication - but it is unlikely that's the case - usually this usage is clear.

Looking more closely - unary ^ is not included in C#, so the above comment is probably right - you may be looking at C++/CLI.

Upvotes: 3

Related Questions