隐式类型如何使代码更清晰?

发布于 2024-09-12 19:04:31 字数 996 浏览 7 评论 0 原文

在我正在阅读的一本书中,它指出隐式类型使以下代码比不使用 var 关键字更清晰:

var words = new[] { "a", "b", null, "d" };

foreach (var item in words)
{
    Console.WriteLine(item);
}

在我看来,情况恰恰相反:如果您使用 string 相反,代码的读者会立即知道它是 foreach 循环中的字符串,而不必在定义变量的代码中查找。

隐式类型如何使上述代码更清晰?

附录

这本书是C # 3.0 - 新的。 schnell + kompakt 是德语,实际文本是:

Das Schluesselwort var kann auch beim Durchlaufen von foreach-Schleifen verwendet werden, um somit den Code uebersichtlicher und einfacher zu gestalten. Besonders bei complexen Typen cann man auf diese Art und Weise Programmierfehler verhindern.

这是我的翻译:

在迭代 foreach 循环时也可以使用 var 关键字,从而使代码的创建更容易、更简单。特别是在使用复杂类型时,这可以防止编程错误。

好吧,现在仔细阅读一下,他实际上指出 foreach 循环中的 var 使代码更容易创建,但不一定更容易阅读。

In a book I'm reading it states the implicit typing makes the following code clearer than if you didn't use the var keyword:

var words = new[] { "a", "b", null, "d" };

foreach (var item in words)
{
    Console.WriteLine(item);
}

It seems to me that the opposite is true: if you used string instead, then readers of the code would immediately know it was a string in the foreach loop, instead of having to look up in the code where the variable is defined.

How does implicit typing make the above code clearer?

Addendum

The book is C # 3.0 - Die Neuerungen. schnell + kompakt which is in German, the actual text is:

Das Schluesselwort var kann auch beim Durchlaufen von foreach-Schleifen verwendet werden, um somit den Code uebersichtlicher und einfacher zu gestalten. Besonders bei komplexen Typen kann man auf diese Art und Weise Programmierfehler verhindern.

here's my translation:

The var keyword can also be used when iterating through foreach loops, thus making the code easier and simpler to create. Especially when using complex types, this can prevent programming errors.

Ok, reading it more closely now he actually states that var in a foreach loop makes the code easier to create but not necessarily easier to read.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

别理我 2024-09-19 19:04:31

就我个人而言,我同意你的观点。我不确定我是否会使用“clearer”这个词,但在某些情况下,var关键字肯定可以使其更干净,即:

var myClass = new ExtremelyLongClassNameIWouldntWantToTypeTwice();

Personally, I'd agree with you. I'm not sure if clearer is the word I would use but in certain situations the var keyword can certainly make it cleaner, i.e:

var myClass = new ExtremelyLongClassNameIWouldntWantToTypeTwice();
拍不死你 2024-09-19 19:04:31

我认为奇怪的是,只有 C# 和 Java 程序员似乎遭受了无法从代码上下文中提取信息的困扰,而 Python、JavaScript、Ruby、F#、Haskell 等开发人员似乎对此免疫。为什么他们看起来做得很好,而我们C#程序员却需要讨论这个问题?

如果上述显式类型声明是草率或懒惰的,这是否意味着没有高质量、可读的 Python 代码?其实不是有很多人称赞Python的可读性吗? JavaScript 中的动态类型有很多让我烦恼的地方,但缺乏显式类型声明并不是其中之一。

静态类型语言中的类型推断应该是常态,而不是例外;它减少了视觉混乱和冗余,同时当您因为想要较少派生的类型而显式指定类型时,使您的意图更加清晰 (IListlist = new List() ;)。

有些人可能会像这样反对 var

var c = SomeMethod();

嗯,对此我想说你应该给你的变量提供更合理的名称。

改进:

var customer = SomeMethod();

更好:

var customer = GetCustomer();

让我们尝试显式输入:

Customer customer = GetCustomer();

您现在拥有哪些以前没有的信息?您现在可以肯定它的类型是 Customer,但您已经知道了,对吧?如果您已经熟悉代码,那么您只需通过变量名称就可以知道 customer 可以使用哪些方法和属性。如果您还不熟悉代码,您不知道 Customer 有哪些方法。这里的显式类型没有增加任何价值。

也许 var 的一些反对者可能会承认,在上面的例子中,var 没有什么坏处。但是,如果方法不返回简单且众所周知的类型(例如 CustomerOrder),而是返回一些经过处理的值(例如某种字典)怎么办?比如:

var ordersPerCustomer = GetOrdersPerCustomer();

我不知道返回什么,可能是字典,列表,数组,任何东西。但这有关系吗?从代码中,我可以推断出我将拥有一个可迭代的客户集合,其中每个 Customer 又包含一个可迭代的 Order 集合。 我真的不关心这里的类型。我知道我需要知道什么,如果结果证明我错了,那是该方法的错误,它的名称误导了我,这是无法通过显式类型声明来修复。

让我们看一下明确的版本:

IEnumerable<IGrouping<Customer,Order>> ordersPerCustomer = GetOrdersPerCustomer();

我不了解你,但我发现从中提取我需要的信息要困难得多。至少不是因为包含实际信息(变量名称)的位位于更右侧,在那里我需要花费更长的时间才能找到它,并且在视觉上被所有那些疯狂的 < 和<代码>>。实际类型毫无价值,它是官样文章,特别是因为要理解它,您需要知道那些泛型类型的作用。

如果在任何时候您仅从名称不确定方法的作用或变量包含什么,您应该给它一个更好的名称。这比看看它是什么类型更有价值。

显式类型不应需要,如果需要,则说明您的代码有问题,而不是类型推断有问题。它不可能是需要的,因为其他语言显然也不需要它。

也就是说,我确实倾向于对“基元”使用显式类型,例如 intstring。但老实说,这更多的是一种习惯,而不是一个有意识的决定。不过,对于数字,如果您忘记将 m 添加到您想要输入为 decimal 的文字数字,类型推断可能会搞砸您,这很容易做到,但编译器不会允许您意外丢失精度,因此这不是一个实际问题。事实上,如果我在任何地方都使用 var ,那么我所处理的大型应用程序中的数量从整数到小数的更改就会容易得多。

这是 var 的另一个优点:它允许快速实验,而无需强迫您更新各处的类型以反映您的更改。如果我想将上面的示例更改为 Dictionary[],我可以简单地更改我的实现,并且使用 var 调用它的所有代码将继续工作(好吧,至少是变量声明)。

I think it's odd that only C# and Java programmers seem to suffer from an affliction that prevents them from extracting information from the context of code, while developers of Python, JavaScript, Ruby, F#, Haskell and others seem to be immune to this. Why is it that they appear to be doing fine, but us C# programmers need to have this discussion?

If foregoing explicit type declarations is sloppy or lazy, does that mean there's no high quality, readable Python code? In fact, don't many people praise Python for being readable? And there are many things that irk me about dynamic typing in JavaScript, but lack of explicit type declarations isn't one of them.

Type inference in statically typed languages should be the norm, not the exception; it reduces visual clutter and reduncancy, while making your intention clearer when you do specify a type explicitly because you want a less derived type (IList<int> list = new List<int>();).

Some might argue a case against var like this:

var c = SomeMethod();

Well, to that I'd say you should give your variables more sensible names.

Improved:

var customer = SomeMethod();

Better:

var customer = GetCustomer();

Lets try explicit typing:

Customer customer = GetCustomer();

What information do you now have that you did not have before? You now know for certain it's of type Customer, but you already knew that, right? If you're familiar already with the code, you know what methods and properties you can expect on customer, just by the name of the variable. If you're not familiar with the code yet, you don't know what methods Customer has any way. The explicit type here added nothing of value.

Perhaps some opponents of var might concede that in the above example, var does no harm. But what if a method doesn't return a simple and well-known type like Customer, or Order, but some processed value, like some sort of Dictionary? Something like:

var ordersPerCustomer = GetOrdersPerCustomer();

I don't know what that returns, could be a dictionary, a list, an array, anything really. But does it matter? From the code, I can infer that I'll have an iterable collection of customers, where each Customer in turn contains an iterable collection of Order. I really don't care about the type here. I know what I need to know, if it turns out I'm wrong, it's the fault of the method for misleading me with its name, something which cannot be fixed by an explicit type declaration.

Lets look at the explicit version:

IEnumerable<IGrouping<Customer,Order>> ordersPerCustomer = GetOrdersPerCustomer();

I don't know about you, but I find it much harder to extract the information I need from this. Not in the least because the bit that contains the actual information (the variable name) is further to the right, where it will take my eyes longer to find it, visually obscured by all those crazy < and >. The actual type is worthless, it's gobbledygook, especially because to make sense of it, you need to know what those generic types do.

If at any point you're not sure what a method does, or what variable contains, just from the name, you should give it a better name. That's way more valuable than seeing what type it is.

Explicit typing should not be needed, if it is, there's something wrong with your code, not with type inference. It can't be needed, as other languages apparently don't need it either.

That said, I do tend to use explicit typing for 'primitives', like int and string. But honestly, that's more a thing of habit, not a conscious decision. With numbers though, the type inference can screw you if you forget to add the m to a literal number that you want to be typed as decimal, which is easy enough to do, but the compiler won't allow you to accidentally lose precision so it's not an actual problem. In fact, had I used var everywhere it would've made a change of quantities in an large application I work on from integers to decimal numbers a lot easier.

Which is another advantage of var: it allows rapid experimentation, without forcing you to update the types everywhere to reflect your change. If I want to change the above example to Dictionary<Customer,Order>[], I can simply change my implementation and all code that called it with var will continue to work (well, the variable declarations at least).

ι不睡觉的鱼゛ 2024-09-19 19:04:31

在这种情况下,情况并非如此,这当然只是我的主观意见。我只在类型位于同一行的其他位置时使用它,例如:

var words = new List<String>();

我喜欢 var,但我不会像在您的示例中那样使用它,因为不清楚类型是什么。

In this case it doesn't, which is just my subjective opinion of course. I only use it when the type is elsewhere on the same line like:

var words = new List<String>();

I like var, but I wouldn't use it like in your example as it is not clear what the type is.

白馒头 2024-09-19 19:04:31

噪音/冗余更少的意义上来说,它更清晰。编译器和开发人员都可以通过 new[] { ... } 语句轻松推断出单词的类型。因此,使用 var 代替 string[],因为后者会在视觉上使代码变得混乱。

透明度的意义上来说,它更加清晰。您可以将实际值与任何其他类型的实例交换,只要它是可枚举类型。如果您没有使用 var,则必须更改示例中的两个声明语句。

它更清晰,因为它迫使您使用好的变量名称。通过使用 var,您无法使用类型声明来指示变量的内容,因此您必须使用描述性名称。一个变量只声明一次,但可能会使用它很多次,所以最好能够通过变量的名称找出变量的内容。从这个角度来看,word 对于循环变量名来说是更好的选择。

请注意,以上推理是从作者的角度进行的。它不一定反映我的个人意见:)

编辑您的附录:

正如我之前提到的,您可以交换底层集合类型,而无需更新所有 foreach 循环。这确实使创建和更改代码变得更容易,但不一定能防止编程错误。在引入 Word 类来替换普通字符串之后,让我们看看这两种情况:

如果我们使用 var 关键字,编译器会捕获错误:

var words = new[] { new Word("a"), new Word("b"), null, new Word("d") };

// The compiler will complain about the conversion from Word to string,
// unless you have an implicit converion.
foreach (string word in words)
{
    Console.WriteLine(word);
}

如果我们do使用var,代码将编译没有错误,但程序的输出将完全不同,如果 Word 类尚未(正确)实现 ToString()

var words = new[] { new Word("a"), new Word("b"), null, new Word("d") };

foreach (var word in words)
{
    Console.WriteLine(word); // Output will be different.
}

因此,在某些情况下,当您使用 var 时可能会引入微妙的错误,否则编译器会捕获这些错误。

It's clearer, in the sense of less noise/redundancy. The type of words can be easily deduced by the new[] { ... } statement, both by the compiler and the developer. So var is used in stead of string[], as the latter can visually clutter the code.

It's clearer, in the sense of transparency. You can swap the actual value with an instance of any other type, as long as it's an enumerable type. If you didn't use var, you'd have to change both of the declaration statements in the example.

It's clearer, as it forces you to use good variable names. By using var, you cannot use the type declaration to indicate the contents of the variable, so you'll have to use a descriptive name. You only declare a variable once, but you may use it many times, so it's better to be able to figure out the contents of the variable by it's name. From this perspective, word would have been a better choice for the loop variable name.

Please note that the above reasoning is done from the author's perspective. It doesn't necessarily reflect my personal opinion :)

Edit regarding your addendum:

As I mentioned before, you can swap the underlying collection type, without having to update all your foreach loops. This does make it easier to create and change your code, but doesn't necessarily prevent programming errors. Let's look at both cases after we introduce a Word class as a replacement of the plain strings:

If we don't use the var keyword, the compiler will catch the error:

var words = new[] { new Word("a"), new Word("b"), null, new Word("d") };

