Which type is inherited from an . NET array?

Asked

Viewed 128 times

0

If we declare a List, we are sure that your type is explicitly the System.Collections.Generic.List<T>.

But when we declare one array? What is the generic type implemented behind it?

Example:

string[] objeto = {"Olá,", " mundo!"};

When we get the Type.FullName of objeto, we have a "System.String[]" as output, but no type that implements or inherits the class. This does not clarify anything about.

Just like a List<>, arrays also has native functions and methods.

The question is, what class do these methods come from? Basically, I need to know if a type T[] there is some class, structure or interface implementable and changeable by type, so that it is possible to make an explicit statement, for example:

public void foo(string[] array) { ... }
...
System.Collections.Generic.TipoAnonimo<string> bar = {"foo", "bar"};
foo(bar);  // OK

Observing: I don’t intend to use LINQ in these operations.

  • 2

    Have you checked the documentation? if not, take a look at it and see if it helps... https://docs.microsoft.com/pt-br/dotnet/api/system.array?view=netframework-4.8

  • @Lodi I read, but found nothing saying that Byte[] could be converted to an Array.

2 answers

3

The class Array described in the other answer is essentially utilitarian and is not the type array of the language, and therefore in this rare specific case consulting the library documentation is not the correct way to evaluate. This guy is special in language, just like a int that even translated into Int32 if you go to look at the structure there will see nothing of how an integer works, only utilitarian methods. This is in the specification (19.1.1):

System.Array is not itself an array-type

Implicitly by specification (10.4.6) and implemented in the compiler the type array inherits conceptually from the type Array who inherits from Object as all types in C# and the interfaces described in the other answer, then the answer is even correct by table, but for the wrong reasons and with wrong explanation.

Then we can’t treat array as we treat other data structures, it receives special treatment from the compiler. Even some people say that the array that we know should be simpler and not be part of the C# type and be used only to allocate memory in a special way and be used internally in other data structures, including the type Array which would be the way you’re used to elsewhere, just like the List that internally is a array.

AP had a hunch that something was wrong there and came to comment on it, but ended up erasing.

I didn’t quite understand the final part of the doubt so I didn’t answer it, but it seems interesting.

  • Good answer, Maniero, but something still puzzles me: for being a special kind, Object[] is a feature of the language or the . NET Framework itself? I realized that when trying to extract the class name from an instance of this type, it is returned something like Array'Object[] and not as Array<Object>. I specified about Linq not to expect any "use .ToArray()".

  • 1

    Both. It’s from the library, unlike array, but there are a number of things that the compiler treats this kind of special way. But he’s a kind full, the way you see it in the library is how it works for everything. In general the differences are for optimizations the specific situations because it is the general type. Alias let’s start talking about . NET or even BCL because . NET Framework died. It returns this way because it is a special type. If the class Pedro cited was the type then the reflection would show how you are "waiting".

  • what is BCL? Why . NET Framework died?

  • found the answer to what is BCL here, but I still don’t understand the fact that ". NET Framework is dead".

  • 2

    If a question is missing in Sopt what you should do? :)

Show 1 more comment

2


Microsoft documentation is good regarding this kind of doubt. I recommend that you always consult there.

As we can see in the class documentation System.Array available here, this class implements some interfaces:

public abstract class Array : ICloneable, System.Collections.IList, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable

The type you will declare may be any of the interfaces mentioned above or even the interfaces that are the basis of them, for example: IEnumerable or ICollection. It depends on what you want to do with the object in question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.