473,659 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another novice question

I aksed a few days ago about static methods and got some good answers that
were really useful.

Now I am not sure if about the use of static on members (variables) and
classes.

Can someone please give me some advice about static classes and members too?

Thanks

Doug
Sep 3 '06 #1
25 1519
Gordon,

Just as rule for yourself, if you *can* avoid them, than avoid them.

They keep forever the memory occupied and therefore you should use them in
my opinion there were you are sure that you needs them endless times and
don't need more instances of them.

(Be aware that there is other behaviour for ASPNET applications than for
WindowForms applications about static, just because a windowforms is a
client while an ASPNET application is a middle tier)

However (not the later sentence), just my idea.

Cor

"gordon" <go**********@o ptusnet.com.aus chreef in bericht
news:44******** **************@ news.optusnet.c om.au...
>I aksed a few days ago about static methods and got some good answers that
were really useful.

Now I am not sure if about the use of static on members (variables) and
classes.

Can someone please give me some advice about static classes and members
too?

Thanks

Doug

Sep 3 '06 #2
I'm not sure I'd put it quite as strongly as Cor... static members do
have their place... but if you have static fields [fields=class level
variables] on a class, then the biggest thing you need to consider is
thread safety - not a trivial subject.

Simple static "helper" functions that don't talk to static fields are
intrinsicly thread-safe, but as soon as fields are involved you need to
be aware that in a non-trivial app different threads may be using the
static class at once, so the impact needs to be understood...

Of course, if you *know* you aren't going to be using threading, then
OK ;-p

Jon Skeet has a very good article on threading - note it goes into many
pages...
http://www.yoda.arachsys.com/csharp/threads/

Marc

Sep 3 '06 #3
This doesn't make any sense. A static method doesn't keep any memory
occupied beyond the stack that is allocated when the call is made.

However, when the method exits, the memory for the stack is gone as
well.

Static fields are a different story, and yes, they will last as long as
the application domain lasts. However, saying that you shouldn't use them
is a premature optimization, since in reality, they might be pointing to an
object, and the size of the reference is not high. Even if the object is
big, instance members can reference large objects, and live for the life of
an app domain as well, depending on the use.

Also, ASP.NET does not treat static fields any differently. Static
fields live for the life of the app domain, no more, no less. Granted,
ASP.NET recylces app domains, and one has to be aware of that, but static
fields have no say in that, it is the runtime of ASP.NET that determines
that.

In the end, the OP asked about static methods, not static fields.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:ev******** ******@TK2MSFTN GP04.phx.gbl...
Gordon,

Just as rule for yourself, if you *can* avoid them, than avoid them.

They keep forever the memory occupied and therefore you should use them in
my opinion there were you are sure that you needs them endless times and
don't need more instances of them.

(Be aware that there is other behaviour for ASPNET applications than for
WindowForms applications about static, just because a windowforms is a
client while an ASPNET application is a middle tier)

However (not the later sentence), just my idea.

Cor

"gordon" <go**********@o ptusnet.com.aus chreef in bericht
news:44******** **************@ news.optusnet.c om.au...
>>I aksed a few days ago about static methods and got some good answers that
were really useful.

Now I am not sure if about the use of static on members (variables) and
classes.

Can someone please give me some advice about static classes and members
too?

Thanks

Doug


Sep 5 '06 #4
Did I write what you wrote?
>
Also, ASP.NET does not treat static fields any differently. Static
fields live for the life of the app domain, no more, no less. Granted,
ASP.NET recylces app domains, and one has to be aware of that, but static
fields have no say in that, it is the runtime of ASP.NET that determines
that.
The behaviour of a windowforms program is different than an ASPNET
application, just because in a windowform program the client is the
application and in a aspnet application that it the genereated
HTML/Javascript page. Therefore the complete application behaves different.
A static method in a ASPNET application acts for the rest the same as in any
other C# application.

Static means fixed. In old non by the OS relocatable programs (C) it means
true fixed to a memory address. In more modern OS it means fixed to the
memory starting point where the program is located. In my idea can there
never been an other description for that otherwise the Static keyword is in
my idea wrong used.

However just my thought,

Cor
Sep 5 '06 #5
Cor,

Again, you are making a premature optimization. If the design calls for
a static member, then you should use a static member. An extra field
reference isn't going to bring your app tumbling down.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:Ok******** ******@TK2MSFTN GP05.phx.gbl...
Did I write what you wrote?
>>
Also, ASP.NET does not treat static fields any differently. Static
fields live for the life of the app domain, no more, no less. Granted,
ASP.NET recylces app domains, and one has to be aware of that, but static
fields have no say in that, it is the runtime of ASP.NET that determines
that.
The behaviour of a windowforms program is different than an ASPNET
application, just because in a windowform program the client is the
application and in a aspnet application that it the genereated
HTML/Javascript page. Therefore the complete application behaves
different. A static method in a ASPNET application acts for the rest the
same as in any other C# application.

Static means fixed. In old non by the OS relocatable programs (C) it means
true fixed to a memory address. In more modern OS it means fixed to the
memory starting point where the program is located. In my idea can there
never been an other description for that otherwise the Static keyword is
in my idea wrong used.

However just my thought,

Cor

Sep 6 '06 #6
Nick,

It is great, but where is the intention from your current message different
from the first line in the one I started with?

Yours
Again, you are making a premature optimization. If the design calls
for a static member, then you should use a static member. An extra field
reference isn't going to bring your app tumbling down.
Mine
>>Just as rule for yourself, if you *can* avoid them, than avoid them.
I did nowhere write that you *should* not use them.

Cor
Sep 6 '06 #7
Cor Ligthert [MVP] <no************ @planet.nlwrote :
It is great, but where is the intention from your current message different
from the first line in the one I started with?

