By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

Initializing static readonly methods

P: n/a
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

Dec 12 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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

Dec 12 '06 #2

P: n/a
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
Dec 12 '06 #3

P: n/a

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.

Dec 12 '06 #4

P: n/a
Hi sunil,
the static variable will have been initialized before you enter the
instance constructor. Make sure you set your breakpoint before you begin
execution, then as you step through you will see if working correctly. I
compiled your code and could step into the myFunc method correctly:

using System;

namespace ConsoleApplication1
{
class A
{
private static char[] temp = myFunc();
private const int i = 9;

private static char[] myFunc()
{
return null;
}

internal A()
{
}
}
class Program
{

static void Main(string[] args)
{
A a = new A();
}
}
}

Mark.
--
http://www.markdawson.org
"sunil" wrote:
>
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.

Dec 12 '06 #5

P: n/a
Hi Rahul,
Static Readonly fields can be only intialized by a static constructor.
You cannot intialize by another static function.
This is not correct, the following is perfectly legal:

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

namespace ConsoleApplication1
{
class Test
{
public static readonly string name = CreateName();

private static string CreateName()
{
return "bob";
}
}

class Program
{

static void Main(string[] args)
{
Test t = new Test();
}
}
}

Mark.
--
http://www.markdawson.org
"Rahul" wrote:
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

Dec 12 '06 #6

P: n/a

Mark R. Dawson wrote:
Hi sunil,
the static variable will have been initialized before you enter the
instance constructor. Make sure you set your breakpoint before you begin
execution, then as you step through you will see if working correctly. I
compiled your code and could step into the myFunc method correctly:

using System;

namespace ConsoleApplication1
{
class A
{
private static char[] temp = myFunc();
private const int i = 9;

private static char[] myFunc()
{
return null;
}

internal A()
{
}
}
class Program
{

static void Main(string[] args)
{
A a = new A();
}
}
}

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

Hello Mark,
I had tried out that. it works now. Before the private variables gets
instantiated only, the static variables got initialized.
Thanks for clearing my doubts

Dec 12 '06 #7

P: n/a

namespace GroupConsole
{
public class TestClass
{
//This statement will excute even before first instance is
created.
//So assigning a value to readonly is perfectly fine.
public static readonly int data1 = ReturnData();
// public static readonly int data2 = AssignData(); //Error
public static readonly int data2;

private static int ReturnData()
{
return 10;
}

private static void AssignData()
{
data2 = 100; // This can not be done.
// Because method can be called at any point of code.
Changing readonly value is not allowed.
}

static TestClass()
{
data2 = 50; // Perfectly fine.
//readonly values are has to be initialized in constructor.
There is no other place to initialize them.
// because data is static constructor has to be static....
because constructor is static, can not accept arguments.
}
}
class Program
{
static void Main(string[] args)
{
TestClass testobject = new TestClass();
Console.WriteLine(TestClass.data1.ToString());
Console.WriteLine(TestClass.data2.ToString());

Console.ReadLine();
}
}

}
readonly is useful when a constant needs to be initialized by client of
the class. For this the class constructor accepts a parameter and
assigns to readonly. From that point onwards readonly will behave like
a constant.

by qualifyinh a readonly data with static, you can only initialize the
data in a static constructor, which will not accept any parameters.
Meaning that client of the class can not assign the value at the object
initilization. class has to know the value by itself. which is as good
enough as a "const".

Finally.... static readonly data is nothing but const data.

replacing all the static readonly to const will make the code much
readable, because data will be assigned at the point of declaration....
not round a round way....

Hope I am clear what I want to say...

Thanks
-Srinivas.

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
Dec 12 '06 #8

P: n/a
Duggi <Du***************@gmail.comwrote:

<snip>
Finally.... static readonly data is nothing but const data.
No. "const" can only be used when the value is known at *compile-time*
(and only for certain types).

For instance, you might have (for whatever reason) a static readonly
field like this:

static readonly DateTime ClassInitializationTime = DateTime.Now;

That is constant as far as the process (or rather, AppDomain) is
concerned but it's not a compile-time constant.

Another point about const vs static readonly - if you're going to make
the value public and use it from a class in another assembly, and if
the value may change to a different one in a future version which you
wish to be binary compatible, you should use static readonly: const
values are compiled into "calling" code (i.e. the code reading the
value).

--
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
Dec 12 '06 #9

P: n/a
Thanks a lot mark.. and also at the same point very sorry for my
comment

Mark R. Dawson wrote:
Hi Rahul,
Static Readonly fields can be only intialized by a static constructor.
You cannot intialize by another static function.

This is not correct, the following is perfectly legal:

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

namespace ConsoleApplication1
{
class Test
{
public static readonly string name = CreateName();

private static string CreateName()
{
return "bob";
}
}

class Program
{

static void Main(string[] args)
{
Test t = new Test();
}
}
}

Mark.
--
http://www.markdawson.org
"Rahul" wrote:
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
Dec 14 '06 #10

P: n/a
Thanks a lot mark.. and also at the same point very sorry for my
comment
Nothing to be sorry about :-) We all have good posting days and not so good
posting days :-)

Mark.
--
http://www.markdawson.org
"Rahul" wrote:
Thanks a lot mark.. and also at the same point very sorry for my
comment

Mark R. Dawson wrote:
Hi Rahul,
Static Readonly fields can be only intialized by a static constructor.
You cannot intialize by another static function.
This is not correct, the following is perfectly legal:

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

namespace ConsoleApplication1
{
class Test
{
public static readonly string name = CreateName();

private static string CreateName()
{
return "bob";
}
}

class Program
{

static void Main(string[] args)
{
Test t = new Test();
}
}
}

Mark.
--
http://www.markdawson.org
"Rahul" wrote:
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
>
>

Dec 15 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.