-4

Why do I have to write:

public VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName> field = new VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>();

Instead of:

public var field = new VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>();

Is there any way to shorten this ridiculous declaration? Why do I have to write two identical types in a single line?

Upd. I have found that using "dynamic" keyword instead of "var" perfectly solves this problem! Please feel free to provide any info on the perfomance (or other) issues with this solution!

Nidere
  • 107
  • 2
  • 9
  • Your concern is that the long type name results in a long field declaration. Aliasing the type name with a shorter one will address that aspect. See first marked duplicate. Your question is unclear, but it's possible you are also/instead asking why you can't use `var`. See second marked duplicate for that. – Peter Duniho Mar 15 '20 at 00:36
  • @PeterDuniho https://stackoverflow.com/questions/824739/implicit-typing-why-just-local-variables is also pertinent. – Zer0 Mar 15 '20 at 00:37
  • if it was me, I would initialize the properties in the constructor anyways. – Hooman Bahreini Mar 15 '20 at 00:41
  • I have found that using "dynamic" keyword instead of "var" perfectly solves this problem! Please feel free to provide any info on the perfomance (or other) issues with this solution! – Nidere Mar 15 '20 at 00:47
  • 4
    _"using "dynamic" keyword instead of "var" perfectly solves this problem!"_ -- no, it doesn't. At compile-time `dynamic` is effectively `object`, delaying all of the resolution of uses of the variable to runtime. You'll get no Intellisense on the field, no compile-time type-checking, no verification of member usages, etc. This is a _horrible_ way to deal with the issue. Frankly, you are making too much of the problem; Intellisense makes it very quick to _type_ the declaration, reading it is not any trouble at all, and if you really hate it, just use a type alias as seen in the duplicate. – Peter Duniho Mar 15 '20 at 00:50
  • Do *not* use `dynamic` to simply shorten a line of code. Among other things you are disabling compile time type checks. That's not the intended usage of `dynamic`. I would strongly advise researching how `dynamic` works before using it in your code. – Zer0 Mar 15 '20 at 00:52
  • Not to mention the fact that for dynamic a separate runtime is required, the DLR. – Sнаđошƒаӽ Mar 15 '20 at 05:18

1 Answers1

1

You can use a type alias. Documentation can be found here. Link to alias for generic class: here

using YourShortName = VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>;

Usage

public YourShortName field = new YourShortName();

See here for why you can only use var for local variables.

And see here for an in depth look at dynamic and why it's not even remotely close to using var.

Zer0
  • 7,191
  • 1
  • 20
  • 34
  • @RobertHarvey Is that something you can do with fields? – Joe Sewell Mar 15 '20 at 00:31
  • @RobertHarvey the OP is asking about a field, `var` wouldnt work – maccettura Mar 15 '20 at 00:32
  • I am mostly asking why wouldn't it work: am I asking too much from the compiler? I just don't understand this silly boilerplate – Nidere Mar 15 '20 at 00:33
  • I have found that using "dynamic" keyword instead of "var" perfectly solves this problem! Please feel free to provide any info on the perfomance (or other) issues with this solution! – Nidere Mar 15 '20 at 00:47
  • 2
    @Nidere You likely want to search SO for answers on this. But a _very_ big difference is you are disabling compile time type checks. I would strongly advise against simply using `dynamic` to shorten a line of code. – Zer0 Mar 15 '20 at 00:49
  • 3
    @Nidere using `dynamic` is about the _worst_ thing you could do. – maccettura Mar 15 '20 at 00:53
  • 2
    @Nidere i think you need this reinforced, you are solving a non-issue by creating a boat load more problems in the future (what will likely need refactoring out an reversed) by using dynamic, and you would likely fail any sane code review. please take peoples advice – TheGeneral Mar 15 '20 at 01:33