Overriding the Defaults in a struct (c#)

前端 未结 12 939
感情败类
感情败类 2021-01-04 00:47

Is it possible to set or override the default state for a structure?

As an example I have an

enum something{a,b,c,d,e};

and a struc

相关标签:
12条回答
  • 2021-01-04 01:15

    Struct constructors are similar to class constructors, except for the following differences:

    Structs cannot contain explicit parameterless constructors. Struct members are automatically initialized to their default values. A struct cannot have an initializer in the form: base (argument-list).

    http://msdn.microsoft.com/en-us/library/aa288208(v=vs.71).aspx

    So, short answer, no you can't override the default constructor (every struct has a parameterless constructor and you can't hide it or override it)...

    0 讨论(0)
  • 2021-01-04 01:15

    Kinda dumb, but works

    public readonly static float default_value = 1;
    public struct YourStruct{
    
        public float yourValue{
            get {
                return _yourValue + default_value;
            }
            set {
                _yourValue= value - default_value;
            }
        }
        public float _yourValue;
    }
    
    0 讨论(0)
  • 2021-01-04 01:17

    You can't override the default (parameterless) constructor for a struct. You can only add new constructors, which take parameters.

    http://csharp.2000things.com/2010/10/03/108-defining-a-constructor-for-a-struct/

    0 讨论(0)
  • 2021-01-04 01:22

    Each time you get/set property you need to set default value call InitDefaultValues() method

    private string _numberDecimalSeparator;
    public string NumberDecimalSeparator
    {
        get
        {
            InitDefaultValues();
            return _numberDecimalSeparator;
        }
        set
        {
            InitDefaultValues(); 
            _numberDecimalSeparator = value;
        }
    }
    

    ...

    private void InitDefaultValues()
    {
        if (!_inited)
        {
            _inited = false;
            var ci = CultureInfo.CurrentCulture;
             _numberDecimalSeparator = ci.With(x => x.NumberFormat).Return(x => x.NumberDecimalSeparator, ".");
    
            ...
        }
    }
    
    0 讨论(0)
  • 2021-01-04 01:25

    There is a workaround

    public struct MyStruct
    {
        public MyStruct(int h = 1, int l = 1)
        {
            high = h;
            low = l;
        }
        public int high;
        public int low;
    }
    
    0 讨论(0)
  • 2021-01-04 01:29

    this should work

    public struct MyStruct 
    {
        private string myName;
        private int? myNumber;
        private bool? myBoolean;
        private MyRefType myType;
    
        public string MyName
        {
            get { return myName ?? "Default name"; }
            set { myName= value; }
        }
        public int MyNumber
        {
            get { return myNumber ?? 42; }
            set { myNumber = value; }
        }
        public bool MyBoolean
        {
            get { return myBoolean ?? true; }
            set { myBoolean = value; }
        }
        public MyRefType MyType 
        {
            get { return myType ?? new MyRefType(); }
            set { myType = value; }
        }
    
        //optional
        public MyStruct(string myName = "Default name", int myNumber = 42, bool myBoolean = true)
        {
            this.myType = new MyRefType();
            this.myName = myName;
            this.myNumber = myNumber;
            this.myBoolean = myBoolean;
        }
    }
    

    [TestClass]
    public class MyStructTest
    {
        [TestMethod]
        public void TestMyStruct()
        {
            var myStruct = default(MyStruct);
            Assert.AreEqual("Default name", myStruct.MyName);
            Assert.AreEqual(42, myStruct.MyNumber);
            Assert.AreEqual(true, myStruct.MyBoolean);
            Assert.IsNotNull(myStruct.MyType);
        }
    }
    
    0 讨论(0)
提交回复
热议问题