implicit-typing

Why would var be a bad thing?

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 ov...

Using implicitly typed local variables

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...

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

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 ...

Implicit typing; why just local variables?

Does anyone know or care to speculate why implicit typing is limited to local variables? var thingy = new Foo(); But why not... var getFoo() { return new Foo(); } ...

Class implicit conversions

I know that I can use implicit conversions with a class as follows but is there any way that I can get a instance to return a string without a cast or conversion? public class Fred { public static implicit operator string(Fred fred) { return DateTime.Now.ToLongTimeString(); } } public class Program { static void...

What are the benefits of implicit typing in C# 3.0 >+

The only advantage I can see to do: var s = new ClassA(); over ClassA s = new ClassA(); Is that later if you decide you want ClassB, you only have to change the RHS of the declaration. I guess if you are enumerating through a collection you can also just to 'var' and then figure out the type later. Is that it?? Is there some othe...

Will "long i = 1;" cause an implicit type conversion in C?

If I write "long i = 1;" instead of "long i = 1l;", will the 1 be recognized as int and then implicitly converted to long? Edit: Thank you all. I see there's no type conversion. Is this also the case with the suffix u (like 10u)? Then what's the use of those l and u? ...

Why can't I do this with implicit types in C#?

var x = new { a = "foobar", b = 42 }; List<x.GetType()> y; Is there a different way to do what I want to do here? If there's not, I don't really see all that much point in implicit types... ...

Implicit type conversions in expressions int to double

I've been trying to reduce implicit type conversions when I use named constants in my code. For example rather than using const double foo = 5; I would use const double foo = 5.0; so that a type conversion doesn't need to take place. However, in expressions where I do something like this... const double halfFoo = foo / 2; etc. I...

Are implicitly-typed variables the way forward (C#)

I am using StyleCop for Resharper on a project originally written for .net v2. But I've since upgraded this project for 3.5 framework. Stylecop is recommending I change the bulk of my explicitly typed variables to implicitly typed for example: string - var custom strong type - var Is this the way forward when working with a .net 3....

Implicit typing and TDD

I just read this post and it makes the case against implicit typing using when starting out with Test driven development/design. His post says that TDD can be "slowed down" when using implicit typing for the return type when unit testing a method. Also, he seems to want the return type specified by the test in order to drive development...

Configuring implicit typing rules for Oracle

Is it possible to configure the implicit typing rule in Oracle Server (at least version 10g) ? If not a link to the documentation of the rules and how Oracle parameters impact the rules would be great. For exemple when executing this query : SELECT '' AS A FROM DUAL Oracle will report that column A has VARCHAR(0) type on Oracle 10g a...

How often do you create implicit conversions for your classes?

Hey there, I've been developing .NET applications for 4 years. So far, I did not need to create any implicit conversions for the classes I authored. Could you provide real-life situations when you could not do without creating implicit conversions? Thank you ...

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

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? ...

Implicit typing of arrays that implement interfaces

Hi, 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 : ISome...

How does implicit typing make code clearer?

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 immed...

Scala: convert a return type into a custom trait

I've written a custom trait which extends Iterator[A] and I'd like to be able to use the methods I've written on an Iterator[A] which is returned from another method. Is this possible? trait Increment[+A] extends Iterator[A]{ def foo() = "something" } class Bar( source:BufferedSource){ //this ain't working def getContents(...

What is the difference between var foo = new Love(); AND object foo = new Love(); ?

Hi, As I am not familiar with implicit typing; can you please tell me the main differences between: var foo = new Love(); AND object foo = new Love(); ...