3
\$\begingroup\$

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 { get; set; }
    }   

}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ For immutable types its mostly a performance choice depending on the size of the class/struct. For mutable types, go with the class. \$\endgroup\$ Sep 6, 2013 at 12:51

2 Answers 2

2
\$\begingroup\$

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);
\$\endgroup\$
1
  • \$\begingroup\$ Thanks, that does make it a bit more readable. I like the way it is called. \$\endgroup\$
    – vwdewaal
    Sep 6, 2013 at 12:32
2
\$\begingroup\$

A structure can be used when you want a type that represents a single entity. A structure is more complicated to implement correctly, so unless there is any special reason, just stick to classes.

You dont need to make it a struct to get a conventient way to create it. Make the constructor take the values.

The YesNo property doesn't even need to be stored in the type. Just calculate it when needed.

public class ReturnValue {

    public int Num { get; private set; }
    public string Str { get; private set; }

    public bool YesNo { get { return Str != "" && Num > 0; } }

    public ReturnValue(int num, string str) {
      Num = num;
      Str = str;
    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.