0

For comparisons / evaluations of objects of the same class(and other purposes), is it better to define a static or a non-static (is this called "dynamic" by chance?) function?

Sample code:

class student
{
    internal decimal medianOfMarks()
    {
        int count = 0;
        decimal sum = 0;
        foreach(float mark in reportCard)
        {
            counter++;
            sum += mark;
        }
        return sum / count;
    }

    **OR**

    internal static decimal medianOfMark(student delinquent)
    {
        [same logic]
    }
}

Let's not consider making the function a property, though parameterless, since I'd like an answer applicable to parameterized functions as well.

What are arguments pro / con each approach?
What deciding factors do exist?
I've already thought into this myself, but I don't want to bias the discussion with my opinion.

Mark
  • 403

1 Answers1

1

Subjectively, I like the non-static instance method, it feels more natural. YMMV.

Objectively, the static form requires one additional parameter that feels redundant, i.e.

myStudent.medianOfMark()

vs.

student.medianOfMark(myStudent)

I'm not a C# wizard so excuse any wrong syntax details. Also, as others have commented, there are some other issues with naming, using floats, etc...

user949300
  • 9,009