473,499 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class or struct?

Hi,
I want to create a separate container for a bunch of constants used in
different classes, like:

internal class Constants
{
private Constants(){}

public const string CONST1 = "1";
.....
}

So, what's better for this container, to be class or struct? Why?

Sunny
Nov 16 '05 #1
5 1191

I may be wrong but when you reference a static (const in this case) variable
an instance of the class object in which it resides is not created, not sure
if this is the case with struct being a value type...
If it is then doesn't matter. If it isn't, go with the class.

"Sunny" <su***@newsgroups.nospam> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
Hi,
I want to create a separate container for a bunch of constants used in
different classes, like:

internal class Constants
{
private Constants(){}

public const string CONST1 = "1";
.....
}

So, what's better for this container, to be class or struct? Why?

Sunny

Nov 16 '05 #2
It really doesn't matter, because the compiler substitutes constants
with their actual values when compiled to IL. One thing that might
irritate you is that you can't prevent construction of a struct i.e you
can't define a private default constructor. So if Constants is a
struct, you will be able to write code like new Constants().CONST1. The
compiler will then report an error that you can access CONST1 only
using the type name. So, I'd suggest going with a class, it atleast
avoids the above possibility.

Regards
Senthil

Nov 16 '05 #3
In article <11**********************@z14g2000cwz.googlegroups .com>,
se**********@wdevs.com says...
It really doesn't matter, because the compiler substitutes constants
with their actual values when compiled to IL. One thing that might
irritate you is that you can't prevent construction of a struct i.e you
can't define a private default constructor. So if Constants is a
struct, you will be able to write code like new Constants().CONST1. The
compiler will then report an error that you can access CONST1 only
using the type name. So, I'd suggest going with a class, it atleast
avoids the above possibility.

Regards
Senthil


Thanks Dan and Senthil.

I was wondering if the compiler is substituting during the compile time.
If this is so, then class is better approach.

Do you have any links where it is explained?

Sunny
Nov 16 '05 #4
Have a look at this link.

http://msdn.microsoft.com/msdnmag/is...04/NETMatters/

Nov 16 '05 #5
In article <11*********************@f14g2000cwb.googlegroups. com>,
se**********@wdevs.com says...
Have a look at this link.

http://msdn.microsoft.com/msdnmag/is...04/NETMatters/


yep, now I'm completely enlightened. Thanks

Sunny
Nov 16 '05 #6

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

Similar topics

2
11219
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
4
31560
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
2
634
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
2
2292
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
2
2650
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class...
0
7171
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
5468
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,...
1
4918
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...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
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...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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 ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.