Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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#_.net - Fatal编程技术网

C#:这个代码可以简化吗?

C#:这个代码可以简化吗?,c#,.net,C#,.net,感觉这段代码可以简化,写这一切真的很困难。我是C#的新手,我不知道它到底是什么,也不知道它与以前的开发人员离开C#后的结果是什么 有人能解释一下这是什么,我怎样才能简化它 Convert.ToInt32(new decimal(new int[] {10, 0, 0, 0}) 你应该通读以下内容 表示32位有符号整数 decimal关键字表示128位数据类型。与其他 浮点类型,十进制类型具有更高的精度和 范围较小,因此适用于金融和货币政策 计算。十进制类型的近似范围和精度 如下表所示 将

感觉这段代码可以简化,写这一切真的很困难。我是C#的新手,我不知道它到底是什么,也不知道它与以前的开发人员离开C#后的结果是什么

有人能解释一下这是什么,我怎样才能简化它

Convert.ToInt32(new decimal(new int[] {10, 0, 0, 0})

你应该通读以下内容

表示32位有符号整数

decimal关键字表示128位数据类型。与其他 浮点类型,十进制类型具有更高的精度和 范围较小,因此适用于金融和货币政策 计算。十进制类型的近似范围和精度 如下表所示

将Decimal的新实例初始化为表示的十进制值 二进制格式,并包含在指定数组中

备注

十进制数的二进制表示法由1位组成 符号、96位整数和用于分割 整数,并指定其小数部分。 比例因子隐式地是数字10,提升为指数 从0到28

  • bits是一个由32位有符号整数组成的四元素长数组

  • 位[0]、位和位包含96位整数的低、中、高32位

所以,让我们来分析一下你在做什么

array = new int[] {10, 0, 0, 0}
数组构造函数允许您从3个32位值和一些标志中构建一个十进制、128位值。这实际上非常深入,但是就int而言,数组中的第一个元素表示十进制值的前32位

例如

Console.WriteLine(new decimal(new int[] {1, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {10, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {100, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {-1, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {-10, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {-100, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {int.MaxValue, 0, 0, 0}) );     
Console.WriteLine(new decimal(new int[] {-int.MaxValue, 0, 0, 0}) );        
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {1, 0, 0, 0})));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {10, 0, 0, 0}) ));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {100, 0, 0, 0}) ));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {-1, 0, 0, 0}) ));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {-10, 0, 0, 0}) ));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {-100, 0, 0, 0}) ));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {int.MaxValue, 0, 0, 0}) ));      
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {-int.MaxValue, 0, 0, 0}) ));     
输出

1
10
100
4294967295
4294967286
4294967196
2147483647
2147483649
1
10
100
-1
-10
-100
2147483647
-2147483647
但是刚才发生了什么!即使是整数也不能转换你的思维方式。您必须将它们视为未签名,然后将它们转换为签名。

Console.WriteLine(new decimal(new int[] {1, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {10, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {100, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {-1, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {-10, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {-100, 0, 0, 0}) );
Console.WriteLine(new decimal(new int[] {int.MaxValue, 0, 0, 0}) );     
Console.WriteLine(new decimal(new int[] {-int.MaxValue, 0, 0, 0}) );        
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {1, 0, 0, 0})));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {10, 0, 0, 0}) ));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {100, 0, 0, 0}) ));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {-1, 0, 0, 0}) ));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {-10, 0, 0, 0}) ));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {-100, 0, 0, 0}) ));
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {int.MaxValue, 0, 0, 0}) ));      
Console.WriteLine((int)Convert.ToUInt32(new decimal(new int[] {-int.MaxValue, 0, 0, 0}) ));     
输出

1
10
100
4294967295
4294967286
4294967196
2147483647
2147483649
1
10
100
-1
-10
-100
2147483647
-2147483647
尽管事实上,这里是关键,您正在构建一个128位十进制(按位),但您希望将其压缩回有符号的32位值,即
Int32

您希望得到的最大值是32位数字的周期

那么这里的小数点是多少?不多(所有其他的部分都一文不值,事实上可能会给你带来更多的麻烦)

我的直觉是你只想创建一个
int
…如果你想创建一个
int
(32位数字),为什么不创建一个以

int = 1;
int = -1;
int = int.MaxValue;

表示
10
int
值的非常有趣的方法。这看起来像某些位运算代码的一部分。如果您查看
Decimal
的构造函数,它会解释此代码的作用

Decimal
有一个构造函数,它接受一个
int
s数组。前三个数字代表
Decimal
96位的低位、中位数和高位。数组中的第四个元素是带比例因子的符号位

我认为这就是它在内部所做的。创建一个表示前3位的二进制,然后将其转换为十进制

using System;

public class Program
{
    public static void Main()
    {
        int[] input = new int[] {10, 0 , 0, 0} ;


        string b1 = Convert.ToString(input[0], 2).PadLeft(32, '0');
        string b2 = Convert.ToString(input[1], 2).PadLeft(32, '0');
        string b3 = Convert.ToString(input[2], 2).PadLeft(32, '0');

        decimal result = 0;

        string s = b3+b2+b1;

        Console.WriteLine("Binary : "+s);

        for( int i=0; i<s.Length; i++ ) 
        {
          // we start with the least significant digit, and work our way to the left
          if( s[s.Length-i-1] == '0' ) continue;
          result += (decimal)Math.Pow( 2, i );
           Console.WriteLine(result);
        }


        Console.WriteLine("Calculated " + result );
        Console.WriteLine("By Framework " + (new decimal(input)));


    }
}
使用系统;
公共课程
{
公共静态void Main()
{
int[]输入=新的int[]{10,0,0,0};
字符串b1=Convert.ToString(输入[0],2).PadLeft(32,'0');
字符串b2=Convert.ToString(输入[1],2).PadLeft(32,'0');
字符串b3=Convert.ToString(输入[2],2).PadLeft(32,'0');
小数结果=0;
串s=b3+b2+b1;
Console.WriteLine(“二进制:+s”);

对于(int i=0;我想他是在把int数组转换成十进制格式,然后再转换回int。从只是看一看,到没有我的电脑,这是用来把一个4字节的输入解析成一个整数的。我唯一一次看到类似的用法是在通过串行通信字节时。代码的上下文是什么?我们是否可以简化它r并不完全取决于它的动机。
var x=10
当然更简单。如果你发布的代码正是你正在处理的代码,你可以用
10
替换它。var myInt=10?可能我只是复制并粘贴到VS中,然后运行它,它实际上只是一个值为10的int。这段代码还有其他内容吗(我注意到括号不匹配)如果不匹配,这个特定的开发人员是否有工作安全问题?