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.

4

5 回答 5

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.

于 2013-07-19T16:37:17.670 回答
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.

于 2013-07-19T16:37:03.323 回答
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.

于 2013-07-19T16:38:44.053 回答
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);
于 2013-07-19T16:38:10.007 回答
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.

于 2013-07-19T16:38:37.227 回答