Skip to main content

Member can be static

In This Article

CodeRush Classic shows the Member can be static code issue if a member of a non-static class does not include calls to non-static members of this class.

#Fix

Add the static keyword to the member declaration.

#Purpose

Highlights the members, which can be made static to increase the scope at which they can be called.

#Example

public class MyClass
{
    public int[] Numbers { get; set; }
    public int GetMaxValue()
    {
        int result = int.MinValue;
        foreach (int num in Numbers)
            result = GetGreaterValue(num, result);
        return result;
    }
    public int GetGreaterValue(int value1, int value2)
    {
        if (value1>value2)
            return value1;
        return value2;
    }
}

Fix:

public class MyClass
{
    public int[] Numbers { get; set; }
    public int GetMaxValue()
    {
        int result = int.MinValue;
        foreach (int num in Numbers)
            result = GetGreaterValue(num, result);
        return result;
    }
    public static int GetGreaterValue(int value1, int value2)
    {
        if (value1>value2)
            return value1;
        return value2;
    }
}