4

I'm new to C#, just a question on the design idea of using static method. For example, we know that Char is struct and has the following static method:

public static bool IsDigit(Char c);

so when we check if a char is a digit, we can code :

char myChar = '8';
if (char.IsDigit(myChar)) {
   ...
}

but why language implementers make this specific design decision? they can also make it non-static as:

public bool IsDigit();

so we can code like:

char myChar = '8';
if (myChar.IsDigit()) {
   ...
}
  • 3
    There are overloads to [`Char.IsDigit`](https://learn.microsoft.com/en-us/dotnet/api/system.char.isdigit?view=netframework-4.8) that don't take a `char`. It's hard to see how these would work. Maybe one instance method, and another `static`? As far as I can see nothing about your premises is incorrect, but IIRC "why did language implementers make this specific design decision?"-type questions tend to be OT here. – spender May 19 '19 at 23:43
  • For these kinds of questions, I'd recommend looking at the docs. https://learn.microsoft.com/en-us/dotnet/api/system.char.isdigit?view=netframework-4.8 That would have shown the scenario that @spender is referring to. – mjwills May 19 '19 at 23:50
  • To save memory. You could have 100 instances of the class and static properties are in global space and the is only one occurrence of property instead of 100 instances. – jdweng May 19 '19 at 23:52
  • Is `IsDigit` a property @jdweng? – mjwills May 19 '19 at 23:56
  • The second option could effectively mutate the struct which is not a good idea. Also when you call a method on a struct a managed pointer is created and the struct value is boxed so it can pass the value to the method. Where as the static example does not require a pointer reference. – Nico May 19 '19 at 23:56
  • 3
    @jdweng I'm pretty sure that instance properties don't take up any additional memory, unless they have a backing store. – Bradley Uffner May 19 '19 at 23:56
  • 1
    Yeah, I think there's something a bit fishy, or at least misleading about @jdweng 's comment. I'm not sure quite how virtual method tables work in C# or if they are even relevent to an implicitly sealed struct, but calling a method a "property"... hmm... – spender May 20 '19 at 00:02
  • 1
    Not that `char` is a class anyway, but adding methods *or* properties doesn't add to the per-instance memory usage. – Jon Skeet May 20 '19 at 06:24
  • @Nico: How could the second option mutate a struct? And no, structs aren't boxed every time you call methods on them. (There are cases where that does happen, but not by default.) – Jon Skeet May 20 '19 at 06:28
  • 1
    @JonSkeet `public bool IsDigit() { this = '.'; return false; }` ? (ignoring readonly fields that is) – Lasse V. Karlsen May 20 '19 at 09:19
  • 2
    @LasseVågsætherKarlsen: But obviously you *wouldn't* do that. I expressed myself badly in the comment before. To put it another way: there are lots of instance methods on various value types (especially `DateTime`). Those *could* all be written to mutate the value as well, but we trust that they don't. What's special about `char.IsDigit` that makes it more sensitive to this theoretical possibility? – Jon Skeet May 20 '19 at 09:43
  • Nothing. Nothing at all. – Lasse V. Karlsen May 20 '19 at 09:49
  • It looks to me like the difference is somewhat arbitrary, but the type of operations we perform with `DateTime` make instance methods more convenient. We can write `var newDate = oldDate.AddDays(1);` instead of `var newDate = DateTime.AddDays(oldDate, 1);` Or `someDate.DayOfWeek` vs. `DateTime.DayOfWeek(someDate);` It looks a lot to me like a style preference, but if so I wonder if there were some very specific considerations and if the exact same choices would be made today. I'd love to know. – Scott Hannen May 20 '19 at 14:59

1 Answers1

0

We generally use static methods for defining utility functions(they are the functions for which an instance is not required). If the function is not a utility function, we make it an instance method

Alsein
  • 4,268
  • 1
  • 15
  • 35