1

I'm playing around with ConditionalWeakTable for the first time, so I figured I'd run the example from the docs.

This doesn't compile out of the box... I had to change this:

else if (cwt.TryGetValue(wr2.Target, out data))

...to this:

else if (cwt.TryGetValue((ManagedClass)wr2.Target, out data))

Even then though, my console spits out Data created at 07/03/2023 12:56:02... which is not what the docs say it should be doing:

// The example displays the following output:
//       No strong reference to mc2 exists.

This is my project file (the only other file in the solution, a part from the example code):

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <LangVersion>10.0</LangVersion>
        <OutputType>Exe</OutputType>
    </PropertyGroup>

</Project>

I did try targeting netcore 3.1 to see if that would make a difference but still no joy.

Am I missing something really basic here?

Charlieface
  • 52,284
  • 6
  • 19
  • 43
James Crosswell
  • 648
  • 4
  • 13
  • 1
    It's very poor example code as you note. It works if you do the creation of `mc2` in a separate function, due to the way GC tracks object lifetime in Debug mode. See https://dotnetfiddle.net/unZjUn There should be a duplicate of this somewhere: GC does not collect in Debug mode if the object is alive in the same function – Charlieface Jul 03 '23 at 01:43

0 Answers0