Structs in C#

  • Thread starter Thread starter Mash
  • Start date Start date
M

Mash

I have the following dilemna. How do I return an instance of a struct
from a method.

For example:

private struct Me
{
private string name;

public string Name
{
get{return name;}
set{name=value;}
}
}

public Me GetMe()
{
Me mash = new Me();
mash.Name="Mash";
return mash;
}

Doing this gets a CS0050 error..accessibilty!!

If I do the following:

public object GetMe()
{
Me mash = new Me();
mash.Name="Mash";
return (object)mash;
}

All is well...I then have to "un-Box" mash when I want to use it.

Questions are:

Does this give me any benefit over using a class?
Does the struct now reside on the HEAP or STACK.
If I change the struct to public it all works. Is this good practice?
Does the struct instance, mash, retain it's private accessibility when
I have boxed/un-boxed?
What is the accepted practice for passing structs between classes?

Thanks for any help.

Mash
 
Mash said:
I have the following dilemna. How do I return an instance of a struct
from a method.

For example:

private struct Me
{
private string name;

public string Name
{
get{return name;}
set{name=value;}
}
}

public Me GetMe()
{
Me mash = new Me();
mash.Name="Mash";
return mash;
}

Doing this gets a CS0050 error..accessibilty!!

This is because your struct is private. Just make it public!
If I do the following:

public object GetMe()
{
Me mash = new Me();
mash.Name="Mash";
return (object)mash;
}

All is well...I then have to "un-Box" mash when I want to use it.

Questions are:

Does this give me any benefit over using a class?

Allocation will be faster. Array allocation will be much faster.
Does the struct now reside on the HEAP or STACK.

Depends.
If the struct is a local variable, it will be allocated on the stack.
It if is a member of an object, it will be allocated on the heap (as part of
the object).
If I change the struct to public it all works. Is this good practice?
Yes, you can still control the visibility of individual members.
Does the struct instance, mash, retain it's private accessibility when
I have boxed/un-boxed?
?

What is the accepted practice for passing structs between classes?

Structs are good for simple things that have value semantics. IMO, they
should always be immutable (like System.IntXx, System.DateTime, etc.) but
some people think otherwise.
I don't know of any special "good practice" about "passing" structs.
 
Mash said:
I have the following dilemna. How do I return an instance of a struct
from a method.

For example:

private struct Me
{
private string name;

public string Name
{
get{return name;}
set{name=value;}
}
}

public Me GetMe()
{
Me mash = new Me();
mash.Name="Mash";
return mash;
}

Doing this gets a CS0050 error..accessibilty!!

If I do the following:

public object GetMe()
{
Me mash = new Me();
mash.Name="Mash";
return (object)mash;
}

All is well...I then have to "un-Box" mash when I want to use it.

Questions are:

Does this give me any benefit over using a class?
Does the struct now reside on the HEAP or STACK.

The struct resides on the stack. But all you have on the stack is a struct
with a single field. That field is a reference to a String, which resides
on the heap. In short there's usually no reason to use a struct unless all
of its fields are value types. No strings, no Arrays, just Int32's and
such.

David
 
This is because your struct is private. Just make it public!

A private struct is fine as long as it is located in the same class as the methods accessing it and should not cause any errors. Having a private member variable is also fine as long as it isn't accessed directly. Here it is accessed through a public property.
 
Back
Top