Skip to content

Make method static if possible #364

@baks

Description

@baks

If method in class is not using any instance fields it can be made static.

class Test 
{
    public char GetThirdCharacterFromString(string str)
    {
        return str[2];
    }
}
class Referencing
{
    void Foo()
    {
        var t = new Test();
        t.GetThirdCharacterFromString("");
    }
}

becomes

public class Test 
{
    public static char GetThirdCharacterFromString(string str)
    {
        return str[2];
    }
    void Foo()
    {
        var t = new Test();
        Test.GetThirdCharacterFromString("");
    }
}

A non static method can be turned static, but only if it is not accessing this.
Also, the code fix would have to find all references and change them to static invocations. If the method is public, on the whole solution, if it is internal, on the project, etc. The current roslyn infrastructure takes care of finding those references for us.

Diagnostic id: CC0091
Category: Design
Severity: Info

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions