❔ Determine what variable is being referenced

I come from a primarily C++ background, and I take it C# is trying it's best to hold my hand and keep me from shooting myself in the foot, but I don't entirely understand local reference variables due to their limitations. In C++, I can determine whether a pointer is referencing something by comparing it's value against another address.
if (somePtr == &someVar) { ... }
if (somePtr == &someVar) { ... }
How would I accomplish the same thing with local reference variables in C#?
int someVar = 0;
ref int someRef = ref someVar;
// how can I determine someRef is still referring to someVar?
int someVar = 0;
ref int someRef = ref someVar;
// how can I determine someRef is still referring to someVar?
9 Replies
cap5lut
cap5lut2y ago
that doesnt sound very c#-ish whats the use case here?
UnionRings ♪
UnionRings ♪OP2y ago
I've got a function that finds the nth space in a string and the space after that so I can underline a word in a console output. When it finds the nth space, it stores the index in one variable and switches to the second variable Something to this effect
int spaceCount = 0;
int leftIndex = 0;
int rightIndex;
ref int currentIndex = ref leftIndex;

while (currentIndex < str.Length)
{
if (!str[currentIndex].Equals(' '))
{
currentIndex++;
continue;
}
if (spaceCount.Equals(fieldId))
{
if (???) break;
rightIndex = leftIndex;
currentIndex = ref rightIndex;
}
spaceCount++;
currentIndex++;
}
int spaceCount = 0;
int leftIndex = 0;
int rightIndex;
ref int currentIndex = ref leftIndex;

while (currentIndex < str.Length)
{
if (!str[currentIndex].Equals(' '))
{
currentIndex++;
continue;
}
if (spaceCount.Equals(fieldId))
{
if (???) break;
rightIndex = leftIndex;
currentIndex = ref rightIndex;
}
spaceCount++;
currentIndex++;
}
I know there are other ways to go about it but trying to implement it this way just begged the question
cap5lut
cap5lut2y ago
that sounds a lot like u want to use Span<T> and especially its slice method (https://learn.microsoft.com/en-us/dotnet/api/system.span-1.slice?view=net-7.0) but u can also simply compare refs:
var x = 1;
var y = 2;

ref var xr = ref x;
ref var yr = ref y;

Console.WriteLine(xr == yr);

yr = xr;
Console.WriteLine(xr == yr);
var x = 1;
var y = 2;

ref var xr = ref x;
ref var yr = ref y;

Console.WriteLine(xr == yr);

yr = xr;
Console.WriteLine(xr == yr);
(will print False True)
that was wrong tbh u dont understand why u need refs at all there, just indices should be enough?
UnionRings ♪
UnionRings ♪OP2y ago
It saves a few branches
cap5lut
cap5lut2y ago
to me that seems to be 2 IndexOfAny calls (https://learn.microsoft.com/en-us/dotnet/api/system.string.indexofany?view=net-7.0) to geth the indices (or even just IndexOf because u seem to care only about the space), no need to reinvent the wheel
UnionRings ♪
UnionRings ♪OP2y ago
That'd definitely be better than the looping approach I was moreso curious if there was a way to overcome that limitation of C# references, but if it really is as simple as "there isn't one" then ah well I can always import some dll function if I need to
Pascal
Pascal2y ago
You would have to use Unsafe.AreSame<int>(ref someRef, ref someVar) (https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafe.aresame?view=net-7.0#system-runtime-compilerservices-unsafe-aresame-1(-0@-0@)) to compare the references in this case. You don't want to use ReferenceEquals(someRef, someVar) due to boxing for value types.
Unsafe.AreSame(T, T) Method (System.Runtime.CompilerServices)
Determines whether the specified managed pointers point to the same location.
UnionRings ♪
UnionRings ♪OP2y ago
Oh sick That works I'll bear that in mind if this comes up again
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server