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

Object creation of class having constant.

Hi all,

I am having suppose say following class
class MyClass
{
public int x;
public int y;
public const int c1 = 5;
public const int c2 = c1 + 5;

public MyClass(int p1, int p2)
{
x = p1;
y = p2;
}
}

here 2 variables c1 and c2 are constant. So those can be access
without creating object of class 'myclass'. So when i create new object
of class 'myclass' will this new object allocate new memory for
constant c1 and c2. of constant are stored at a time of loading
application only?

please correct me if i am wrong.

thanks in advance.

Jan 9 '07 #1
3 1015
No, memory will not be allocated on a per object basis.

archana wrote:
Hi all,

I am having suppose say following class
class MyClass
{
public int x;
public int y;
public const int c1 = 5;
public const int c2 = c1 + 5;

public MyClass(int p1, int p2)
{
x = p1;
y = p2;
}
}

here 2 variables c1 and c2 are constant. So those can be access
without creating object of class 'myclass'. So when i create new object
of class 'myclass' will this new object allocate new memory for
constant c1 and c2. of constant are stored at a time of loading
application only?

please correct me if i am wrong.

thanks in advance.
Jan 9 '07 #2
One of the differences between a "const" and a "static readonly" is
that a "const" will get burnt into the *calling* code, so actually
MyClass.c1 will only be evaluated at *compile time* and will never
reference the class after build. And so it doesn't need to create any
instances. Or even load the assembly (if referenced). More detail
below - however, this is your 3rd (at least) post on this (or related)
subject(s) - why don't you just tell us what you are trying to do?

Using your code and
static void Main()
{
Console.WriteLine(MyClass.c1);
}
compiles to (IL):
..method private hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 8
L_0000: nop
L_0001: ldc.i4.5
L_0002: call void [mscorlib]System.Console::WriteLine(int32)
L_0007: nop
L_0008: ret
}
or reversed back to C#:
private static void Main()
{
Console.WriteLine(5);
}

Marc
Jan 9 '07 #3
Additional to my last post, the consts themselves compile to (IL):
.field public static literal int32 c1 = int32(0x00000005)
.field public static literal int32 c2 = int32(0x0000000A)

I don't know enough about IL to say for sure, but this suggests that
(in addition to the compiler evaluation) they may get treated broadly
along the same rules as static; if this is correct, then static fields
are initialised when first touching the class, so it may consume a
tiny amount of memory when you create the first object, but not
afterwards... but given the volumes - do you have a specific scenario
in mind?

Marc
Jan 9 '07 #4

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

Similar topics

2
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a...
106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
7
by: J-T | last post by:
I can instantiate my object in my *ASP.NET* application in two ways: A) public sealed class RSSingleton { private static ReportingServiceProxy m_RsProxy=null; static RSSingleton() {...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
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...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
14
by: =?GB2312?B?zPC5zw==?= | last post by:
Howdy, I wonder why below does not work. a = object() a.b = 1 # dynamic bind attribute failed... To make it correct, we have to create a new class: class MyClass(object): pass a =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
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...

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.