Redundancy in C#?

2020-06-09 10:01发布

Take the following snippet:

List<int> distances = new List<int>();

Was the redundancy intended by the language designers? If so, why?

17条回答
乱世女痞
2楼-- · 2020-06-09 10:02

I see one other problem with the using of var for laziness like that

var names = new List<string>();

If you use var, the variable named "names" is typed as List<string>, but you would eventually only use one of the interfaces inherited by List<T>.

IList<string> = new List<string>();
ICollection<string> = new List<string>();
IEnumerable<string> = new List<string>();

You can automatically use everything of that, but can you consider what interface you wanted to use at the time you wrote the code?

The var keyword does not improve readability in this example.

查看更多
来,给爷笑一个
3楼-- · 2020-06-09 10:05

The reason the code appears to be redundant is because, to a novice programmer, it appears to be defining the same thing twice. But this is not what the code is doing. It is defining two separate things that just happen to be of the same type. It is defining the following:

  1. A variable named distances of type List<int>.
  2. An object on the heap of type List<int>.

Consider the following:

Person[] coworkers = new Employee[20];

Here the non-redundancy is clearer, because the variable and the allocated object are of two different types (a situation that is legal if the object’s type derives from or implements the variable’s type).

查看更多
Luminary・发光体
4楼-- · 2020-06-09 10:08

The compiler improvements for C# 3.0 (which corresponds with .Net 3.5) eliminate some of this sort of thing. So your code can now be written as:

var distances = new List<int>();

The updated compiler is much better at figuring out types based on additional information in the statement. That means that there are fewer instances where you need to specify a type either for an assignment, or as part of a Generic.

That being said, there are still some areas which could be improved. Some of that is API and some is simply due to the restrictions of strong typing.

查看更多
趁早两清
5楼-- · 2020-06-09 10:10

The redunancy wasn't intended, per se, but was a side-effect of the fact that all variables and fields needed to have a type declaration. When you take into account that all object instantiations also mention the type's name in a new expression, you get redundant looking statements.

Now with type-inferencing using the var keyword, that redundancy can be eliminated. The compiler is smart enough to figure it out. The next C++ also has an auto keyword that does the same thing.

The main reason they introduced var, though, was for anonymous types, which have no name:

var x = new {Foo = Bar, Number = 1};
查看更多
闹够了就滚
6楼-- · 2020-06-09 10:11

C# is definitely getting less verbose after the addition of functional support.

查看更多
放我归山
7楼-- · 2020-06-09 10:12

As others have said: var removes the redundancy, but it has potential negative maintenance consequences. I'd say it also has potential positive maintenance consequences.

Fortunately Eric Lippert writes about it a lot more eloquently than I do: http://csharpindepth.com/ViewNote.aspx?NoteID=63 http://csharpindepth.com/ViewNote.aspx?NoteID=61

查看更多
登录 后发表回答