Yours
Again, you are making a premature optimization. If the design calls
for a static member, then you should use a static member. An extra field
reference isn't going to bring your app tumbling down.

Mine
>Just as rule for yourself, if you *can* avoid them, than avoid them.

I did nowhere write that you *should* not use them.
But you're suggesting that you should avoid them where possible - in
other words, that it's worth skewing the design in order to avoid them.
I, like Nicholas, disagree with that. If a static member is the natural
design, there's no need to avoid it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 6 '06 #8
Jon,

Where is a static class or a whatever class a natural design.

I don't know if you believe in the great Englisman Darwin, otherwise have a
look in the Bibble both is about natural design. A static class is never for
me a Natural design as is not any class.

Both you are writing "an extra field reference isn't going to bring your app
tubling down"

I cannot place the context of this message, is this something Nick has
stated in this thread.

You should know that I would be the last one who would write that, therefore
we have had to much discussions about by instance avoiding boxing. From
which I write forever that you should try to avoid that but not put to much
effort in that.

So I am curious in what context you place that sentence?

Cor

"Jon Skeet [C# MVP]" <sk***@pobox.co mschreef in bericht
news:MP******** *************** *@msnews.micros oft.com...
Cor Ligthert [MVP] <no************ @planet.nlwrote :
>It is great, but where is the intention from your current message
different
from the first line in the one I started with?

Yours
Again, you are making a premature optimization. If the design calls
for a static member, then you should use a static member. An extra
field
reference isn't going to bring your app tumbling down.

Mine
>>Just as rule for yourself, if you *can* avoid them, than avoid them.

I did nowhere write that you *should* not use them.

But you're suggesting that you should avoid them where possible - in
other words, that it's worth skewing the design in order to avoid them.
I, like Nicholas, disagree with that. If a static member is the natural
design, there's no need to avoid it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 7 '06 #9
Cor Ligthert [MVP] <no************ @planet.nlwrote :
Where is a static class or a whatever class a natural design.
Where utility methods are required, for starters. Besides, you didn't
originaly limit yourself to static classes, but to all static
variables.

At that point, you're preventing patterns such as the singleton pattern
(which does have disadvantages, but is appropriate sometimes).
I don't know if you believe in the great Englisman Darwin, otherwise have a
look in the Bibble both is about natural design. A static class is never for
me a Natural design as is not any class.
What would you do with HttpUtility then, just as an example? All its
members are static, so it effectively could (and should) be a static
class. The fact that it's got a public constructor is a mistake,
basically.

How about the System.Math class?
Both you are writing "an extra field reference isn't going to bring your app
tubling down"
Actually, I never wrote that.
I cannot place the context of this message, is this something Nick has
stated in this thread.

You should know that I would be the last one who would write that, therefore
we have had to much discussions about by instance avoiding boxing. From
which I write forever that you should try to avoid that but not put to much
effort in that.
Again though - where the design naturally uses boxing, it's fine to use
boxing.
So I am curious in what context you place that sentence?
You said to avoid static members where you *can* avoid them. You almost
always *can* avoid them - which means that by your standards, they
should virtually never be used.

Here's an example. Consider the Encoding class and its ASCII property.
That *could* create a new instance of ASCIIEncoding each time, instead
of caching it in a static variable. So one *can* avoid using a static
variable - and according to your logic, one therefore *should* avoid
it. Now, how is creating an instance of ASCIIEncoding every time
Encoding.ASCII is used actually a *good* idea? Do you like having lots
of instances of classes which effectively have no state?

Now, if you didn't actually mean your statement to be as strong as it
was, you shouldn't have made it that way. I can only go by what you've
actually written...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2365
by: Marian | last post by:
Hi, I am totaly novice in .NET and I am studying a book about this. There was mentioned "assembly". I did not understand, how function does it has . I would like to know the exact run of code (intermediate language and so on). Is there any page on internet, which makes me clear? Thanx
39
6521
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
188
7171
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection - non-deterministic destructors - Objects can't exist on the stack - Type / Reference Types
1
2034
by: John A. Bailo | last post by:
Excuse the novice aspects of this question, but: What techniques are available to me for launching one c# application ( console .exe) from another? For example, I know there is the Process and ProcessInfo classes, but these seem designed for launching non-CLR applications. Should I access the .exe as a reference?
3
5292
by: con.brady | last post by:
Hi Folks, Disclaimer: I apologize if this is the wrong place for this type of question, but if so, I don't know where the right place is and I'd be very grateful if someone could point me in the right direction. Question:
1
2996
by: TwistedSpanner | last post by:
Hello all, For the record I am a complete java novice. I have to write a program to generate/output to screen 10 simple maths question and output a final score . The question is as follows Random number, Random operator (+ or -) random number. I have written the program but cannot make the random operator work. Can anyone help? My code.
9
3048
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped. Below you will find the code I've written and the error that results. I'm hoping that someone can give me some direction as to what syntax or parameter is missing from the code that is expected by VBA. Overview: I'm trying to copy calculated...
2
1602
by: Dorish3 | last post by:
I want to apologize ahead of time for being a novice with MS Access and VBA. I desperately need help with 2 queries that I am trying to put together. I want to thank anyone that can help me out with this situation. I want to put a select query(Query1) that uses one table and the criteria would be . When an event number is entered it will give me the Event Number, Event Name, Event Date, Event time, and House Size. Next, I need to...
4
2039
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice with MS Access and VBA. I desperately need help with 2 queries that I am trying to put together. I want to thank anyone that can help me out with this situation. I want to put a select query(Query1) that uses one table and the criteria would...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.