Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 默认值为";“价值”;在原始类型中_C#_Vb.net - Fatal编程技术网

C# 默认值为";“价值”;在原始类型中

C# 默认值为";“价值”;在原始类型中,c#,vb.net,C#,Vb.net,基本类型(整数、字符串等)现在是类。但是,要访问类的值,只需使用对象名(ex,x=y)。不必引用类的属性(x.value=y.value) 为了实现一个抽象数据类(比如英寸),我们需要一个value属性 尺寸x为英寸(我们班) 我们必须使用: x、 值=3 这公平吗?您可以选择重载赋值运算符以满足您的需要 例如: public static implicit operator Inches(int value) { return new Inches(value); } class P

基本类型(整数、字符串等)现在是类。但是,要访问类的值,只需使用对象名(ex,x=y)。不必引用类的属性(x.value=y.value)

为了实现一个抽象数据类(比如英寸),我们需要一个value属性 尺寸x为英寸(我们班) 我们必须使用: x、 值=3


这公平吗?

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

例如:

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

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);
    }
}

在这一点上,您可以这样做:

Inches something = 4;
另一个例子:

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

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);
    }
}


我认为您正在寻找的是一个与基元类型之间的隐式转换。Joseph给出了部分C#代码,这是VB.Net版本(具有两种运算符类型)

这允许您编写以下代码

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

你能给出一些你正在尝试做的事情的代码示例吗?正如你所知,这个问题被标记为“vb.net”。@Andrew:这在vb.net中不可行吗?或者你只是指出,因为答案是在C#?@Andrew Hare中给出的-也标记为C#@Andrew它在VB.NET和C#中都没有标记?抱歉,Joseph-我以为C#与VB.NET有相同的问题,因为我对VB.NET解决方案非常感兴趣。据我所知,vb.net不允许您覆盖分配。谢谢Jared,我在vb.net中找不到任何关于如何覆盖分配的信息