初始化静态只读方法 [英] Initializing static readonly methods

查看:71
本文介绍了初始化静态只读方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我是c#的新手。我有一些基本的编程疑惑。请帮助我

澄清这些疑惑。


我想初始化一个静态和只读字段,返回值

by静态方法。但是,当我调试时,该方法不是被调用的
。所以我无法弄清楚,该领域是否正确地进行了初始化。请解释一下这种行为

提前致谢

解决方案

Hi Sunil,

你在什么时候看调试器?在您尝试创建类的实例或访问任何

静态成员或公共字段之前,静态字段将初始化为
。你有一个小例子吗?

看到这个问题?


马克

-
http://www.markdawson.org

" sunil"写道:


你好,

我是c#的新手。我有一些基本的编程疑惑。请帮助我

澄清这些疑惑。


我想初始化一个静态和只读字段,返回值

by静态方法。但是,当我调试时,该方法不是被调用的
。所以我无法弄清楚,该领域是否正确地进行了初始化。请解释一下这种行为

提前致谢


静态只读字段只能由静态构造函数初始化。

你不能通过另一个静态函数初始化。可能这就是为什么你在调试时遇到问题。


相反,你可以尝试使用普通的静态变量并创建一个

readonly属性为exibit readonly属性。


-Rahul


sunil写道:


您好,

我是c#的新手。我有一些基本的编程疑惑。请帮助我

澄清这些疑惑。


我想初始化一个静态和只读字段,返回值

by静态方法。但是,当我调试时,该方法不是被调用的
。所以我无法弄清楚,该领域是否正确地进行了初始化。请解释一下这种行为

先谢谢




Mark R. Dawson写道:


您好Sunil,

您在调试器中查看的是什么时候?在您尝试创建类的实例或访问任何

静态成员或公共字段之前,静态字段将初始化为
。你有一个小例子吗?

看到这个问题?


马克

-
http://www.markdawson.org

< blockquote class =post_quotes>



Hello Mark

感谢您的快速回复。我正在考虑当包含静态只读字段的类的新

对象实例化为
时。举一个代码示例:


class A

{

private static char [] temp = myFunc();

private const i = 9;

private static char [] myFunc()

{

}

内部A

{

}

}


myFunc()返回char []最后。

我试图创建一个A类对象。当控件转到构造函数
时,myFunc()没有被调用。


Hello,
I am new to c# . I have some basic programming doubts. Please help me
in clarifying these doubts.

I want to initialize a static and readonly field with a value returned
by a static method. How ever, when I am debugging, that method is not
being called. So I am not able to figure out, whether the field is
getting initialized properly or not. Please explain this behavior
Thanks in advance

解决方案

Hi Sunil,
at what point are you looking in the debugger? The static field will be
initialized before you try to create an instance of the class or access any
static members or public fields. Do you have a small example where you are
seeing this problem?

Mark
--
http://www.markdawson.org
"sunil" wrote:

Hello,
I am new to c# . I have some basic programming doubts. Please help me
in clarifying these doubts.

I want to initialize a static and readonly field with a value returned
by a static method. How ever, when I am debugging, that method is not
being called. So I am not able to figure out, whether the field is
getting initialized properly or not. Please explain this behavior
Thanks in advance


Static Readonly fields can be only intialized by a static constructor.
You cannot intialize by another static function. Probably thats why you
are having problems in debugging.

Instead you can try using a normal static variable and create a
readonly property for it to exibit the readonly attribute.

-Rahul

sunil wrote:

Hello,
I am new to c# . I have some basic programming doubts. Please help me
in clarifying these doubts.

I want to initialize a static and readonly field with a value returned
by a static method. How ever, when I am debugging, that method is not
being called. So I am not able to figure out, whether the field is
getting initialized properly or not. Please explain this behavior
Thanks in advance



Mark R. Dawson wrote:

Hi Sunil,
at what point are you looking in the debugger? The static field will be
initialized before you try to create an instance of the class or access any
static members or public fields. Do you have a small example where you are
seeing this problem?

Mark
--
http://www.markdawson.org

Hello Mark
Thanks for the quick response. I was looking at the point when a new
object of the class containing the static readonly field is being
instantiated. To give a code example:

class A
{
private static char[] temp = myFunc();
private const i = 9;
private static char[] myFunc()
{
}
internal A
{
}
}

myFunc() returns the char[] at the end.
I was trying to create an object of class A. When the control goes to
the constructor, myFunc() is not being called.


这篇关于初始化静态只读方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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