Free functions in C #

As a C ++ programmer, every time I work with C #, I wonder why it lacks support for stand-alone functions; in other words: functions that are not part of any class. I really skip this function because autonomous functions allow you to add functionality to classes without requiring full access to them, avoiding hard-to-reach monolithic classes. In addition, it allows you to expand third-party libraries. I know that you can use a static class, but the class name does not matter at all, which makes the client code unnecessarily verbose.

For example. I want to create a helper function to count words in a string. How can I avoid the need to write "StringHelperClass.CountWords ();" in client code? StringHelperClass acts as a namespace, only I can not write "using StringHelperClass;". I am forced to repeat "StringHelperClass" every time I use it, while it is obvious that this is an auxiliary function of the string, since its only parameter is the string.

Is there a way to extend the functionality of the class while keeping the client code concise?

+3
source share
2 answers

, , #. - . , "" . , , OpenGL #, , OpenGL.glClearDepth(1.0f); OpenGL.glEnable(GL_DEPTH_TEST); .. , , , - , , - . , , , #, (, ).

+2

:

" ?"

, # , ? , ( ), .

( OregonGhost ) , IntelliSense ..

: ? ( #), ?

+11

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


All Articles