struct ToString()不会自动调用 [英] struct ToString() not automatically invoked

查看:60
本文介绍了struct ToString()不会自动调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#中的所有内容(呃,每个类)都有ToString(),这是在Debug.WriteLine()

或字符串中使用时方便自动调用的连接,等等我做了一个结构,我想

制作一个方法来打印出类似格式的数据。所以,我做了

这个:


public struct MyStruct

{

public override string ToString ()

{

返回(你好);

}

}


但是,它不会被自动调用。如果我没有把b $ b放在公共的位置,它就会抱怨或者覆盖,所以我认为它知道它已经超越

的东西(我不知道是什么,因为它不是一个类,因此不是一个

对象)。所以:

1.为什么没有覆盖而抱怨?并且

2.为什么不自动调用它?


我知道类是引用,结构是值类型。由于

结构不是一个类,我甚至不知道*为什么*它抱怨

我在其中创建一个ToString()方法。我希望它不会抱怨b $ b抱怨,也不会自动调用。但是,既然它确实抱怨了b $ b,我想也许它也会被自动调用

,它不会。


Zytan

Everything (er, every class) in C# has ToString() which is
conveniently automatically invoked when using it in Debug.WriteLine()
or in a string concatenation, etc. I made a struct, and I want to
make a method to print out its data in a similar format. So, I did
this:

public struct MyStruct
{
public override string ToString()
{
return ("Hello");
}
}

But, it doesn''t get automatically invoked. It complains if I don''t
put "public" or "override", so I assume it knows that it''s overriding
something (I am not sure what, since it''s not a class, and thus not an
object). So:
1. why does it complain without "override"? and
2. why isn''t it automatically invoked?

I know classes are references and structs are value types. Since the
struct isn''t a class, I don''t even know *why* it is complaining about
my creation of a ToString() method in it. I would expect it wouldn''t
complain, and also wouldn''t be invoked automatically. But, since it
does complain surprisingly, I thought maybe it would also be invoked
automatically, which it doesn''t.

Zytan

推荐答案

> 1。为什么没有覆盖而抱怨?并且


因为在基类中定义了具有相同签名的方法

(在本例中为System.Object)。
>1. why does it complain without "override"? and

Because a method with the same signature is defined in a base class
(System.Object in this case).


> 2。为什么不自动调用?
>2. why isn''t it automatically invoked?



在什么情况下它没有被调用?以下打印Hello

Hello正如预期的那样


public struct MyStruct

{

公共覆盖字符串ToString()

{

返回(你好);

}

}


class Test

{

static void Main()

{

MyStruct m = new MyStruct();

Console.WriteLine(" Hello" + m);

}

}

Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。

In what situation is it not invoked? The following prints "Hello
Hello" as expected for me

public struct MyStruct
{
public override string ToString()
{
return ("Hello");
}
}

class Test
{
static void Main()
{
MyStruct m = new MyStruct();
Console.WriteLine("Hello " + m);
}
}
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Zytan< zy ********** @ yahoo.comwrote:
Zytan <zy**********@yahoo.comwrote:

C#中的所有内容(呃,每个类)都有ToString(),这是在Debug.WriteLine()中使用它时方便自动调用的。
或者字符串连接等我做了一个结构,我想

制作一个方法来打印出类似格式的数据。所以,我做了

这个:


public struct MyStruct

{

public override string ToString ()

{

返回(你好);

}

}


但是,它不会被自动调用。
Everything (er, every class) in C# has ToString() which is
conveniently automatically invoked when using it in Debug.WriteLine()
or in a string concatenation, etc. I made a struct, and I want to
make a method to print out its data in a similar format. So, I did
this:

public struct MyStruct
{
public override string ToString()
{
return ("Hello");
}
}

But, it doesn''t get automatically invoked.



在什么情况下?这是一个测试,显示它被调用:


使用系统;


public struct MyStruct

{

公共覆盖字符串ToString()

{

return(" Hello");

}

}


类测试

{

static void Main()

{

MyStruct foo = new MyStruct();

Console.WriteLine(foo);

}

}

In what situation? Here''s a test which shows it being invoked:

using System;

public struct MyStruct
{
public override string ToString()
{
return ("Hello");
}
}

class Test
{
static void Main()
{
MyStruct foo = new MyStruct();
Console.WriteLine (foo);
}
}


它抱怨我是不是要把b $ b放在公共的位置。或者覆盖,所以我认为它知道它已经超越

