Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# not allow generic properties?

I was wondering why I can not have generic property in non-generic class the way I can have generic methods. I.e.:

public interface TestClass {    IEnumerable<T> GetAllBy<T>(); //this works     IEnumerable<T> All<T> { get; } //this does not work } 

I read @Jon Skeet's answer, but it's just a statement, which most probably is somewhere in the specifications.

My question is why actually it is that way? Was kind of problems were avoided with this limitation?

like image 302
Sunny Milenov Avatar asked Dec 23 '11 21:12

Sunny Milenov


People also ask

Why does letter C exist?

Like the letter G, C emerged from the Phoenician letter gimel (centuries later, gimel became the third letter of the Hebrew alphabet). In ancient Rome, as the Latin alphabet was being adapted from the Greek and Etruscan alphabets, G and C became disambiguated by adding a bar to the bottom end of the C.

Why do we use K instead of C?

In political writingReplacing the letter c with k in the first letter of a word was used by the Ku Klux Klan during its early years in the mid-to-late 19th century. The concept is continued today within the group. For something similar in the writing of groups opposed to the KKK, see § KKK replacing c or k, below.

Does the letter C need to exist?

So the C is indeed a very important letter and has no reason to feel ashamed because it makes no sound on it own. Like a man and a women come together to make a unique "sound" in their marriage, so "C" marries "H" to produce a special combined sound. CH itself has three different sounds.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

Technically, the CLR supports only generic types and methods, not properties, so the question is why it wasn’t added to the CLR. The answer to that is probably simply “it wasn’t deemed to bring enough benefit to be worth the costs”.

But more fundamentally, it was deemed to bring no benefit because it doesn’t make sense semantically to have a property parameterised by a type. A Car class might have a Weight property, but it makes no sense to have a Weight<Fruit> and a Weight<Giraffe> property.

like image 108
Timwi Avatar answered Sep 26 '22 03:09

Timwi


This Generic Properties blog post from Julian Bucknall is a pretty good explanation. Essentially it's a heap allocation problem.

like image 38
JamieSee Avatar answered Sep 24 '22 03:09

JamieSee