P
Prefers Golfing
We are wrapping web ui controls in our own classes. Is there a downside
(performance/memory management) to making a class static?
(performance/memory management) to making a class static?
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.
(performance/memory management) to making a class static?
Bob Powell said:All classes should be static. So should all methods. We should
systematically expose fields as public and do away with object oriented
programming too.
Before taking up C#, did you write Visual Basic by any chance?
But seriously..
A static or utility class will have no instance data. Each method must be
self contained. This is to say that the method will perform some instant
task, with or without parameters and possibly return some result without
maintaining any state.
A classic utility class is System.Math.
But while structs have
Bill Woodruff said:Bob Powell [MVP] wrote :
"A static or utility class will have no instance data. Each method must be
self contained. This is to say that the method will perform some instant
task, with or without parameters and possibly return some result without
maintaining any state.
A classic utility class is System.Math."
i have been using static classes in certain circumstances (in WinForms
apps)
:
1. function repository model : there is a thematically related set of
functions which several independent classes (or winforms) may need to
call.
this usage i consider very consistent with the way .NET itself packages
certain function libraries as static.
2. global state container model : there are pointers to objects (like
winforms), or constant values, which several different independent classes
or winforms need access to. this usage i waver on in the sense that i feel
in many circumstances techniques like injection of a form or object
reference, or writing formal Properties with tight control over set/get is
better.
Bill Woodruff said:The hueristic in my head goes something like : "if there's one and one only
... consider 'static'

I'm still baffled by my inability to use an array of
structs in a static class as I expected; and that's one (among many) reasons
why I am going to buy Skeet's book tomorrowBut while structs have
"bitten" me, my experience using static classes has been sweet.

I've found that the "extra discipline" required to write static classes is
beneficial to me, though I can't justify that statement with computer
science theory. And I have not considered the issues of using static classes
with multi-threaded apps.
I put these thoughts out with the goal of learning from folks like you, and
J Skeet, and H. Wagner, P. Duniho, and N. Paladino, and everyone else, how
"out of whack" my thinking is![]()

Jon said:Consider it - but also consider that even if there's only one
*production* implementation it may be useful to encapsulate the
behaviour in an interface and mock it for *test* purposes. I often end
up with interfaces like that - it may look pointless when there's only
one class in source control which implements each interface, but
introduce Rhino.Mocks into the picture and things change very quickly
![]()

Of course that's not a Singletonpattern that enforces one-and-one-only of some object, and
"Singleton" as a method of having a single-instance-at-a-time-running
in the context of WinForms apps ?
Interfaces, as contracts that enforce, appeal to me but I keep
sometimes wondering why there isn't an Interface "flavour" that
specifies the scoping of what it requires you code : i.e., that says,
in effect : "when you inherit from this Interface, you must implement
this as static, this as public, and this as private readonly." From
an "enforcement" point of view I like that, but realize it's really
not what Interfaces as we have them now are.
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.