Conventions when using auxiliary cast functions

I recently started using features to make it easier to put fingers, for example, I had something like this

((Dictionary<string,string>)value).Add(foo);

and converted it to a tiny helper function so that I can do it

ToDictionary(value).Add(foo);

Is it against coding standards?

Also, what about simple examples? For example, in my scripting engine, I thought of doing something like this

((StringVariable)arg).Value="foo";

to be

ToStringVar(arg).Value="foo";

I just don’t like how to assign a value in order and instantly get a property, you have to enclose it in double brackets. I feel that the latter is much worse than the first, although

+3
source share
3 answers

, - - " ", using, .

:

using ShorterType = Dictionary<string, Dictionary<int, List<Dictionary<OtherType, ThisIsRidiculous>>>>;
+4

. - , , . Glib ( C) , . , .

+1

, , . , , , , (Java) (++) generics/templates. , .

Without additional context, it’s hard to say about the example you included. There are, of course, situations where the type of caste that you describe is inevitable; but they are the exception rather than the rule. For example, the type of casting (and related helper functions / macros) that you describe is extremely common in C shared libraries.

0
source

Source: https://habr.com/ru/post/1745839/


All Articles