2

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.

Luke Hankins
  • 588
  • 5
  • 13
  • Could you give examples? – tymtam Oct 11 '18 at 05:05
  • What leads you to believe it's C#? (Hint: it's not - at least not the first two snippets.) What kind of codebase were you looking at that used these symbols? – Lance U. Matthews Oct 11 '18 at 05:14
  • 3
    Dude - it looks like Microsoft's flavor of .Net C++ ("CLI") to me... Not C#. For example: https://msdn.microsoft.com/en-us/library/yk97tc08.aspx – paulsm4 Oct 11 '18 at 05:20
  • I think you are both right. It was not one of the developers who told me it was C#, so they must have been mistaken. I know they do deal with .NET, though. – Luke Hankins Oct 11 '18 at 05:27
  • It's certainly C++, because you can't use `` token (it will treated as generic token). C# uses `` token for `double` generic type parameter. – Tetsuya Yamamoto Oct 11 '18 at 05:47

2 Answers2

3

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.

Kobi
  • 135,331
  • 41
  • 252
  • 292
1

& 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

tymtam
  • 31,798
  • 8
  • 86
  • 126