Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why method should be converted to a property?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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; }
    }

>Solution :

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;
        }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading