I like to return var from my method.
I manage to use dynamic, but then my try-catch-finally does not work.
Could you improve my method to return var?
I do not need to use dynamic, I am looking for the best method to return var.

you can't return var. What type would it be....?
– Mitch WheatJul 6 '13 at 12:50

why you need to return var? why not return an array? or use generics?
– MEYWDJul 6 '13 at 12:51

I don't think it's possible to return var. var is typed when the code is compiled. Returning var would be confusing.
– Khanh TOJul 6 '13 at 12:51

2

You may benefit from describing what exactly you're trying to do with this method. You're actually always returning a string[] according to your last line, it's unclear why you can't just use that as the return type.
– ChrisJul 6 '13 at 12:53

@RBrattas, You do not need to return dynamic to return an array of unknown size. Every time dynamic pops up, you’re putting method resolution, etc., of to runtime which makes for more confusing failures. It would be much better to have your method just return string[] or even IEnumerable<string> or List<string>.
– binkiMar 25 '15 at 19:02

Could you improve my method to return var? I do not need to use
dynamic, I am looking for the best method to return var.

var is not a type but a C# keyword for shortening and making the code more readable (an implicitly typed local variable) - read here about it more deeply. So forget about returning var - Use a real type in your method signature, just like: int, string etc.

Frustrates me more and more that this isn't possible - as typescript adds more and more clever functionality of a similar nature. There are obviously complexities to implementing this (like cross assembly issues), but there's no reason why a function couldn't return its anonymous type as a 'var' and pass on the definition to whatever calls it. The type may be anonymous but it exists in the CLR as a real type! The compiler can figure it out from there. Certain limitations are fine - I'd be fine if this was limited to being internal to a class. I really hope someday we can get this.
– Simon_WeaverNov 10 '18 at 20:48

However... there are now tuples in C# 7 which allows you to create statically typed 'types' in place that can be returned from a method (or even used as a generic type parameter). They can offer some similarities to creating anonymous types that can alleviate the need for var as I said earlier. docs.microsoft.com/en-us/dotnet/csharp/tuples
– Simon_WeaverNov 11 '18 at 0:42