473,320 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Declaring with static

Ant
Hi
I'm quite new to C#. I've been playing around with variables declared
outside the scope of a method with & without the static keyword. What
difference does declaring variables in either fashion make? For that matter,
why must you declare methods with the static keyword?

Thanks for any ideas
Ant
Nov 17 '05 #1
4 1557

"Ant" <An*@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
Hi
I'm quite new to C#. I've been playing around with variables declared
outside the scope of a method with & without the static keyword. What
difference does declaring variables in either fashion make? For that
matter,
why must you declare methods with the static keyword?

Thanks for any ideas
Ant


I'll get this started with what will probably be an inadequate explanation.

A static datamember of a class (declared as you said, outside a method's
code block), exists from the time the class definition is available to your
application throughout its life, and only one instance of the static field
is created no matter how many objects of the class are created. The static
datamember is independent of objects created. Contrast this behavior with
that of instance datamembers (declared without the static keyword) which
exist only in the context of an object created from the class. Each object
gets its own copy of each instance datamember. Zero instances of instance
datamembers exist until objects are created.
Static methods are normally created to manipulate static datamenbers in
situations where there are no objects of the class available, because
instance methods, although they can access static datamembers, cannot be
used unless you have an object to qualify the call.

Knowing the group here, you'll get better explanations, so hang in there.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Nov 17 '05 #2
Hi Ant,

Static member variables exist at class scope, while non-static
variables exist at instance scope. What this means is that if you
have declared:

class Ant
{
static int x = 0;
}

then all object instances in the Ant class will be accessing the same
variable x. So:

Ant a;
Ant b;
a.x = 3;

then b.x will be 3. If x were not defined as static, then each
instance has its own copy of x, and b.x would be 0 (and a.x would still
be 3). This is used to allow all instances of a class to share some
information. Think of them as class-wide global variables.

As for static on methods, you put them on methods that you want to
be able to call without an instance of that class. For example, if I
declare

class Ant
{
public static void Hello1() {...}
public void Hello2() {...}
}

then I can call Ant.Hello1(), but not Ant.Hello2(). Hello2()
requires that I have an instance, so Ant ant; ant.Hello2(); would
work, whereas with Hello1() I can invoke the method using the class
name. This is usually used either because one wants a user to be able
to change static variables, hence a static function fits the bill, or
you are grouping some functions into a class based on semantic
similarity, and an instance isn't really necessary. For instance,
String.Format() semantically belongs in the String class, but doesn't
require an instance, so it is static.

Hope this helps,
Harold

Nov 17 '05 #3
<hs****@gmail.com> wrote:
Static member variables exist at class scope, while non-static
variables exist at instance scope. What this means is that if you
have declared:

class Ant
{
static int x = 0;
}

then all object instances in the Ant class will be accessing the same
variable x.
More than that - there don't have to be *any* instances of the Ant
class in order to use static members. Static members are associated
with the type rather than any particular instance of the type.
So:

Ant a;
Ant b;
a.x = 3;

then b.x will be 3.


Fortunately, the above won't compile. C# took the wise decision of
forbidding access to static members via instances.

<snip>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Hi Ant,

The scope of the static members are at class level but the scope of the
reference members are at object level.
Hi
I'm quite new to C#. I've been playing around with variables declared
outside the scope of a method with & without the static keyword. What
difference does declaring variables in either fashion make? For that matter,
why must you declare methods with the static keyword?

Thanks for any ideas
Ant

Nov 17 '05 #5

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

Similar topics

2
by: Brandon | last post by:
In a templated class I wrote, there is a public typedef for a function pointer. The class has an instance of the function pointer as a private member. That pointer is declared as static so I need...
5
by: Generic Usenet Account | last post by:
I am trying to compile the following sample code: class WhatISHappeningHere { static const int x = 32; static const char* yy = "Howdy"; // ... // blah blah blah // ... };
5
by: param | last post by:
Declaring struct as static is creating problem with newer version of CC compiler 5.7 in solaris. e.g. static struct new_str { int a; int b; };
4
by: GnG | last post by:
Hello all, Someone posted a similar question a while ago but there was no response. Does anyone know the answer? I have a managed C++ DLL which is used by a C# project. In that DLL, I have...
3
by: kkkkkillerkkkkk | last post by:
How Can I Declare A -template Static Array In A Template Class- (declaring And Initializing ) Note :the Array Is So Huge . And If There Is Somebody Who Can Help To Make The Huge Array In The...
5
by: Markus Pitha | last post by:
Hello, I have a class "Menu". In this class I declare an object "Controller". Now I have a problem: Controller uses a ctor but I get the value I have to pass later in my program. The only way...
5
by: =?Utf-8?B?RWl0YW4=?= | last post by:
Hello, I am declaring an element like this: public static List<myListElementmyList = new List<myListElement>(); I would like to declare an array of this myList. How would I modify the...
1
by: prisesh26 | last post by:
What is the difference between declaring a member variable as final static and static final? thanks
8
by: bintom | last post by:
What are the differences between the following methods of declaring strings? char string1 = "C++ forum"; char* string2 = "C++ forum"; I know that the first uses the array notation, whereas...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.