implicit-typing

Define variable type conditionally C#

a 夏天 提交于 2021-02-05 11:55:28
问题 In "ADO.NET Entity Data Model" I have created a "database first" model from a number of tables. All tables have "code" and "name" fields and different set of other fields. Then I've created a "context" object. Now I want to create a variable "src_table", which will be assigned to context.table1 or context.table2 etc conditionally and then work with src_table.code and src_table.name properties. A code like this works fine: var context = new postgresEntities(); var src_table = context.table1;

Define variable type conditionally C#

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 11:55:21
问题 In "ADO.NET Entity Data Model" I have created a "database first" model from a number of tables. All tables have "code" and "name" fields and different set of other fields. Then I've created a "context" object. Now I want to create a variable "src_table", which will be assigned to context.table1 or context.table2 etc conditionally and then work with src_table.code and src_table.name properties. A code like this works fine: var context = new postgresEntities(); var src_table = context.table1;

How does implicit typing make code clearer?

情到浓时终转凉″ 提交于 2019-12-19 05:23:50
问题 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

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

两盒软妹~` 提交于 2019-12-18 07:36:29
问题 This question already has answers here : Implicit typing; why just local variables? (6 answers) Closed 5 years ago . 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? 回答1: Here's a blog post from Eric that explains the

How should I pass a user-defined type to SqlParameterCollection.AddWithValue?

此生再无相见时 提交于 2019-12-11 05:05:02
问题 I have a custom data type called StudentID , which has an implicit conversion to string. When I pass a StudentID instance to SqlCommand.Parameters.AddWithValue (as the value) and execute the command, I receive the following error: No mapping exists from object type StudentID to a known managed provider native type. Specifying a type for the parameter like SqlDbType.NVarChar doesn't help. The only thing that works is to explicitly cast the StudentID value to a string, which defeats the purpose

Implicitly-Typed Variables in try…catch

蓝咒 提交于 2019-12-07 02:30:24
问题 I like using implicit typing for almost everything because it's clean and simple. However, when I need to wrap a try...catch block around a single statement, I have to break the implicit typing in order to ensure the variable has a defined value. Here's a contrived hypothetical example: var s = "abc"; // I want to avoid explicit typing here IQueryable<ABC> result = null; try { result = GetData(); } catch (Exception ex) { } if (result != null) return result.Single().MyProperty; else return 0;

Implicit typing of arrays that implement interfaces

风格不统一 提交于 2019-12-06 23:10:22
问题 I was under the impression that the C# compiler will implicitly type an array based off a type that they can all be implicitly converted to. The compiler generates No best type found for implicitly-typed array public interface ISomething {} public interface ISomething2 {} public interface ISomething3 {} public class Foo : ISomething { } public class Bar : ISomething, ISomething2 { } public class Car : ISomething, ISomething3 { } void Main() { var obj1 = new Foo(); var obj2 = new Bar(); var

Use of C# var for implicit typing of System.Data.Datarow

别来无恙 提交于 2019-12-04 16:11:47
问题 foreach (var row in table.Rows) { DoSomethingWith(row); } Assuming that I'm working with a standard System.Data.DataTable (which has a collection of System.Data.DataRow objects), the variable 'row' above resolves as an object type, not a System.Data.DataRow . foreach (DataRow row in table.Rows) { DoSomethingWith(row); } Works as I would expect. Is there a particular reason for this? Thanks. 回答1: That's because Rows is DataRowCollection , which in turn is IEnumerable and not IEnumerable

Why doesn't C# let you declare multiple variables using var?

有些话、适合烂在心里 提交于 2019-12-03 15:31:15
问题 Given the following: // not a problem int i = 2, j = 3; so it surprises me that this: // compiler error: Implicitly-typed local variables cannot have multiple declarators var i = 2, j = 3; doesn't compile. Maybe there is something I don't understand about this (which is why I'm asking this)? But why wouldn't the compiler realize that I meant: var i = 2; var j = 3; which WOULD compile. 回答1: It's just another point of possible confusion for the programmer and the compiler. For example this is

Why doesn't C# let you declare multiple variables using var?

走远了吗. 提交于 2019-12-03 05:13:54
Given the following: // not a problem int i = 2, j = 3; so it surprises me that this: // compiler error: Implicitly-typed local variables cannot have multiple declarators var i = 2, j = 3; doesn't compile. Maybe there is something I don't understand about this (which is why I'm asking this)? But why wouldn't the compiler realize that I meant: var i = 2; var j = 3; which WOULD compile. James Gaunt It's just another point of possible confusion for the programmer and the compiler. For example this is fine: double i = 2, j = 3.4; but what does this mean? var i = 2, j = 3.4; With syntactic sugar