Initializing static readonly methods

  • Thread starter Thread starter sunil
  • Start date Start date
S

sunil

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
 
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
 
Mark said:
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
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.
 
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.
 
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 said:
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
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.
 
Mark said:
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.
[/QUOTE]
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
 
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.
 
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).
 
Thanks a lot mark.. and also at the same point very sorry for my
comment
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 said:
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
 
Thanks a lot mark.. and also at the same point very sorry for my

Nothing to be sorry about :-) We all have good posting days and not so good
posting days :-)

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


Rahul said:
Thanks a lot mark.. and also at the same point very sorry for my
comment
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 said:
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
 
Back
Top