结构代码逻辑问题 [英] Struct code logic problem

查看:214
本文介绍了结构代码逻辑问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么这样吗?这是用C#编写的本文的一部分,我删除了大部分内容以实现这一目标.我想知道的是,binaryNumber变量如何以字符串1111结尾?我试图跟踪它,但它似乎从未调用过重写方法ToString().如果注释掉Overiden ToString()方法不起作用?它如何或何时调用该方法?以及为什么binaryNumber中的值1111和字段成员m_value中的数字15是.谢谢.

Hi, can someone possibly explain why this works? It''s a portion of an article from here written in c#, I stripped out most of it to get to this. What I would like to know is how does the variable binaryNumber end up with the string 1111? I have tried to trace it but it never seems to call the overiden method ToString(). If you comment out the overiden ToString() method it doesn''t work? How or when does it call that method? and why is the value 1111 in the binaryNumber and the number 15 in the field member m_value. Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
  public struct Bin32
  {
    private int m_Value;

    private Bin32(int value)
    {
      m_Value = value;
    }

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

    public override string ToString()
    {
      return Convert.ToString(m_Value, 2);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {

      Bin32 binaryNumber = 15;
    }
  }
}

推荐答案

ToString方法由Console.WriteLine调用.覆盖被调用.是什么让您认为它不是?
The ToString method is called by Console.WriteLine. The override is called. What makes you think it is not ?


您将看到调试器将binaryNumber显示为{1111},这当然是15的二进制表示形式. (调试器无法在ToString()中设置的断点处停止,否则调试器本身可能会锁定.)
我只是拿了你的代码并执行了它.
Main中的行用值15初始化binaryNumber,该值由私有构造函数存储在m_Value中. 如上所述,调试器将binaryNumber显示为{1111}.
我在初始化后立即在Main中添加了Console.WriteLine(binaryNumber);,它确实调用了ToString()并在该处的断点处停止.
You''ll see the debugger display binaryNumber as {1111} which is, of course, the binary representation of 15. (The debugger cannot stop at a breakpoint set in ToString() or else the debugger itself could lock up.)
I just took your code and executed it.
The line in Main initializes binaryNumber with the value 15, which is stored in m_Value by the private constructor.
The debugger shows binaryNumber as {1111} as I noted above.
I added a Console.WriteLine(binaryNumber); into Main, right after the initialization, and it does call the ToString() and stop at the breakpoint there.


控制台写行取决于您传入的变量的类型,您可能会获得不同的输出(在使用控制台写出之前更改数据类型),并且大多数数据类型都有一些隐式转换,请参见下表.

http://msdn.microsoft.com/zh-CN/library/y5b434w4 (v = vs.80).aspx [ http://msdn.microsoft.com/zh-CN/library/s8s7t687 (v = vs.80).aspx [
When your using console write line depending on the type of the variable you pass in you could get diferent outputs(change your data type before you use the console to write out) and also most data types have some implicit conversion look at the table below.

http://msdn.microsoft.com/en-US/library/y5b434w4(v=vs.80).aspx[^]

Use a format string:-

http://msdn.microsoft.com/en-US/library/s8s7t687(v=vs.80).aspx[^]


这篇关于结构代码逻辑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