HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Is the use of Struct or Class personal preference?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thestructpreferenceuseclasspersonal

Problem

I have a solution in which I instantiate a class with various types inside of it. I feel it is becoming to cumbersome to instantiate and then create variables for each of the types I want to access in the class (Option 2 in the code), so I've come up with a custom return type so I only have to return data once for use (option 1).

So with the struct my code is more readible in the MAIN block, but is there a better way to do this?

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace retclass
{
class Program
{
static void Main(string[] args)
{

//Option 1 Struct and function
//create struct
ret x = new ret();
x = goofie(3, "this is the struct string");
Console.WriteLine("The String is {0}, the int is {1} and the value is {2}", x.str, x.num, x.yesno);

//Option 2 Class
//Instantiate retclass
retclass retins = new retclass();
retins.num = 3;
retins.str = "this is the class string";
if ((retins.num > 0) && retins.str != "")
{
retins.yesno = true;
}
Console.WriteLine("The String is {0}, the int is {1} and the value is {2}", retins.str, retins.num, retins.yesno);
Console.ReadLine();

}

public struct ret
{
public int num { get; set; }
public string str { get; set; }
public bool yesno { get; set; }
}

public static ret goofie(int isn, string ss)
{
ret dex = new ret();
dex.str = ss;
dex.num = isn;
if ((dex.str != "") && (dex.num > 0))
{
dex.yesno = true;
}
return dex;
}
}

public class retclass
{
public int num { get; set; }
public string str { get; set; }
public bool yesno { g

Solution

This MSDN Page states:


CONSIDER defining a struct instead of a class



  • if instances of the type are small and commonly short-lived or are commonly embedded in other objects.





AVOID defining a struct unless the type has all of the following characteristics:



  • It logically represents a single value, similar to primitive types (int, double, etc.).



  • It has an instance size under 16 bytes.



  • It is immutable.



  • It will not have to be boxed frequently.




So if you want to use a class instead of a structure, you can just add the same static goofy method, to get the same style like you get with the structure:

public static retclass goofie(int isn, string ss)
    {
        retclass dex = new retclass();
        dex.str = ss;
        dex.num = isn;
        if ((dex.str != "") && (dex.num > 0))
        {
            dex.yesno = true;
        }
        return dex;
    }


You can either add this to the Main or better inside the class itself. If you consider the second, you can call it like:

retclass x = retclass.goofie(3, "this is the reclass string");

Console.WriteLine("The String is {0}, the int is {1} and the value 
      is {2}", x.str, x.num, x.yesno);

Code Snippets

public static retclass goofie(int isn, string ss)
    {
        retclass dex = new retclass();
        dex.str = ss;
        dex.num = isn;
        if ((dex.str != "") && (dex.num > 0))
        {
            dex.yesno = true;
        }
        return dex;
    }
retclass x = retclass.goofie(3, "this is the reclass string");

Console.WriteLine("The String is {0}, the int is {1} and the value 
      is {2}", x.str, x.num, x.yesno);

Context

StackExchange Code Review Q#30872, answer score: 2

Revisions (0)

No revisions yet.