noob.. no functions in C#?

  • Thread starter Thread starter Glenn
  • Start date Start date
G

Glenn

Searching the "How Do I.." pages.. I don't see anything about
functions like classic C/C++. If I want to do even a one-time
operation, I _have_ to create a class? Seems weird to have to define
an "object" when I will never use it, just would be creating it to run
something one time.
 
Glenn said:
Searching the "How Do I.." pages.. I don't see anything about
functions like classic C/C++. If I want to do even a one-time
operation, I _have_ to create a class? Seems weird to have to define
an "object" when I will never use it, just would be creating it to run
something one time.

You can define a class with a static method. Then you do not
need an object.

Arne
 
You can define a class with a static method. Then you do not
need an object.

Even better, you can define a _static_ class to hold your static
methods! That way you cannot even create an object. :)

That's the closest thing C# has to traditional functions.
 
Glenn said:
Searching the "How Do I.." pages.. I don't see anything about
functions like classic C/C++. If I want to do even a one-time
operation, I _have_ to create a class? Seems weird to have to define
an "object" when I will never use it, just would be creating it to run
something one time.

You don't have to create another class to add a function. You already
have a class where you can put it.

As C# is object oriented, all code is in methods inside classes.
Therefore, the code from where you wish to call the function also is
inside a class.

Then, of course it depends on what the function does. You might want to
create a class for it, and for other similar functions.


Eventhough C++ has object orientation support, it's still a functional
programming language where the ground level is the main program, and you
can use objects from there. In C# there is no ground level like that,
instead the main program is just a method in a class, and the project
knows what method to use to start the program.

It's a bit getting used to, but it's really nice to work in a truly
object oriented language.
 
Searching the "How Do I.." pages.. I don't see anything about
functions like classic C/C++. If I want to do even a one-time
operation, I _have_ to create a class? Seems weird to have to define
an "object" when I will never use it, just would be creating it to run
something one time.

Take a look at the Math class in the .NET Framework. Most of the
functions there will be familiar. They're static: they don't require
you to create an instance in order to use them. The class merely
provides a grouping mechanism.

However, I'll warn you: what you're likely experiencing is a problem
with the paradigm shift. When I moved from C to OOP I, too, thought,
"Is there no way to just make a simple function?" My mistaken
assumption was that I would often need to make simple functions, or,
once I figured out that I could create static methods, that I would
often have need of static methods that weren't associated with any
class. After all, I'd been programming that way for years. How could I
program without writing stand-alone functions?

The truth of it is that you need them far, far less than you think.
Once you get the hang of designing software for OOP, you'll discover
that what you once wrote as stand-alone functions really do belong in
objects. Until you've designed quite a few classes, you can't really
see how to organize things effectively in an OOP language. However, I
predict that once you get the hang of it, you'll almost never need to
write a function that isn't connected to anything else.
 
Eventhough C++ has object orientation support, it's still a functional
programming language

I believe you meant to say "procedural" instead of functional (just trying
to prevent any confusion :-))
 
Lebesgue said:
I believe you meant to say "procedural" instead of functional (just trying
to prevent any confusion :-))

Yes, you are absolutely correct. Thanks.
 
Thanks to everyone for your input. I wasn't able to see the code on
the Math class, however, with the static suggestion, I ended up
finding Static Classes and Static Class Members (C#). There's a good
example in there (go to it through Help/Search).

Thanks again to all for your help :)
Glenn
 

Ask a Question

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.

Ask a Question

Back
Top