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

Sealed class with constant.

Hi all,

I have created one sealed class having one constant.

Can i access that constant of sealed class without creating object of
that class. If yes will internally new object gets created. Or will it
behave like static member which remains in memory throughotut
applications' lifetime.

Please correct me if i am wrong.

thanks in advance.

Jan 9 '07 #1
3 2081
archana wrote:
Hi all,

I have created one sealed class having one constant.

Can i access that constant of sealed class without creating object of
that class. If yes will internally new object gets created. Or will it
behave like static member which remains in memory throughotut
applications' lifetime.

Please correct me if i am wrong.

thanks in advance.
In C#, Constants are accessed as if they were static fields, although
they cannot use the static keyword. So I believe you should access that
constant of sealed class without creating object of that class.
Jan 9 '07 #2
archana wrote:
I have created one sealed class having one constant.

Can i access that constant of sealed class without creating object of
that class.
Yes - constants are inherently static.
If yes will internally new object gets created. Or will it
behave like static member which remains in memory throughotut
applications' lifetime.
It effectively *is* a static member, so no objects will be created.
I seem to recall that there are some subtle differences between a
constant and a normal static member - but in particular the constant
value will be "baked into" any assembly referencing it, so changing the
constant's value in source code requires both the assembly containing
it *and any referencing assemblies* to be recompiled.

Jon

Jan 9 '07 #3
archana wrote:
I have created one sealed class having one constant.

Can i access that constant of sealed class without creating object of
that class. If yes will internally new object gets created. Or will it
behave like static member which remains in memory throughotut
applications' lifetime.
If the constant is public, then you can use it from other classes.

Referring to a constant *looks like* referring to a static field, but
the compiled code is different. Referring to a static field reads the
current field value from memory; every reference compiles to object
code that contains a memory address. (If the field changes, the value
you read changes.) Referring to a constant inlines the constant value
in the object code; it's just like using a literal value in your code.

Iow, referring to a constant will not create an instance of the class
(whether sealed or not) and will not allocate storage in the same way
that referring to a static field will.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
Jan 9 '07 #4

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

Similar topics

6
by: Julie | last post by:
What would be the primary motivation to make a class 'sealed' (meaning that you can't derive from it) in C++? (I understand that there is currently no sealed keyword in C++, but that there are...
5
by: Bharat Karia | last post by:
Hi, Is it possible to writed Sealed classes in C++ . i.e. there is no sealed/final keyword in C++, but is it possible to achieve the same effect? i.e. deriving from a sealed class is an error...
14
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
13
by: Mark Rae | last post by:
Hi, Since sealed classes can't be instantiated with the new keyword e.g. CClass objClass = new CClass(), does this mean that they don't have constructors / deconstructors or, if they do, that...
9
by: Kylin | last post by:
any better reason ? -- FireCrow Studio Kylin Garden EMail:gaotianpu@gmail.com ICQ:156134382
8
by: shawnz | last post by:
Is there any way to either derive a sealed class, or access private members of a sealed class? Either that, or is there a way to run internal methods in a sealed class outside the current assembly?...
18
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent...
7
by: Hilton | last post by:
Jon Skeet wrote: I for one wish the "sealed" keyword on a class didn't exist. Why can I not have: class NamedThread : Thread { string name; :
7
by: Zytan | last post by:
I know you cannot have a sealed static class, but why not? Why must static classes be left open to inheritance? This article: http://msdn.microsoft.com/msdnmag/issues/03/07/NET/ recommends to...
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:
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?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.