-2

is there really no way in c# of shortening lengthy code like

Dictionary<my.very.long.type, my.other.long.type> myVar;
//code that repetitevely uses those classes

with something like

using acronym = my.very.long.type {
  using acronym2 = my.other.long.type {
     Dictionary<acronym , my.other.long.type> myVar;
     //code that repetitively uses those classes
  }
}

without using a file-wide alias? (using statements at the beginning of the file)

for a verbose language it seems like a must-have feature,
it would widely improve readability on the sections where the focus must be on the operation sequence rather than the types.

My searches have only found no as answer, but i refuse to believe it.

-EDIT- i'll include here some explanation i gave as comments as why i'm looking for this feature:

I like to be explicit and avoid using statements whenever possible. Still, for the sake of readability i'd like to "group them" at the beginning of the interested method or code-block.
Also, a public property should have an explicit understandable name, while the class methods implementations do not, and can use an acronym.
Sure i could define the property as a getter of a private acronym, but that's more code and more runtime overhead for something a compiler could expand for me.

..What i come from is the (javascript) let keyword. A local scoped reference, that could also "hold a class type". As a compiled language C# should be able to avoid declaring a variable at all and just locally alias the name.
I don't remember having this problem in C++ either, maybe cuz it had pointers but it's been a while.

  • 5
    No, there isn't such a thing that I'm aware of. Given how many developers have been using C# for so long - and this is the first time I've seen this feature request - I think it's far from a "must-have" feature. – Jon Skeet Aug 07 '23 at 08:18
  • 3
    What's your objection to file wide aliases? Given the existence of partial classes, the file itself need only contain a single method from the class and if that's still too big a scope, perhaps ask if you're solving the right problem. – Damien_The_Unbeliever Aug 07 '23 at 08:18
  • 1
    Also, if your types have such very long names, perhaps you should think about renaming them (and it *might* also indicate they have too many responsibilities, but that's really just a guess at this point) – Zohar Peled Aug 07 '23 at 08:31
  • 1
    From where did you switch to C#? I am having the feeling you are looking for a way to apply a thing that's used in another language in C# but it's not idiomatic, here. – Fildor Aug 07 '23 at 08:39
  • No, But you should rarely need to declare types more than once if you use `var` and all the other language features. And if file wide aliases is a problem you might want to reduce the size of your classes/files. If you have conflicting type names you can give one or more namespaces a shorter name. If you have overly nested generic types you might want to create non-generic types to break the nesting and simplify naming. – JonasH Aug 07 '23 at 08:55
  • @Damien_The_Unbeliever Jon_Skeet I like to be explicit and avoid using statements whenever possible. Still, for the sake of readability i'd like to "group them" at the beginning of the interested method or code-block. Also, a public property should have an explicit understandable name, while the class methods implementations do not, and can use an acronym. Sure i could define the property as a getter of a private acronym, but that's more code and more runtime overhead for something a compiler could expand for me. – Andrea Bardelli Aug 10 '23 at 08:40
  • @Fildor heh, last language i used is Javascript ("language"), and i cursed it for it's lack of checks (i couldn't use typescript for various reasons). So you see, the transition is rough. From hyper compact to Hyper expanded (can't use too much syntactic sugar as i'm still grasping it's basics). What i miss is the let keyword. A local scoped reference, that could also hold a class. As a compiled language C# should be able to avoid declaring a variable at all and just alias the name. – Andrea Bardelli Aug 10 '23 at 08:57

0 Answers0