Should I put extension methods of an interface in the interface.cs file?

Imagine this set up:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface IMass{
double Mass {get;}
}
public static class IMassExtension {
public static double ToKg(this IMass massObject) {
return massObject.Mass / 1000.0;
}
public static double CalculateInteractiveGravity(this IMass massObject, IMass otherMassObject)
{
return blah;
}
}
</code>
<code>public interface IMass{ double Mass {get;} } public static class IMassExtension { public static double ToKg(this IMass massObject) { return massObject.Mass / 1000.0; } public static double CalculateInteractiveGravity(this IMass massObject, IMass otherMassObject) { return blah; } } </code>
public interface IMass{
    double Mass {get;}
}

public static class IMassExtension {
    public static double ToKg(this IMass massObject) {
        return massObject.Mass / 1000.0;
    }

    public static double CalculateInteractiveGravity(this IMass massObject, IMass otherMassObject)          
    {
         return blah;
    }
}

Is it ok to put the extension class in the same file as the interface (i.e. IMass.cs) or should it be in a separate file (IMassExtension.cs)?


A base class is not possible here. Imagine

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Person : Animal, IMass {}
</code>
<code>public class Person : Animal, IMass {} </code>
public class Person : Animal, IMass {}

and

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class House : Building, IMass {}
</code>
<code>public class House : Building, IMass {} </code>
public class House : Building, IMass {}

1

Yeah, that’s totally fine. Further, I would encourage you to do that, as it makes the functionality available in the interface more discoverable, while reducing the noise in the “what functionality is in this csproj?” view in visual studio.

The only arguments I’ve heard against this are:

  • “but then you have 2 types per file!” – guidelines are there to help you, not tie your hands. You’d make it only one type if you could.
  • “but that extension uses XYZ!” – yeah, if you wouldn’t include the method if it were an abstract class, then you shouldn’t hard-wire it to the interface either. That’s not a problem with this practice, but with your design.

1

In summary: Having the extension methods in a separate file would be the approach to go. It is also very important to name extensions appropriately, as that will definitely safe time during the maintenance.

I would definitely prefer the name MassExtensions over IMassExtensions. The reason being that to most programmers an I prefix on a type implies that it is an interface. This is both a very common pattern and part of the .NET Design Guidelines.

Adding an I prefix for the extension method case breaks both assumptions and established guidelines.

There is also precedence for this in the Base Class Library. The majority of the extension methods available for IEnumerable are contained in the type Enumerable.

Following related posts might be helpful:

  • C# – using extension methods to provide default interface implementation
  • C# Extension methods namespace and promoting extension methods

2

Normally, I wouldn’t put extension methods in the same file as the interface. Same library/package? Sure.

In this example, I would definitely wouldn’t. What if someone implemented mass in imperial units (slugs)? The toKg() should be in the interface (and maybe well as toSlug(), but that would technically be derivable from toKg()), and the extension methods would be methods like toG(), toMcg(), etc., which could be implemented using the interface methods.

I would put it in a separate file, and I would give it a more descriptive name than
“IMassExtension.cs”. Perhaps something like “MassConversions.cs”, since that’s what the class actually does.

Don’t forget, the extension methods can still be called as if they were simple static methods (e.g. MassConversions.ToKg(massObject) ), so the class should be named with that in mind. Also, your extension class isn’t an interface, so it could be misleading to name it with the leading ‘I’.

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật