Home > front end >  Why method should be converted to a property?
Why method should be converted to a property?

Time:12-30

I'm creating a method named Check_Finished() but the visual studio won't allow me to do so. Instead, it prompts me as potential fix to convert it into a property. I can't understand why can't we use a method instead of the property here, provided that both have the same purpose and both are gonna return the same thing. enter image description here

This(below) is the code I want to use.

public bool Check_Finished()
    {
        if(started && !running)
        {
            return true;
        }
    }

This is the one visual studio prompts me to convert to:

public bool Check_Finished
    {
        get
        {
            if (started && !running)
            {
                return true;
            }
        }
    }

which still isn't working. Here this happens: enter image description here

I can do the following and it works but I wanted to try a different approach just to understand things more.

public bool Finished
    {
        get { return started && !running; }
    }

CodePudding user response:

It's totally optional

it's up to you whether you want to use method or property. The VS studio code is just suggesting you here you can learn when to use property and when to use methods.

Question: which still isn't working. Here this happens:


public bool Check_Finished
{
        get
        {
            if (started && !running)
            {
                return true;
            }
            // you must return a value if the condition gets false
            return false;
        }
}

  • Related