Why isn t Array a generic type

0 votes

Array is initiated

public abstract class Array
    : ICloneable, IList, ICollection, IEnumerable {

But why not like this?

public partial class Array<T>
    : ICloneable, IList<T>, ICollection<T>, IEnumerable<T> {
  1. What if it was specified as a generic type? Would that be a problem?
  2. Do we still require the non-generic type if it's a generic one, or could it derive from Array<T>? Like:
public partial class Array: Array<object> { 
Jun 11, 2022 in C# by pranav
• 2,590 points
458 views

1 answer to this question.

0 votes

They basically copied the concept of arrays from Java in C# 1.0. Generics did not exist at the time, but the developers believed they were clever and replicated the Java arrays' faulty covariant array semantics. This implies that you can do things like this without getting a compile-time error (rather than a runtime error):

Mammoth[] mammoths = new Mammoth[10];
Animal[] animals = mammoths;            // Covariant conversion
animals[1] = new Giraffe();             // Run-time exception

Generics were introduced in C# 2.0, although there were no covariant/contravariant generic types. If arrays were made generic, you wouldn't be able to cast Mammoth[] to Animal[], which was previously possible (even though it was broken). As a result, making arrays generic would have caused a lot of code to break.

Covariant/contravariant generic types for interfaces were introduced in C# 4.0. This allowed the array covariance to be fixed once and for all. However, this would have caused a lot of current code to break.

Array<Mammoth> mammoths = new Array<Mammoth>(10);
Array<Animal> animals = mammoths;           // Not allowed.
IEnumerable<Animals> animals = mammoths;    // Covariant conversion

Compatibility. Array is a legacy type that dates back to a time when generics were not available.

Today, it would be more logical to have Array first, then Array<T>, and finally the specific class.

answered Jun 21, 2022 by jyoti
• 1,240 points

Related Questions In C#

0 votes
0 answers

What is a jagged array?

In C#, what is a jagged array? ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
549 views
0 votes
0 answers

With arrays, why is it the case that a[5] == 5[a]?

This property of arrays in C is ...READ MORE

Nov 17, 2022 in C# by Ashwini
• 5,430 points
437 views
0 votes
1 answer

Rich Text Editor on a web page

Perhaps something along the lines of TinyMCE ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
654 views
0 votes
1 answer

Why program working differently on every run when using async?

You can use the async Main method ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
521 views
0 votes
1 answer

What is cool about generics, why use them

Okay. For simplification, I am listing it ...READ MORE

answered Jun 23, 2022 in C# by krishna
• 2,820 points
474 views
0 votes
0 answers

String was not recognized as a valid DateTime " format dd/MM/yyyy"

I want to convert my string formatted ...READ MORE

May 1, 2022 in Other DevOps Questions by Kichu
• 19,050 points
1,297 views
0 votes
1 answer

Datatypes in C#

You might be beginner I think. Go ...READ MORE

answered Jun 9, 2022 in C# by rajiv
• 1,620 points
520 views
0 votes
0 answers

Creating a generic method in C#

I'm attempting to integrate several comparable techniques ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
531 views
0 votes
1 answer

How to find the extension of a file in C#

Go to here https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getextension?redirectedfrom=MSDN&view=net-6.0#System_IO_Path_GetExtension_System_String_ string myFilePath = @"C:\MyFile.txt"; string ext ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
602 views
0 votes
1 answer

What is the difference between a field and a property

The internal workings of a class should ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
618 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP