implicit-typing

Declaring an implicitly typed variable inside conditional scope and using it outside

感情迁移 提交于 2019-11-30 21:27:30
In the simplified code below, if(city == "New York City") { var MyObject = from x in MyEFTable where x.CostOfLiving == "VERY HIGH" select x.*; } else { var MyObject = from x in MyEFTable where x.CostOfLiving == "MODERATE" select x.*; } foreach (var item in MyObject) { Console.WriteLine("<item's details>"); } The variable MyObject is not accessible outside conditional block. How can I iterate outside the if..else ? Let's clarify your confusing question. The problem is that you have two local variables, each of which has the same "unspeakable" type -- a sequence of anonymous type. I would change

Declaring an implicitly typed variable inside conditional scope and using it outside

瘦欲@ 提交于 2019-11-30 17:08:36
问题 In the simplified code below, if(city == "New York City") { var MyObject = from x in MyEFTable where x.CostOfLiving == "VERY HIGH" select x.*; } else { var MyObject = from x in MyEFTable where x.CostOfLiving == "MODERATE" select x.*; } foreach (var item in MyObject) { Console.WriteLine("<item's details>"); } The variable MyObject is not accessible outside conditional block. How can I iterate outside the if..else ? 回答1: Let's clarify your confusing question. The problem is that you have two

var in C# - Why can't it be used as a member variable? [duplicate]

爱⌒轻易说出口 提交于 2019-11-29 13:17:39
This question already has an answer here: Implicit typing; why just local variables? 6 answers Why is it not possible to have implicitly-typed variables at a class level within C# for when these variables are immediately assigned? ie: public class TheClass { private var aList = new List<string>(); } Is it just something that hasn't been implemented or is there a conceptual/technical reason for why it hasn't been done? Here's a blog post from Eric that explains the reasoning. 来源: https://stackoverflow.com/questions/2771485/var-in-c-sharp-why-cant-it-be-used-as-a-member-variable

Using implicitly typed local variables [duplicate]

 ̄綄美尐妖づ 提交于 2019-11-28 17:42:59
This question already has an answer here: Why would var be a bad thing? 17 answers I just installed a trial version of ReSharper and one of the first things I noticed is that it always suggests to replace explicitly typed local variables with implicitly typed ones, e.g: public string SomeMethod(int aParam) { int aNumber = SomeOtherMethod(aParam); // should be changed to: var aNumber = SomeOtherMethod(aParam); } I think explicitly typed variables are more readable (more explicit). What do you think about ReSharper's suggestion? Is there any advantage in using implicitly typed variables? When do

Inferring type of generic implicit parameter from return type

廉价感情. 提交于 2019-11-28 00:41:13
问题 Say I have a simple class like this abstract class Foo { implicit val impInt: Int = 42 def f[A]()(implicit a: A): A val f2: Int = f() } When declaring val f2 , compiler is able to infer that the type of implicit parameter of function f is Int because that type is the same as the result type, and result type needs to match the type of value f2 , which is Int . However, throwing an Ordering[A] into the mix: def f[A]()(implicit a: A, m: Ordering[A]): A val f2: Int = f() results in this compile

Why would var be a bad thing?

江枫思渺然 提交于 2019-11-27 18:28:26
I've been chatting with my colleagues the other day and heard that their coding standard explicitly forbids them to use the var keyword in C#. They had no idea why it was so and I've always found implicit declaration to be incredibly useful when coding. I've never had any problems finding out what type the variable was (you only hover over the variable in VS and you'll get the type that way). Does anyone know why it would be a bad idea to use the var keyword in C#? The writers of the .Net Framework Design Guidelines (awesome book) that came out in November 2008 recommend considering using var

Why can't I use the array initializer with an implicitly typed variable?

谁都会走 提交于 2019-11-27 14:02:36
Why can't I use the array initializer with an implicitly typed variable? string[] words = { "apple", "strawberry", "grape" }; // legal string[] words = new string[]{ "apple", "strawberry", "grape" }; // legal var words = new []{ "apple", "strawberry", "grape" }; // legal var words = new string[]{ "apple", "strawberry", "grape" }; // legal var words = { "apple", "strawberry", "grape", "peach" }; // ILLEGAL Is there a technical reason for this limitation? Why can't it infer the type like it would for: var number = 10; var text = "Hello"; The compiler clearly knows what I am trying to do, it just

Using implicitly typed local variables [duplicate]

岁酱吖の 提交于 2019-11-27 10:40:20
问题 This question already has answers here : Why would var be a bad thing? (17 answers) Closed 5 years ago . I just installed a trial version of ReSharper and one of the first things I noticed is that it always suggests to replace explicitly typed local variables with implicitly typed ones, e.g: public string SomeMethod(int aParam) { int aNumber = SomeOtherMethod(aParam); // should be changed to: var aNumber = SomeOtherMethod(aParam); } I think explicitly typed variables are more readable (more

Why would var be a bad thing?

你离开我真会死。 提交于 2019-11-26 19:27:10
问题 I've been chatting with my colleagues the other day and heard that their coding standard explicitly forbids them to use the var keyword in C#. They had no idea why it was so and I've always found implicit declaration to be incredibly useful when coding. I've never had any problems finding out what type the variable was (you only hover over the variable in VS and you'll get the type that way). Does anyone know why it would be a bad idea to use the var keyword in C#? 回答1: The writers of the

Why can't I use the array initializer with an implicitly typed variable?

自闭症网瘾萝莉.ら 提交于 2019-11-26 16:35:14
问题 Why can't I use the array initializer with an implicitly typed variable? string[] words = { "apple", "strawberry", "grape" }; // legal string[] words = new string[]{ "apple", "strawberry", "grape" }; // legal var words = new []{ "apple", "strawberry", "grape" }; // legal var words = new string[]{ "apple", "strawberry", "grape" }; // legal var words = { "apple", "strawberry", "grape", "peach" }; // ILLEGAL Is there a technical reason for this limitation? Why can't it infer the type like it