Ma Dude
Ma Dude

Reputation: 557

New Class Object - Old Values Remain

I've been doing C# for like 3-4 years now but I never really bothered with using a Class system as I never really needed it.

But now my project is gotten to the point where it would benefit from it. So I started to create and use one.

So I have an internal constructor that takes 2 Parameters (let's just say strings) and it stores them in private string's outside of the internal constructor.

I then new MyClass("str1", "str2"); and use all its other public methods. (e.g. .StartBenchmark())

Here's what's odd, if I have a private string thisMustAlwaysStartAs="ThisString123456789"; (which gets changed in the constructor to changed123;

When I new MyClass() I'm expecting thisMustAlwaysStartAs to start as ThisString123456789 but it's already changed123 yet I created a brand new instance?

(for e.x. Constructor Code would be the following, so you create 1 instance then another right after, you would get 2 MessageBox Popups, first one is the start as a string, the 2nd one is not, but how is that?)

MessageBox.Show(thisMustAlwaysStartAs);
thisMustAlwaysStartAs="changed123";

Notes: MyClass doesn't implement IDisposable, so I'm never Disposing of it but that shouldn't matter, right? The thisMustAlwaysStartAs "string" is an example, it's not actually a string, it's a CancellationTokenSource, if I Cancel() it, and make a new instance, it creates as already Cancelled. Is this possibly due to CancellationTokenSource being IDisposable meaning im meant to Dipose it when I'm finished with it? I know this would work, but when I create a new MyClass I'm assuming it essentially creates a brand new instance of CancellationTokenSource, not reuse one from another instance of MyClass.

Do I understand everything correctly?

Q1: Why does CancellationTokenSource keep its IsCancellationRequested set to true even if I new MyClass() and check from the new instance?

Q2: If it's because I must Dispose() the CancellationTokenSource EVEN THOUGH I'm creating a brand new MyClass() why?

Q3: If Q2 is correct, should I create an IDisposable implementation to MyClass and Dispose CancellationTokenSource from that and use MyClass in a using()?

If there is anything I'm missing or completely wrong about, Please tell me!


Show us the code

namespace NotEveryQuestionNeedsExamples {
    class Lazy {
        private static CancellationTokenSource CancellationTokenSource = new CancellationTokenSource();
        public bool Stopped => CancellationTokenSource.Token.IsCancellationRequested;
        internal Lazy() {
            if(Stopped) { MessageBox.Show("This shouldn't show up"); }
            CancellationTokenSource.Cancel();//This will make Stopped return true
        }
    }
}

Testing:
new Lazy();
new Lazy();
//If you got ANY messagebox, then CancellationTokenSource somehow got the .IsCancellationRequested from the first Lazy() instance

This messagebox issue doesn't occur if I do CancellationTokenSource = new CancellationTokenSource(); at start of internal Lazy()

Upvotes: 0

Views: 653

Answers (1)

Binary Worrier
Binary Worrier

Reputation: 51709

Q1: Why does CancellationTokenSource keep its IsCancellationRequested set to true even if I new MyClass() and check from the new instance?

Because it's static, this means, for all the instances (each object created by new) there will only be one instance of CancellationTokenSource (with only one value for IsCancellationRequested) shared between them.
Each object gets it's own value for non static members (e.g. each with have a non-shared value for Stopped).

Q2: If it's because I must Dispose() the CancellationTokenSource EVEN THOUGH I'm creating a brand new MyClass() why?

Nope, nothing to do with dispose here, it's because it's static

Q3: If Q2 is correct, should I create an IDisposable implementation to MyClass and Dispose CancellationTokenSource from that and use MyClass in a using()?

Regardless of Q2 being incorrect, your class really should implement IDisposable to dispose of object it owns, like the instance of CancellationTokenSource i.e. where they are non static members.

I'm guessing CancellationTokenSource shouldn't be static on this class

See C# - Static for a tutorial on static, the second section Static Members in a non static class applies to this situation.

Upvotes: 1

Related Questions