Click here to Skip to main content
15,921,622 members
Home / Discussions / C#
   

C#

 
Generalneed an opinion.. Pin
OMalleyW12-Jul-04 9:02
OMalleyW12-Jul-04 9:02 
GeneralRe: need an opinion.. Pin
Nick Parker12-Jul-04 9:11
protectorNick Parker12-Jul-04 9:11 
GeneralRe: need an opinion.. Pin
OMalleyW12-Jul-04 9:35
OMalleyW12-Jul-04 9:35 
GeneralRe: need an opinion.. Pin
Heath Stewart12-Jul-04 9:41
protectorHeath Stewart12-Jul-04 9:41 
GeneralRe: need an opinion.. Pin
OMalleyW12-Jul-04 10:55
OMalleyW12-Jul-04 10:55 
Generalplaying a wav file :- directx/c# Pin
Anonymous12-Jul-04 8:57
Anonymous12-Jul-04 8:57 
GeneralClass or Struct Pin
Gomac12-Jul-04 7:34
Gomac12-Jul-04 7:34 
GeneralRe: Class or Struct Pin
Heath Stewart12-Jul-04 9:31
protectorHeath Stewart12-Jul-04 9:31 
How many properties you have and whether or not you override ToString should not be a deciding factory for using a struct or a class.

Here are some of the factors:
  • Will it be short-lived? (y: struct, n: class)
  • Must the item be instantiated quickly (y: struct (alloc'd on the stack), n: class (alloc'd on the heap))
  • Will a lot of boxing/unboxing be required? (y: class, n: struct - which you obviously already know)
  • Are small in size (.NET Framework recommends 16 bytes) (y: struct, n: class)
This is by no means a comprehensive list, but it does contain the things you should consider. Both classes and struct can define fields, properties, and methods, and both can implement interfaces. Classes can extend another class and define events. Both can also contain nested types.

If you feel you'll incur a big performance penalty from boxing and unboxing, then use a class. But if your structs are seldomly accessed as struct (i.e., you read in a bunch of structs once and don't deal with them again in your ListView or whatever), then I wouldn't worry about it.

If you want to take advantage of the fact that structs can be destroyed and memory on the stack can be reused ASAP, I would use the disposable pattern and implement IDisposable on a class. While instantiate does take only slightly longer (negligible in most cases), it probably won't affect you any.

So, here are some of the things you have to ask yourself. You know the scenario so you're really the only one that can answer them. More information is needed in order for anyone that doesn't already understand your project to draw any decent conclusion.

 

Microsoft MVP, Visual C#
My Articles
QuestionScroll a Treeview in Code? Pin
selden0312-Jul-04 6:29
selden0312-Jul-04 6:29 
AnswerRe: Scroll a Treeview in Code? Pin
Jay Shankar12-Jul-04 20:54
Jay Shankar12-Jul-04 20:54 
QuestionConstructing a setup project with variable paths not working??? Pin
LongRange.Shooter12-Jul-04 5:13
LongRange.Shooter12-Jul-04 5:13 
QuestionCan not catch KeyDown message from RETURN key Pin
Roman Muntyanu12-Jul-04 3:49
Roman Muntyanu12-Jul-04 3:49 
AnswerRe: Can not catch KeyDown message from RETURN key Pin
Peter Vertes12-Jul-04 4:22
Peter Vertes12-Jul-04 4:22 
GeneralRe: Can not catch KeyDown message from RETURN key Pin
Nick Parker12-Jul-04 4:56
protectorNick Parker12-Jul-04 4:56 
GeneralRe: Can not catch KeyDown message from RETURN key Pin
Roman Muntyanu12-Jul-04 6:28
Roman Muntyanu12-Jul-04 6:28 
GeneralRe: Can not catch KeyDown message from RETURN key Pin
Mike Dimmick12-Jul-04 12:51
Mike Dimmick12-Jul-04 12:51 
GeneralRe: Can not catch KeyDown message from RETURN key Pin
Roman Muntyanu12-Jul-04 14:31
Roman Muntyanu12-Jul-04 14:31 
GeneralRe: Can not catch KeyDown message from RETURN key Pin
Roman Muntyanu14-Jul-04 1:55
Roman Muntyanu14-Jul-04 1:55 
Generalblock an IP Pin
chuchichäschtli12-Jul-04 2:28
chuchichäschtli12-Jul-04 2:28 
GeneralRe: block an IP Pin
Heath Stewart12-Jul-04 3:04
protectorHeath Stewart12-Jul-04 3:04 
Generalrequest socket Pin
shahrokh nabavi12-Jul-04 1:31
shahrokh nabavi12-Jul-04 1:31 
GeneralRe: request socket Pin
Heath Stewart12-Jul-04 4:07
protectorHeath Stewart12-Jul-04 4:07 
Generalrequest socket Pin
shahrokh nabavi12-Jul-04 1:29
shahrokh nabavi12-Jul-04 1:29 
GeneralA serious bug of list view in compact framework Pin
ting66812-Jul-04 0:18
ting66812-Jul-04 0:18 
GeneralRe: A serious bug of list view in compact framework Pin
Mike Dimmick12-Jul-04 12:43
Mike Dimmick12-Jul-04 12:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.