M
Mark
are there functions in c# for just members of a class
myclass.Method/function
Thanks
Mark
myclass.Method/function
Thanks
Mark
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
are there functions in c# for just members of a class
myclass.Method/function
Bob Powell said:Global methods or functions are mot possible. They must all be members of
a class even if that class is static.
FYI This sort of programming methodology is incredibly bad practice.
Static functions should be avoided at all cost or at least kept to nothing
more complex than the program entry point.
Ben Voigt said:Baloney. This whole idea that C# and Java have that log10, sin, etc, have
to be members of a Math class is ludicrous and adds nothing to readability
or maintainability. Insisting on having an object instance to call such
functions to "avoid static functions at all cost" is insane.
Jon Skeet said:I would say that forcing methods to be part of classes is good, but
avoiding static methods is a bad idea. So I'm against *global* methods,
but have no problem with *static* methods (where appropriate, of
course).
Ben Voigt said:For reasons of dealing with name collisions, you usually want such functions
to be namespace members and not global. But you absolutely should be able
to import the entire group en masse with "using namespace ..." or the
equivalent, and then be able to use undecorated names. Perhaps the answer
is to unify namespaces and classes, providing a using directive that imports
static members of a class into scope.
But there ought to be some way to
call functions without explicitly specifying the scope. Currently, C# only
lets you have this friendly use of static members of a single class, and
that uses up your single inheritance, and doesn't work with sealed classes
(including static classes).
For reasons of dealing with name collisions, you usually want such functions
to be namespace members and not global. But you absolutely should be able
to import the entire group en masse with "using namespace ..." or the
equivalent, and then be able to use undecorated names. Perhaps the answer
is to unify namespaces and classes, providing a using directive that imports
static members of a class into scope. But there ought to be some way to
call functions without explicitly specifying the scope.
Example in other languages.....
there could be a function declaration
Function int myfunc()
///do somthing
return
vb6
private function myfunc()
'// do somthing
myfunc=true
end function
is there functions like the above that do not belong as member in a class
Or
is c# Class Driven only
public myclass
{
public int myfunc()
{
// do somthing
return 0}
}
// main app
myclass = new myclass().myfunc()
Bruce Wood said:But with any feature like this, I have to ask, what does it gain for
me, in return for making the compiler / language more complicated.
In this case, my answer is, "Practically nothing." Perhaps it's
because I'm a touch typist, but being able to type Sin instead of
Math.Sin seems to me a finesse, a minor touch. To re-jig the whole way
that names work in C# to accommodate seems, IMHO, to be akin to
killing a fly with a shotgun.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.