原始类型中“value”的默认值

时间:2022-03-24 16:27:45

primitive types (integer, string, etc) are now classes. However to access the value of the class you just use the object name (ex, x=y). It isn't necessary to refer to a property of the class (x.value = y.value).

原始类型(整数,字符串等)现在是类。但是,要访问类的值,只需使用对象名称(例如,x = y)。没有必要引用类的属性(x.value = y.value)。

To implement an abstract data class (say inches), we need a value property so if we have dim x as inches (our class) we have to use: x.value = 3

要实现一个抽象数据类(比如英寸),我们需要一个value属性,所以如果我们将dim x作为英寸(我们的类),我们必须使用:x.value = 3

is that fair?

那是公平的吗?

3 个解决方案

#1


You have the option of overloading the assignment operator to suite your needs.

您可以选择重载赋值运算符以满足您的需求。

For example:

public static implicit operator Inches(int value)
{
   return new Inches(value);
}

At which point you would then be able to do something like so:

那时你可以做到这样的事情:

Inches something = 4;

#2


I think what you are looking for is an implicit conversion to and from a primitive type. Joseph gave part of the C# code, here is the VB.Net version (with both operator types)

我认为您正在寻找的是与原始类型的隐式转换。 Joseph提供了部分C#代码,这里是VB.Net版本(包含两种运算符类型)

Class Inches
    Private _value As Integer
    Public ReadOnly Property Value() As Integer
        Get
            Return _value
        End Get
    End Property
    Public Sub New(ByVal x As Integer)
        _value = x
    End Sub

    Public Shared Widening Operator CType(ByVal x As Integer) As Inches
        Return New Inches(x)
    End Operator

    Public Shared Widening Operator CType(ByVal x As Inches) As Integer
        Return x.Value
    End Operator
End Class

This allows you to write the following code

这允许您编写以下代码

    Dim x As Inches = 42
    Dim y As Integer = x

#3


Another example:


class Program
{
    class A
    {
        private int _x;
        public A(int x)
        {
            _x = x;
        }

        public static implicit operator int(A a)
        {
            return a._x;
        }
    }
    static void Main(string[] args)
    {
        A a = new A(3);
        Console.WriteLine(a);
    }
}

#1


You have the option of overloading the assignment operator to suite your needs.

您可以选择重载赋值运算符以满足您的需求。

For example:

public static implicit operator Inches(int value)
{
   return new Inches(value);
}

At which point you would then be able to do something like so:

那时你可以做到这样的事情:

Inches something = 4;

#2


I think what you are looking for is an implicit conversion to and from a primitive type. Joseph gave part of the C# code, here is the VB.Net version (with both operator types)

我认为您正在寻找的是与原始类型的隐式转换。 Joseph提供了部分C#代码,这里是VB.Net版本(包含两种运算符类型)

Class Inches
    Private _value As Integer
    Public ReadOnly Property Value() As Integer
        Get
            Return _value
        End Get
    End Property
    Public Sub New(ByVal x As Integer)
        _value = x
    End Sub

    Public Shared Widening Operator CType(ByVal x As Integer) As Inches
        Return New Inches(x)
    End Operator

    Public Shared Widening Operator CType(ByVal x As Inches) As Integer
        Return x.Value
    End Operator
End Class

This allows you to write the following code

这允许您编写以下代码

    Dim x As Inches = 42
    Dim y As Integer = x

#3


Another example:


class Program
{
    class A
    {
        private int _x;
        public A(int x)
        {
            _x = x;
        }

        public static implicit operator int(A a)
        {
            return a._x;
        }
    }
    static void Main(string[] args)
    {
        A a = new A(3);
        Console.WriteLine(a);
    }
}