473,386 Members | 1,775 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,386 software developers and data experts.

C#, static method, using constructor call

Please consider the following two cases and explain to me the difference in output:

Consider Case 1:

using System;

class MyClass
{
static int s;
static int i;

public MyClass(int a, int b)
{
s = a;
i = b;
}

public static int myMethod()
{
return s * i;
}

}

class MainClass
{
static void Main()
{
MyClass mc1 = new MyClass(1, 1);
MyClass mc2 = new MyClass(2, 2);
Console.WriteLine("The result of mc1 = {0}", MyClass.myMethod());
Console.WriteLine("The result of mc2 = {0}", MyClass.myMethod());
}
}

Case 2:

using System;

class MyClass
{
static int s;
static int i;

public MyClass(int a, int b)
{
s = a;
i = b;
}

public static int myMethod()
{
return s * i;
}

}
Aug 1 '07 #1
1 1064
vanc
211 Expert 100+
The point is static variable is unique, and it doesn't need to be instantiated. So whenever you assign a value to a static variable, it will retain this value until you assign new value to it.
In your case, you created two objects but the static variables will be overwritten by the last assignment. Thus, it will return 2*2 not the first value.

cheers.
Aug 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Yu | last post by:
I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can...
9
by: A J Le Couteur Bisson | last post by:
Could someone please confirm that static class constructors are only called at the first use of a non-static constructor on the class, or am I doing something wrong? If this is indeed the case...
10
by: Marek | last post by:
Hi, I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003. Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used inside a static constructor? The code...
10
by: Marc Selis | last post by:
Hi, I have a class with a static constructor in which the class registers itself as capable of doing something (using a delegate) The problem is that the static constructor is never called, as...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
5
by: | last post by:
Hi, How long do webservice objects live for? In particular, if i have static variables filled with data from a static constructor in a webservice, how long will that data persist? thxs
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
3
by: David Veeneman | last post by:
Why do these questions always come up on Friday afternoon? I'm starting to use GoF singleton classes in my projects. Right off the bat, I've run into a surprise. I thought that a Singleton could...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
6
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...

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.