初始化静态只读方法

编程入门 行业动态 更新时间:2024-10-27 02:18:12
本文介绍了初始化静态只读方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 我是c#的新手。我有一些基本的编程疑惑。请帮助我 澄清这些疑惑。 我想初始化一个静态和只读字段,返回值 by静态方法。但是,当我调试时,该方法不是被调用的。所以我无法弄清楚,该领域是否正确地进行了初始化。请解释一下这种行为 提前致谢

解决方案

Hi Sunil, 你在什么时候看调试器?在您尝试创建类的实例或访问任何 静态成员或公共字段之前,静态字段将初始化为。你有一个小例子吗? 看到这个问题? 马克 - www.markdawson " sunil"写道:

你好, 我是c#的新手。我有一些基本的编程疑惑。请帮助我 澄清这些疑惑。 我想初始化一个静态和只读字段,返回值 by静态方法。但是,当我调试时,该方法不是被调用的。所以我无法弄清楚,该领域是否正确地进行了初始化。请解释一下这种行为 提前致谢

静态只读字段只能由静态构造函数初始化。 你不能通过另一个静态函数初始化。可能这就是为什么你在调试时遇到问题。 相反,你可以尝试使用普通的静态变量并创建一个 readonly属性为exibit readonly属性。 -Rahul sunil写道:

您好, 我是c#的新手。我有一些基本的编程疑惑。请帮助我 澄清这些疑惑。 我想初始化一个静态和只读字段,返回值 by静态方法。但是,当我调试时,该方法不是被调用的。所以我无法弄清楚,该领域是否正确地进行了初始化。请解释一下这种行为 先谢谢

Mark R. Dawson写道:

您好Sunil, 您在调试器中查看的是什么时候?在您尝试创建类的实例或访问任何 静态成员或公共字段之前,静态字段将初始化为。你有一个小例子吗? 看到这个问题? 马克 - www.markdawson

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 -- www.markdawson "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 -- www.markdawson

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.

更多推荐

初始化静态只读方法

本文发布于:2023-11-06 07:43:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   静态   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!