Skip to main content

Operator cannot be abstract

In This Article

CodeRush Classic shows the Operator cannot be abstract code issue if an operator declaration contains an abstract keyword.

#Fix

Remove the abstract keyword from the operator declaration.

#Purpose

Highlights the operator declarations, which would cause the The modifier ‘abstract’ is not valid for this item compilation error.

#Example

public class MyData
{
    public MyData(string name, List<string> data)
    {
        Name = name;
        Data = data;
    }
    public string Name { get; set; }
    public List<string> Data { get; set; }
    public static abstract MyData operator +(MyData d1, MyData d2);
}

Fix:

public class MyData
{
    public MyData(string name, List<string> data)
    {
        Name = name;
        Data = data;
    }
    public string Name { get; set; }
    public List<string> Data { get; set; }
    public static MyData operator +(MyData d1, MyData d2)
    {
        string newName = String.Format("{0}&{1}", d1.Name, d2.Name);
        List<string> newData = d1.Data;
        newData.AddRange(d2.Data);
        return new MyData(newName, newData);
    }
}