// The compiler will complain about the conversion from Word to string,
// unless you have an implicit converion.
foreach (string word in words)
{
    Console.WriteLine(word);
}

If we do use var, the code will compile without errors, but the output of the program will be completely different, if the Word class hasn't (properly) implemented ToString().

var words = new[] { new Word("a"), new Word("b"), null, new Word("d") };

foreach (var word in words)
{
    Console.WriteLine(word); // Output will be different.
}

So, in certain cases subtle bugs can be introduced when you use var, which would have been caught by the compiler otherwise.

嗼ふ静 2024-09-19 19:04:31

这里的干净意味着更少的冗余。由于编译器很容易推断出对象的类型是 string[],因此显式指定它被认为是冗长的。然而,正如您所指出的,对于阅读代码的人来说,这可能不是那么明显。

Clean here means less redundant. Since it is trivial for the compiler to infer that the type of the object is string[], it is considered verbose to specify it explicitly. As you point out, however, it may not be so obvious to the human reading the code.

饮惑 2024-09-19 19:04:31

这个例子很糟糕,就像许多展示语法糖的例子一样 - 语法糖在事情复杂的时候有帮助,但没有人喜欢复杂的例子。

在两种情况下您可能需要使用 var,另一种情况您必须使用:

您可能需要它的情况:

  1. 当您在探索自己的代码时快速切换所涉及的类型时,它在实验代码中非常有用。问题空间。
  2. 它对于复杂的基于泛型的类型很有用,例如 IGrouping>>,尤其是在复杂的中间状态下发生的情况查询和枚举操作。

就我个人而言,我更喜欢使用复杂的形式而不是 var,因为不关心确切类型的人不需要阅读它(他们可以跳过它,认为“复杂的分组类型”) ),但对于真正关心这个问题的人来说是很清楚的,而无需他们自己解决。

你需要它的地方:

在处理匿名类型时,在如下代码中:

var res = from item in src select new {item.ID, item.Name};    
foreach(var i in res)    
    doSomething(i.ID, i.Name);

这里 res 是匿名类型的 IEnumerable 或 IQueryable,而 i 是该匿名类型。由于该类型没有名称,因此无法显式声明它。

在最后一种情况下,它不是语法糖,但实际上至关重要。

一个相关的抱怨是,SharpDevelop 曾经有自己的 var 编辑时形式;可以输入:

? words = new string[] { "a", "b", null, "d" };

在分号处,编辑器将生成:

string[] words = new string[] { "a", "b", null, "d" };

这提供了(特别是在更复杂的情况下)打字速度以及生成显式代码的优势。他们似乎已经放弃了这一点,因为 var 在概念上做了同样的事情,但遗憾的是失去了显式形式的打字快捷方式。

The example is poor, as many examples demonstrating syntactic sugar tend to be - syntactic sugar helps where things are complicated, but nobody likes complicated examples.

There are two cases where you might want to use var, and one where you must:

Where you might want it:

  1. It can be useful in experimental code, when you are switching the types involved quickly while exploring your problem-space.
  2. It can be useful with complicated generic-based types such as IGrouping<int, IEnumerable<IGrouping<Uri, IGrouping<int, string>>>> which can especially happen with intermeditary states within complex queries and enumeration operations.

Personally, I prefer to use even the complicated form over var, as it doesn't cost someone reading it who doesn't care about the exact type (they can just skip it thinking "complicated grouping type"), but is clear to someone who does care without their having to work it out themselves.

Where you need it:

In dealing with anonymous types, in code like:

var res = from item in src select new {item.ID, item.Name};    
foreach(var i in res)    
    doSomething(i.ID, i.Name);

Here res is an IEnumerable or IQueryable of an anonymous type, and i is of that anonymous type. Since the type has no name it's impossible to explicitly declare it.

In this last case, it is not syntactic sugar, but actually vital.

A related gripe, is that SharpDevelop used to have it's own while-editting form of var; one could type:

? words = new string[] { "a", "b", null, "d" };

And at the semi-colon, the editor would produce:

string[] words = new string[] { "a", "b", null, "d" };

Which gave (especially in more complicated cases) the advantage of typing speed along with producing explicit code. They seem to have dropped this now that var does conceptually the same thing, but it's a pity to have lost the typing shortcut to the explicit form.

爱情眠于流年 2024-09-19 19:04:31

使用隐式类型是一个指南,而不是法律。

你提出的是一个极端的例子,隐式肯定不理想。

Using implicit typing is a guideline, not a law.

