0

I'm learning C# recently with a strong C++ background, and there is something about C# that I don't quite understand, given my understanding of and experiences with C++.

In C++, people do care a lot about uniformity, otherwise it would be impossible to write generic code using template meta-programming. In C#, however, people seem to care little about uniformity. For example, while array types have a Length property, List<T> uses Count. While IndexOf, LastIndexOf, and alike for array types are static methods, their counterparts for List<T> are not. This gives me the impression that instead of being uniform, C# is actually trying hard to be nonuniform. This doesn't make sense to me. Since C# doesn't support template meta-programming, uniformity is not that important as in C++. But still, being uniform can be beneficial in many other ways. For example, it would be easier for humans to learn and master. When things are highly uniform, you mater one, and you master it all. Please note that I'm not a C++ fanatics nor diehard. I just don't really understand.

Lingxi
  • 14,579
  • 2
  • 37
  • 93
  • That's just what the developers of the language decided to do. – gunr2171 Apr 07 '16 at 16:04
  • @gunr2171 Then, what are the motivations and rationale behind such decisions? – Lingxi Apr 07 '16 at 16:05
  • Don't know. Ask Microsoft. The community here can make some educated guesses, but only the developers will know for sure. – gunr2171 Apr 07 '16 at 16:05
  • 2
    I'm voting to close this question as off-topic because it is a question about the design decisions that went into a language. Only the developers can give a proper response. – gunr2171 Apr 07 '16 at 16:10
  • I agree that the question may be poorly worded, but the core question is a valid one that many newcomers to the .NET world probably will be asking. I don't think it should be closed. But then again, why not? On Stack Overflow, closing questions is what we do! Yeehaw – Dave Markle Apr 07 '16 at 16:29
  • I don't know what you mean by uniformity but based on your examples you seem to be saying that the names are not logical. Many names in C/C++ are also not logical. One problem with this question is that it is subjective. Answers depend on opinions and preferences. – Sam Hobbs Apr 07 '16 at 16:45
  • @user34660 By uniformity, I take to mean that they should be named identically. I don't actually care whether it should be `Length` or `Count`. Just be uniform, and use the same name for both. – Lingxi Apr 07 '16 at 16:48
  • That question really makes sense for people who are learning a new language. It's mainly a matter of consistency (between concepts and naming). For new developers, common rules (naming conventions for properties, methods, etc.) really help to assimilate the concepts. Indeed, it's always weird to see a same concept (in the author's example, a collection/array length) expressed in two different ways, I assume that everybody can understand it ! – Ishikawa Oct 21 '19 at 15:37

1 Answers1

5

You've got a conceptual issue here.

List<T>, and the other collection classes with it, aren't C# constructs. They are classes in the BCL. Essentially, you can use any BCL class in any .NET Language, not just C#. If you're asking why the BCL classes differ in certain ways, it's not because the designers disrespected or didn't want uniformity. It's probably for one of (at least two) reasons:

1) The BCL and FCL evolved over time. You're likely to see very significant differences in classes that were introduced before and after generics were added. One example, DataColumnCollection, is an IEnumerable (but not an IEnumerable<DataColumn>). That causes you to need to cast to perform some operations.

2) There's a subtle difference in the meaning of the method. .Length, I believe, is made to imply that there's a static number somewhere, where .Count implies that some operation might be done to get the number of items in the list.

Community
  • 1
  • 1
Dave Markle
  • 95,573
  • 20
  • 147
  • 170