的东西(我不知道是什么,因为它不是一个类,因此不是一个

对象)。
It complains if I don''t
put "public" or "override", so I assume it knows that it''s overriding
something (I am not sure what, since it''s not a class, and thus not an
object).



它仍然(某种)派生自对象 - 你真的在这里覆盖了一个

方法。细节非常繁琐。

It still (sort of) derives from object - you are genuinely overriding a
method here. The details are pretty fiddly.


所以:

1.为什么没有覆盖而抱怨?和
So:
1. why does it complain without "override"? and



因为那时你会隐藏方法而不是覆盖它。

Because then you''d be hiding the method instead of overriding it.


2。为什么不自动调用?
2. why isn''t it automatically invoked?



您希望什么时候自动调用它?

When are you expecting it to be automatically invoked?


我知道类是引用和结构是价值类型。由于

结构不是一个类,我甚至不知道*为什么*它抱怨

我在其中创建一个ToString()方法。我希望它不会抱怨b $ b抱怨,也不会自动调用。但是,既然它确实抱怨了b $ b,我想也许它会被自动调用

,但事实并非如此。
I know classes are references and structs are value types. Since the
struct isn''t a class, I don''t even know *why* it is complaining about
my creation of a ToString() method in it. I would expect it wouldn''t
complain, and also wouldn''t be invoked automatically. But, since it
does complain surprisingly, I thought maybe it would also be invoked
automatically, which it doesn''t.



如果没有更多关于何时可以调用它的详细信息,那就无法回答你。

无法回答你。 />

你能发一个简短但完整的程序来演示

问题吗?


http://www.pobox.com/~skeet/csharp/complete.html 详情请参阅

我的意思。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件

Without more details of when you''d expect it to be invoked, it''s
impossible to answer you.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


我不知道你做错了什么。它应该工作。


---------------------------------

使用系统;


类测试

{

public static void Main()

{

MyStruct x = new MyStruct();

Console.WriteLine(" {0}",x);

}

}


public struct MyStruct

{

公共覆盖字符串ToString( )

{

返回(你好);

}

}


-----------------------------------

这个绝对打印你好


" Zytan" < zy ********** @ yahoo.comwrote in message

news:11 ******************** *@j27g2000cwj.googlegro ups.com ...
I don''t know what you are doing wrong. It should work.

---------------------------------
using System;

class Test
{
public static void Main()
{
MyStruct x = new MyStruct();
Console.WriteLine("{0}", x);
}
}

public struct MyStruct
{
public override string ToString()
{
return ("Hello");
}
}

-----------------------------------
This definitely prints "Hello"

"Zytan" <zy**********@yahoo.comwrote in message
news:11*********************@j27g2000cwj.googlegro ups.com...

C#中的所有内容(呃,每个类)都有ToString()这是

在Debug.WriteLine()

或字符串连接等中使用时方便自动调用。我做了一个结构,我想

制作一个方法来打印以类似的格式输出数据。所以,我做了

这个:


public struct MyStruct

{

public override string ToString ()

{

返回(你好);

}

}


但是,它不会被自动调用。如果我没有把b $ b放在公共的位置,它就会抱怨或者覆盖,所以我认为它知道它已经超越

的东西(我不知道是什么,因为它不是一个类,因此不是一个

对象)。所以:

1.为什么没有覆盖而抱怨?并且

2.为什么不自动调用它?


我知道类是引用,结构是值类型。由于

结构不是一个类,我甚至不知道*为什么*它抱怨

我在其中创建一个ToString()方法。我希望它不会抱怨b $ b抱怨,也不会自动调用。但是,既然它确实抱怨了b $ b,我想也许它也会被自动调用

,它不会。


Zytan
Everything (er, every class) in C# has ToString() which is
conveniently automatically invoked when using it in Debug.WriteLine()
or in a string concatenation, etc. I made a struct, and I want to
make a method to print out its data in a similar format. So, I did
this:

public struct MyStruct
{
public override string ToString()
{
return ("Hello");
}
}

But, it doesn''t get automatically invoked. It complains if I don''t
put "public" or "override", so I assume it knows that it''s overriding
something (I am not sure what, since it''s not a class, and thus not an
object). So:
1. why does it complain without "override"? and
2. why isn''t it automatically invoked?

I know classes are references and structs are value types. Since the
struct isn''t a class, I don''t even know *why* it is complaining about
my creation of a ToString() method in it. I would expect it wouldn''t
complain, and also wouldn''t be invoked automatically. But, since it
does complain surprisingly, I thought maybe it would also be invoked
automatically, which it doesn''t.

Zytan



这篇关于struct ToString()不会自动调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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