0

So I'm creating a connection library for a colleague of mine to save him time on his current project. My colleague will use this library in his c# application to connect to a rest api. Within the library I created Handlers for each request(GET / POST / PUT / DEL). When his application talks to my library i will return the response like this:

return client.PostAsync(url, content).Result;

This returns a dynamic object from the rest api.

Today he used my library and couldn't get it to work in combination with his application for some reason. I told him to use a var and that it would work like this:

var x = API.CreateTraject(parameter1,parameter2);

He refused to use var and ended up spending about 40 min figuring out how to get it to work without it. Then he blamed me for returning a dynamic object and that he will never use var because explicit is better so he told me.

Im normaly working as mobile developer (IOS / Android) where i use var all the time.

Now my question is:

Is it really so bad to use var? Should I convert the response in my library so he can explicitly type it in his application? In my opinion I would rather use var and save some time then spend 40 min trying go make it explicit.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Florian Schaal
  • 2,586
  • 3
  • 39
  • 59
  • The comments that I just deleted here point out *exactly* why this type of question is not left open here. You got good information below. Read it, check the Internet, or get a book. – Andrew Barber Jul 20 '13 at 10:35

5 Answers5

12

Is it really so bad to use var? Should I convert the response in my library so he can explicitly type it in his application? In my opinion I would rather use var and save some time then spend 40 min trying go make it explicit.

var, in C#, is merely a compiler "trick". There is no dynamic typing involved, and the compiled code is exactly the same. When you hover your mouse over the variable, the IDE will tell you the "real" type that gets used.

Whether he uses var or your actual return type shouldn't matter at all in terms of how you create your library.

If your library is returning dynamic unnecessarily, that may be a different issue, and one which the user may have valid complaints. Unlike var (which is merely a compile time trick), dynamic does change the behavior significantly.

Alternatively, if you're returning anonymous types from your library, you may want to consider making an actual class for your values. Anonymous types are really only intended to be used within a local scope, and shouldn't be part of any public API.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks, The dynamic is returned by the rest api wich is made by another software company that we are working with. I feel like returning the response of the rest api is how it should be and if it works by using a var why not. – Florian Schaal Jul 19 '13 at 16:47
  • @FlorianSchaal If it's truly a dynamic object, then have him use dynamic, not var... – Reed Copsey Jul 19 '13 at 16:49
  • Ok, But wouldn't var make it sort of strongly typed to work with in future code? Say for example the dynamic is a json container and he needs to extract the data from it. – Florian Schaal Jul 19 '13 at 16:53
  • 2
    @FlorianSchaal If you're returning dynamic, var will just be "dynamic" - var just says "use the type of whatever is being used to initialize me". – Reed Copsey Jul 19 '13 at 16:55
  • Ah ok, This makes it clear for me! Thanks for the information. – Florian Schaal Jul 19 '13 at 17:24
  • I completley agree with this answer. If you are looking for more elaborated version check this out : http://www.c-sharpcorner.com/UploadFile/b1df45/var-vs-dynamic-keywords-in-C-Sharp/ – Amit Andharia Dec 05 '14 at 09:38
3

There is nothing illegal with using var, it just lets the compiler figure out what data type the object should be.

The only danger is that you, the programmer, don't know what it is until you mouse over. This can lead to problems where you thought it was one thing, but it turned out to be something else.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
3

It is okay to use var, it's not illegal or anything.

But I think if you know the type of variable, declare the variable with the type. This will make your code easier to read.

sora0419
  • 2,308
  • 9
  • 39
  • 58
1

It's not bad to use implicit typing, it's just a matter of style. I personally use it a lot simply because it makes all my variable declarations take up the same amount of space, regardless of type.

However, using dynamic definitely can be bad, especially when it's used excessively. It means that type safety checks that can be performed compile-time need to be deferred until run time. Occasionally that's useful, but it can have performance impacts. Unless you really need to return a dynamic, I'd strongly recommend returning a specific type from your API method.

For what it's worth, if your coworker is so opposed to implicit typing he could've just used this:

dynamic x = API.CreateTraject(parameter1,parameter2);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
1

var has nothing to do with dynamic. var is about type inference.

Your methods should not return dynamic to begin with. In any case create Generic Methods and let the consumer (your Colleague) decide what type of object the method will return.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154