Is there anything wrong with a class with all static methods?

后端 未结 16 1047
余生分开走
余生分开走 2021-01-30 16:43

I\'m doing code review and came across a class that uses all static methods. The entrance method takes several arguments and then starts calling the other static methods passin

16条回答
  •  孤城傲影
    2021-01-30 17:04

    No, many people tend to create completely static classes for utility functions that they wish to group under a related namespace. There are many valid reasons for having completely static classes.

    One thing to consider in C# is that many classes previously written completely static are now eligible to be considered as .net extension classes which are also at their heart still static classes. A lot of the Linq extensions are based on this.

    An example:

    namespace Utils {
        public static class IntUtils        {
                public static bool IsLessThanZero(this int source)
                {
                    return (source < 0);
                }
        }
    }
    

    Which then allows you to simply do the following:

    var intTest = 0;
    var blNegative = intTest.IsLessThanZero();
    

提交回复
热议问题