473,657 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1574

"Ant" <An*@discussion s.microsoft.com > wrote in message
news:06******** *************** ***********@mic rosoft.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.c om> 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.co m>
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
3992
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 to declare an instance of the pointer. So, I tried using the keyword typename to declare the static member. I don't get any syntax errors but the pointer's name shows up in an "undefined symbol" error at compile time. Have a look at this...
5
2631
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
2226
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
2807
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 code like this: namespace Foo { public __value const int Bar = 123;
3
2227
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 Cursor List Please Help And Give Me The Syntax.
5
1784
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 to solve this is to make a new instance of "Controller" and use it dynamically, but how can I use it as static object? class Menu {
5
1918
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 declaration of myList so it will represents an array? Thanks Eitan
1
1564
by: prisesh26 | last post by:
What is the difference between declaring a member variable as final static and static final? thanks
8
3454
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 the second uses pointer notation. But apart from that what are the implications / dangers, etc. if any.
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
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...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
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
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.