What you brought up is an extreme example where implicit is certainly not ideal.

原来是傀儡 2024-09-19 19:04:31

时,它会使代码更清晰

  1. 当您有一个名称很长的类
  2. 。您有 linq 查询。

It makes the code clearer when

  1. You have a class with a really long name.
  2. You have linq queries.
耳根太软 2024-09-19 19:04:31

在我开始使用 F# 之前,我认为我并没有真正理解 C# 中隐式类型的优点。 F# 具有类型推断机制,在某些方面类似于 C# 中的隐式类型。在 F# 中,类型推断的使用是代码设计的一个非常重要的方面,即使在简单的示例中,它也确实可以使代码更具可读性。学习在 F# 中使用类型推断有助于我理解隐式类型可以使 C# 更具可读性而不是更令人困惑的情况。 (我认为 Linq 的情况相当明显,但很多情况并非如此。)

我意识到这听起来更像是 F# 的插件,而不是问题 re.C# 的答案,但这不是我可以确定的问题一组简单的规则。这是一种在考虑可读性和维护等问题时以新的眼光看待代码的学习。 (那个,也许它是 F# 的一点插件,哈哈。)

I don't think I really understood the good aspects of implicit typing in C# until I started using F#. F# has a type inference mechanism that is in some ways similar to implicit typing in C#. In F#, the use of type inference is a very important aspect of code design that can really make the code more readable even in simple examples. Learning to use type inference in F# helped me understand those situations when implicit typing could make C# more readable, as opposed to more confusing. (The Linq case is, I think, fairly obvious, but many cases are not.)

I realize this sounds more like a plug for F# than an answer to the question re.C#, but it's not something that I can pin down to a simple set of rules. It's a learning to look at the code through new eyes when thinking about things like readability and maintenance. (That, and maybe it is a little bit of a plug for F#, lol.)

风追烟花雨 2024-09-19 19:04:31

如果你想让这段代码更明确,我建议扩展新的而不是删除 var:

var words = new string[] { "a", "b", null, "d" };

Resharper 会提示你删除该示例中的冗余代码,但如果你愿意,你可以关闭这些提示。

使用 var 的理由是,在许多情况下,局部变量的类型标识符是多余的,应该删除多余的代码。通过删除冗余代码,您可以使您真正关心的情况更加清晰,例如,如果您想为局部变量强制执行特定的接口类型:

ISpecificInterface foo = new YourImplementation()

If you wanted to make this code more explicit, I would suggest expanding the new instead of removing var:

var words = new string[] { "a", "b", null, "d" };

Resharper will give you hints to remove redundant code with that example, but you can turn those hints off if you like.

The argument for using var is that in many cases the type identifiers for local variables are redundant, and redundant code should be removed. By removing redundant code, you can make the cases where you do actually care clearer, for example if you want to enforce a specific interface type for a local variable:

ISpecificInterface foo = new YourImplementation()
世界等同你 2024-09-19 19:04:31

当您需要更改代码时,您必须在更少的地方进行更改。不再有 Dictionary 或更长的类型声明,不再有 Some.Very.Long.Class.With.Very.Long.Path;声明 = functionParameter[index] 等。

尽管我确实同意,当它用于除小方法之外的其他情况时,它可能会变得非常混乱。

When you need to change your code, you'd have to do it in less places. No more Dictionary or longer type declarations, no more Some.Very.Long.Class.With.Very.Long.Path<string> declaration = functionParameter[index], etc.

Although I do agree that when it is used in other situations than small methods, it might get very confusing.

怎会甘心 2024-09-19 19:04:31

在我看来,显式类型的主要好处是,只需查看代码即可确定变量的类型。所以可读性增加了。

隐式类型的主要好处是:

  • 减少程序文本,特别是长类型名称(类、接口)的程序文本,从而提高可读性
  • 当方法的返回类型更改时,会导致更少的更改。

看起来这两个选项都提高了可读性。

所以我想这取决于您的喜好,也许还取决于您团队中的编程指南。借助 IDE 中当前(重构)工具的支持,更改类型名称变得更加容易(无需动脑筋),因此从工作量角度来看,隐式类型减少更改的原因实际上已经消失。

我建议:做最适合你的事情。没有对错之分。尝试每种方法一段时间(例如,通过配置您最喜欢的重构工具的选项),然后使用使您作为开发人员的生活更轻松的方法。

The primary benefit of explicit typing is in my view that it's possible by just looking at the code what type the variable has. So readability is increased.

