【问题标题】:Is there a solution for this error for converting decimal to binary将十进制转换为二进制的此错误是否有解决方案
【发布时间】:2019-08-17 22:09:49
【问题描述】:

我想将十进制数 (int) 转换为二进制数并打印出来。 我在 C# 中的代码是这样的:

    int t1 = 2;
    public string test = Convert.ToString(t1, 2);

我得到错误:

字段初始值设定项不能引用非静态字段、方法或 属性“字段”

我找不到解决方案:/

我找到了解决方案:您必须将 t1 声明为静态。 static int t1 有效!

【问题讨论】:

标签: c# binary decimal


【解决方案1】:

检查此代码,

using System;

public class Program
{
    public static void Main()
    {
        int static t1 = 2;
        string test = Convert.ToString(t1, 2);
        Console.WriteLine(test);
    }
}

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 2015-12-12
    • 1970-01-01
    • 2015-07-19
    • 2021-11-14
    • 1970-01-01
    相关资源
    最近更新 更多