-2

I want to do have the following functionality:

var item0 = new Item();
var item1 = item0;
item0 = null;

Console.WriteLine(item1 == null); // true

So I'd be overwriting the memory that item0 and item1 point to. I figured maybe I could do this with pointers..? However, when I tried declaring an item pointer:

Item* itemPointer;

i got an error. Any ideas?

abdbna
  • 1
  • 1
  • I don't know of any language where the syntax you proposed would work - and if it did it would introduce a lot of hard to find bugs – UnholySheep Nov 14 '17 at 17:32
  • 1
    you can free a pointer using Marshaling but if that pointer is owned else where you would most likely just have an horrible death. [I guess you could check this for a start](https://stackoverflow.com/questions/18172979/deleting-c-sharp-unsafe-pointers) – Prix Nov 14 '17 at 17:33
  • 8
    I am mostly curious as to *why* you want to do this? What is the purpose of directly modifying memory this way (and giving up all the safety the runtime provides)? – UnholySheep Nov 14 '17 at 17:34
  • @UnholySheep: My guess is he doesn't want any garbage left over in memory. – JuanR Nov 14 '17 at 17:35
  • I totally agree with @UnholySheep, but it looks to me he just want to make a new copy of the item rather than having a clone. – Prix Nov 14 '17 at 17:36
  • if `item1` was `Func item1 = () => item0;` then `Console.WriteLine(item1() == null);` would return `true` – Matthew Whited Nov 14 '17 at 17:42
  • @UnholySheep in summary: i have written a load classes which are immutable. i want to put some instances of these classes in dictionaries, and have other objects use information from the stuff in the dictionaries. however, i occassionally want to reassign the stuff in the dictionary, but i would also want to keep the references of my other objects up to date (hence my question). i think the easiest solution will be to make the necessary things settable, therefore the dictionary values stay the same the whole time – abdbna Nov 14 '17 at 18:40

3 Answers3

5

Since C# 7 You can use Ref Locals:

var item0 = new Item();
ref var item1 = ref item0;
item0 = null;

Console.WriteLine(item1 == null); // true  
// THIS WORKS!          

Ref returns and ref locals have mainly been introduced to avoid copying large structs in time sensitive scenarios like gaming where large arrays of vectors have to be processed. I don't see the advantage in using them with reference types. They tend to make the code hard to understand, thus I would restrict their use to exceptional and rare cases.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0
var item0
{
   get{return item0;}
   set
   {
     item0 = value;
     item1 = item0;
   }
}

Something like this will overwrite the value of item1 every time the value of item0 changes.

Brandon Miller
  • 1,534
  • 1
  • 11
  • 16
0

There are no local aliases/references in C# However there is a ref keyword for arguments:

static void RefClean(ref String arg){
    arg = null;
}
static void Main(string[] args)
{
    var test = "Hello";
    Console.WriteLine(test == null);
    RefClean(ref test);
    Console.WriteLine(test == null);
}


For pointers you need the unsafe keyword, and you have to use unmanaged types only (primitive types and structures built from them), which rules out your case with objects/references. See https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/unsafe-code-pointers/pointer-types for some overview.
tevemadar
  • 12,389
  • 3
  • 21
  • 49