简体   繁体   English

在 C# ala Delphi 中定义新的简单类型

[英]Defining new simple types in C# ala Delphi

I would like to define new 'simple' types like this (in delphi):我想像这样定义新的“简单”类型(在delphi中):

type
  TString2        = string[2];
  TString10       = string[10];
  TYesNo          = (isNull=-1, isNo=0,   isYes=1);
  TBit2           = 0..3;

And then, use that inside my class-fields, like this (in delphi again):然后,在我的类字段中使用它,就像这样(再次在 delphi 中):

TCMDchild = class(TCMDParent)
strict protected
    fSgMrMs:        TString2;
    fSgIsMale:      TYesNo;
    fSgValue1:      TBit2;
    ......

¿ Are there any way to get the same easy " simple type construction " in C# (VS2010)? ¿ 有没有办法在 C# (VS2010) 中获得同样简单的“简单类型构造”?

Thanks for your comments.感谢您的意见。

Yes you can do that是的,你可以这么做

You can use using keyword to do something like delphi Type or typedef in C and C++.您可以使用using关键字执行 delphi 类型或 C 和 C++ 中的 typedef 之类的操作。

More information can be found here:更多信息可以在这里找到:

https://stackoverflow.com/a/9258058/970420 https://stackoverflow.com/a/9258058/970420

No, there aren't any type aliases like that in C#.不,在 C# 中没有任何类型别名。 It was not included, because most of the time it has been used to hide away what the code does instead of making the code clearer.它没有被包括在内,因为大多数时候它被用来隐藏代码的作用,而不是使代码更清晰。

Besides, you don't specify the size of a string in C#, and there are no range limited numbers.此外,C# 中没有指定字符串的大小,也没有范围限制的数字。 You can use properties that check the values when you set them.您可以使用在设置值时检查值的属性。

For the YesNo type you can use an enum:对于YesNo类型,您可以使用枚举:

public enum YesNo {
  No = 0,
  Yes = 1,
  Null = -1
}

class CommandChild : CommandParent {

  private string _fSgMrMs;
  private string _fSgValue1;

  public string fSgMrMs {
    get { return _fSgMrMs; }
    set {
      if (value.Length > 2) {
        throw new ArgumentException("The length of fSgMrMs can not be more than 2.");
      }
      _fSgMrMs = value;
    }
  }

  public YesNo fSgIsMale { get; set; }

  public int fSgValue1 {
    get { return _fSgValue1; }
    set {
      if (value < 0 || value > 3) {
        throw new ArgumentException("The value of fSgValue1 hase to be between 0 and 3.");
      }
      _fSgValue1 = value;
    }
  }

}

Note: You should try to use more descriptive names than things like "fSgMrMs".注意:您应该尝试使用比“fSgMrMs”等更具描述性的名称。

For the TYesNo you can use an enum:对于 TYesNo,您可以使用枚举:

public enum TYesNo
{
    IsNull = -1,
    No = 0,
    Yes = 1
}

For the others you could use properties and check the lenght in the setter:对于其他人,您可以使用属性并检查设置器中的长度:

public class TCmdChild : TCmdParent
{
    public TYesNo FSgIsMale { get; set; }

    protected string fSgMrMs;
    public string FSgMrMs
    {
        get { return fSgMrMs; }
        set
        {
            if(value.Length > 2)
                throw new OutOfRangeException("Value.Length needs to be <= 2");
            fSgMrMs = value;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM