The operand of an increment or decrement operator must be a variable, property or indexer

时间:2016-04-04 17:04:33

标签: c# function

can someone help me ? I have this function code in C# :

    public static int count ()
    {
        count++;
        return count;
    }

But, I get the message :"The operand of an increment or decrement operator must be a variable, property or indexer"

Can you explain why?

Thanks!

2 个答案:

答案 0 :(得分:1)

you need to declare the return variable

  public static int count ()
    {
        int count = 0;
        count++;
        return count;
    }

However, I think what you actually intend is:

    private static int _count = 0;
    public static int CountAlternative
    {
        get
        {
            return _count++;
            /* or */
            ///return ++_count;
        }
    }

If that's what you intend, then make sure you read this as well:

http://csharpindepth.com/Articles/General/Singleton.aspx

答案 1 :(得分:0)

Because your post-increment of count is being interpreted by the compiler as an increment operation on your function itself. Either you have not declared count at all, or you need to rename one or other of the variable, or the function so they do not have the same name.