Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't Array methods built into an Array instance?

Tags:

arrays

.net

char

Sorry for what is probably a silly question but it's bugging me...

int[] i = {3, 2, 1};
//why
Array.Sort(i);
//instead of
i.Sort();

char c = 'c';
//why
char.IsLetter(c);
//instead of
c.Isletter();
like image 919
paul Avatar asked Jun 24 '09 13:06

paul


People also ask

What method is used to Create a new array using elements of another array?

The slice() method creates a new array. The slice() method does not remove any elements from the source array.

What method would create new array that will have the same length of the original array?

Array.prototype.map() prototype. map() creates a new array by applying the provided transformation to each element of the original array. The result is an array with the same length as the original array and elements transformed based on the provided function.

How many array methods are there in JavaScript?

In JavaScript, arrays have three static methods.


2 Answers

Thanks to Pedro d'Aquino for identifying these other questions that provide answers.

The basic point is that instance methods on structures are not thread-safe but static methods are.

See these questions:

  • Why is .NETs char.IsLower() a static method?
  • Why IsNan() is a static method on the double class instead of an instance property?
like image 200
2 revs Avatar answered Oct 23 '22 21:10

2 revs


These are utility methods that don't need to belong to these classes. This reinforces the Single Responsibility Principle

(edit) I was confusing with Java

(About static members):

Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.

The thread-safe point of view is also a good reason.

like image 35
bruno conde Avatar answered Oct 23 '22 19:10

bruno conde