And the primary benefits of implicit typing are:

  • Reduces program text in particular for long type names (classes, interfaces) thus improving readibility
  • Causes fewer changes when a return type of a method changes.

It looks as if both options improve readability.

So I guess it depends on your preferences and maybe also on the programming guidelines in your team. With current (refactoring) tool support in IDE's it has become much easier to change type names (a no brainer) so the reason that implicit typing reduces changes has virtually disappeared from an effort perspective.

I'd suggest: Do what works best for you. There is no right or wrong. Try each approach for a while (e.g. by configuring the options of your favorite refactoring tool), then use what makes your life as a developer easier.

少女七分熟 2024-09-19 19:04:31

好吧,您已经掌握了重要的想法 - 过度使用 var 确实可能是有害的,并且在实际类型非常简单的情况下,应该这样声明。

然而,在处理更大的继承层次结构和模板时,var 表现出色。您也可以将其理解为“我不在乎 - 只需给我数据” 虽然 C# 中的模板不具备 C++ 模板的表达能力,但它们确实比 Java 的泛型具有更强的表达能力,这意味着人们如果您必须明确地确定确切的类型,则可以创建不仅尴尬而且难以定位的构造。

例如 - 想象一个围绕几种 DataReader-s 的模板包装器用于与 SQL 对话 - 这样您仍然可以高效(调用存储过程,获取结果,完成),但没有内务处理的负担(关闭读取器和连接,重试或完成)错误等)。使用它的代码将只调用一个函数,该函数具有尽可能短的语法,并且它将返回一个包装器,该包装器的作用类似于 C++ 中的智能指针 - 类似于代码的 DataReader,但也处理所有横向事务。所以它看起来很简单:

using (var r = S.ExecuteReader("[MySproc]", params))
{
    while ((+r).Read())
    (
       // get my data and be done
    )
} // at this point wrapper cleans up everything

在这样的情况下,不仅你不在乎包装器是如何命名和声明的,你甚至不在乎知道它的名称 - 对于你的代码来说它是无关紧要的。你只想要你的该死的数据并继续:-)而不处理任何人的长声明。

它实际上允许您选择何时关心完整类型声明,何时不关心。这不是全有或全无的事情,您会发现自己使用这两种样式。

如果您开始使用 lambda 表达式,您会很高兴拥有它的另一个地方。如果您在 C# 中使用 lambda,几乎总是因为您想要一些简短、紧凑的代码,该代码将运行一次,并且不值得将其转换为常规方法,或者它依赖于宿主方法中的局部变量。

哦,甚至 VS 编辑器也会为你推断完整类型,提供自动完成功能,并在你尝试使用它不能做的事情时抱怨,所以 var 根本不会破坏类型安全(新的 C++ 也得到了 var 的等价物 -早就应该了)。

Well, you have picked up important idea - that overuse of var can be detrimental indeed and that in cases where actual type is pretty simple it should be stated as such.

var shines however when dealing with larger inheritance hierarchies and templates. You can also read it as "I don't care - just give me data" While templates in C# don't have expressive power of their C++ counterpart they do have more expressive power than Java's generics and that means that people can make constructs which are not just awkward but also hard to located if you have to nail exact type explicitly.

For example - imagine a template wrapper around several kinds of DataReader-s for talking to SQL - so that you can still be efficient (call sproc, get results, done) but without the burden of housekeeping (closing the reader and connection, retrying or errors etc). The code that uses it will just call one function, made to have as short syntax as possible and it will return a wrapper which will act like smart pointer in C++ - act like DataReader for your code but also handle all sideways things. So it looks as simple as:

using (var r = S.ExecuteReader("[MySproc]", params))
{
    while ((+r).Read())
    (
       // get my data and be done
    )
} // at this point wrapper cleans up everything

In a case like this, not just that you couldn't care less how is wrapper named and declared, you don't even care to know it's name - for your code it's irrelevant. You just want your darn data and to go on :-) without dealing with anyones long declarations.

It literally allows you to choose when you care about the full type declaration and when not. It's not all or nothing thing and you'll find yourself using both styles.

Another place where you'll be happy to have it is lambda expressions if you start using them. If you use lambdas in C# it will almost always be because you want some short, compact code that will run once and it's either not worth the trouble to turn into a regular method or it depends on local variables from the host method.

Oh and even VS editor will infer full type for you, offer auto-completion and complain if you try to use something it can't do, so var doesn't break type safety at all (new C++ got it's var equivalent as well - long overdue).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文