Why are most methods of System.Array static? [closed]
Asked Answered
I

1

18

I guess this is more of a framework design question. I recently wondered why most of the methods in System.Array are static. My gut reaction always is to use e.g. IndexOf(object) on the Array instance, not as System.Array.IndexOf(array, object). Is there a main principle behind the decision, whether to make a method static or not?

I found this question: When is it best to use Static Functions in ASP.NET

But it didn't satisfy me :-/

Indicia answered 23/5, 2013 at 12:1 Comment(16)
(I removed the ASP.NET tag, because the design of Array etc is not related to ASP.NET)Nashbar
I would stipulate most of them are static because they are only meant for arrays and extension methods weren't invented at the time.Yepez
@Romoku they could still be instance methods, thoughNashbar
But does it need to be an instance method if it doesn't modify any internal state?Yepez
It's probably because Array is the base class for all arrays which types are dynamically created by the compilers. You could certainly end up with many array types in an application, leaving these methods as static allows avoiding to maintain a methods table for each generated type.Brassiere
@vc74 if they were instance methods of the base type, Array, there wouldn't be any significant difference in what is declared for each concrete array typeNashbar
Probably a duplicate of this question - #177759Selfaggrandizement
What is this? Who do you want to answer this question? What answer are you looking for? You'd need to ask the design team of the class to answer this, otherwise answers will only be speculation! Even if the programmer(s) who made the Array class hangs around on SO, this is not a suitable place to ask this..Winebaum
@MarcGravell I think the v table contains a pointer to each instance method, even if declared in a parent type. If these methods were instance methods, each generated array type would have a reference to them in its table.Brassiere
Mr Lippert to the rescue?Brassiere
@Winebaum - Related meta questionGarrow
@vc74: This answer by Hans supports your assumption btw.Winebaum
First of all thanks for all the attention :) I admit this was my 1st question on this site and I obviously overlooked the rules about precise asking. I assumed that it does not just depend on the taste of the developer and there must be some rules for this. I understand your argumentation, but if questions end up being marked as not constructive after 2 hours because 5 people don’t know the answer right away and decide that anyone else also won’t know it, I believe many interesting questions are being condemned too fast. Thanks anyway vc74 and @Winebaum for finding the very good answer above.Indicia
"because 5 people don’t know the answer right away" - I don't that was the reason. Read the description below explaining what "not constructive" means on this site.Shrewish
@Shrewish I read the description and I don't see why this question should cause extended debates... despite this one ;) It got a clear answer in a similar question found by Patrick. Unfortunatley I didn't find it right away.Indicia
Good response of Hans Passant hereLachus
H
1

The most time you inherit of System.Array is using a single dimension array. like:

int[] a1 = new int[2];

When you define int[] this implicitly derived from System.Array type like @Sergey Rybalkin says. In this case the method IndexOf would surely be best implemented as a instance method and not as static method.

But there is another types that inherit from System.Array like mult dimension arrays. In this case (mult dimension) the method IndexOf does not make sense.

Test this:

int[,] arr = new int[2, 2];

arr[0, 0] = 3; arr[1, 0] = 4;
arr[0, 1] = 5; arr[1, 1] = 6;

Array.IndexOf(arr, 4);

The last like throws a RankException with the message "Only single dimension arrays are supported here."

Perhaps, and most probably, because of that this method is implemented as static.

...

About the comment Is there a main principle behind the decision, whether to make a method static or not?

There is, and the principle is quite simple. The instance method represents a action or behavior of a object. The static method is a function of the system that is logic related with the class, or in some cases a method you want to call without creating an instance of the class.

Think in System.Math class how mess will be if you need instance math every time you want call a method like Sqrtor Pow?

The final example I will give to you is the System.Text.RegularExpressions.Regex class. This class have a Match method implemented as instance and an overload implemented as static.

Each one is used in a diferent context. The instance is used when you use the same pattern multiple times. The static when you use the pattern a unique time in your code.

Hypersonic answered 23/5, 2013 at 13:16 Comment(3)
"If the answer was enlightening please mark..."? :-P No no.. Mark as correct answer if this is the correct answer.Winebaum
I don't think your additional examples are quite fitting. There would be no point in making functions such as Sqrt or Pow instance methods of System.Math because those functions do not require a System.Math instance. On the other hand, making them instance methods of the various number types such as double etc. would be conceivable. As for the regular expressions, the static method does not perform better for unique invocations IMO; it just helps shorten the code.Treble
@O. R. Mapper you right instance method perform better. I corrected the text.Hypersonic

© 2022 - 2024 — McMap. All rights reserved.