✅ Why is the attempted fix still creating a new copy of the dv each time CallDispose is called?

// given, results in "Disposing for first time" being printed 3 times
// static void CallDispose(IDisposable o)
// {
// o.Dispose();
// }

// attempted fix, still prints "Disposing for first time" 3 times
static void CallDispose<T>(T o) where T : IDisposable
{
o.Dispose();
}

DisposableValue dv = new();
Console.WriteLine("Passing value variable:");
CallDispose(dv);
CallDispose(dv);
CallDispose(dv);

public struct DisposableValue : IDisposable
{
private bool _disposedYet;

public void Dispose()
{
if (!_disposedYet)
{
Console.WriteLine("Disposing for first time");
_disposedYet = true;
}
else
{
Console.WriteLine("Was already disposed");
}
}
}
// given, results in "Disposing for first time" being printed 3 times
// static void CallDispose(IDisposable o)
// {
// o.Dispose();
// }

// attempted fix, still prints "Disposing for first time" 3 times
static void CallDispose<T>(T o) where T : IDisposable
{
o.Dispose();
}

DisposableValue dv = new();
Console.WriteLine("Passing value variable:");
CallDispose(dv);
CallDispose(dv);
CallDispose(dv);

public struct DisposableValue : IDisposable
{
private bool _disposedYet;

public void Dispose()
{
if (!_disposedYet)
{
Console.WriteLine("Disposing for first time");
_disposedYet = true;
}
else
{
Console.WriteLine("Was already disposed");
}
}
}
12 Replies
D.Mentia
D.Mentia4d ago
it's a struct, a value type. Changing its data (_disposedYet) doesn't update the existing one, dv, it makes a new one with the new values which admittedly doesn't sound like it makes much sense when the struct is changing its own values, but I don't think structs typically have any logic in them, assumedly for that reason I'm not sure I explained that accurately but the point is just that the dv you have a reference to is not the DisposableValue that has _disposedYet = true. Basically, use a class imo, or maybe dig into how structs work and maybe if you ref it into CallDispose you can make it work
Jason_Bjorn
Jason_Bjorn4d ago
I get it I went from boxing to getting a new copy of the value thanks @D.Mentia
D.Mentia
D.Mentia4d ago
tbh I don't get it :lul: like if it's creating a new DisposableValue when you set _disposedYet... which instance is that method running on, the old one or new one?
Jason_Bjorn
Jason_Bjorn4d ago
A new one is created on each function call
D.Mentia
D.Mentia4d ago
like if you did
if (!_disposedYet)
{
Console.WriteLine("Disposing for first time");
_disposedYet = true;
Console.WriteLine(_disposedYet);
}
if (!_disposedYet)
{
Console.WriteLine("Disposing for first time");
_disposedYet = true;
Console.WriteLine(_disposedYet);
}
would it print false?
Jason_Bjorn
Jason_Bjorn4d ago
In the commented out top one you get a new box with a new copy of the value, in the attempted fix you get a copy of just the value without a box
Passing value variable:
Disposing for first time
True
Was already disposed
Was already disposed
Passing value variable:
Disposing for first time
True
Was already disposed
Was already disposed
D.Mentia
D.Mentia4d ago
So then... when exactly is the copy created? When it first enters the Dispose() method, it doesn't know that a value is going to be changed... so it must be entering it on the original instance But then upon the value being changed, it creates a new instance, and then continues method execution from within that new instance apparently? IDK, weird
Jason_Bjorn
Jason_Bjorn4d ago
value types are pass-by-copy
D.Mentia
D.Mentia4d ago
Oh I see, got it. Even when not changing a value
Jason_Bjorn
Jason_Bjorn4d ago
Yeah
D.Mentia
D.Mentia4d ago
you've answered my question, I'll mark this thread as resolved :lul:
Jason_Bjorn
Jason_Bjorn4d ago
we both learned something
Want results from more Discord servers?
Add your server
More Posts
Questions regarding a linq queryHello there, I'm playing a little bit with linq queries but don't know if i truly understand this. WDistinct List in List of Lists1,1].Distinct() returns [1] [[1],[1]].Distinct() returns [[1],[1]] what to do ?✅ System Exception cannot figure out why it's happeningHello, I have several DataViewGrids that I am binding data to. I'm binding objects from inside of thWhich object creation method is more used?I've started learning C# recently and I am wondering which way of object creation is more acceptableHow to link C++ library to C#, and make C++ library cannot to decompile?I make some CLR Class Library with C++. But when i using dotpeek, it still decompiled. How to make i"ReferencePath" item list does not define a value for the "CopyLocal" metadataHi there,I am making a form application with C# and after downloading Iot.Device.Bindings System.DevUnit Testing | Unsupported expression: ht => ht.LoadAll<ReasonType>()Non-overridable membersanyone know how to fix this error? here a function i tried to test ```c++ public List<ReasonType> GRegister user doesnt work (MySQL)Hey, im new to databases and just wanted to learn how to work with them. But every time I regsiter a✅ How To Pass A String or LoginModel back through navigation Avalonia.MVVMLoginView.axaml: https://pastebin.com/hnwBdAe9 MainWindowViewModel.cs: https://pastebin.com/6LMgkL0cKestrel https url binding warningI have the testapplication in screenshot 1. on startup i get a warning that i want to understand and