473,466 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Initializing static readonly methods

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
10 7830
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
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

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
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
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

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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
3
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I...
19
by: cody | last post by:
Iam wondering what the benefit of using const over static readonly is. static readonly is a runtime constant and can be set once in the initializer or the static ctor, whereas const is suffering...
5
by: Sek | last post by:
hi folks, i have a bunch of strings used in my code in many places. these strings reside inside a instantiable class. so, i want to replace these with constant/static variable to control the...
8
by: John | last post by:
Hello, is there any compiler option for g++ for initializing static members of the class. Due to some unknown reason, static member in one of our c++ application is not getting initialized...
6
by: RichB | last post by:
I am slightly confused regarding when to use an instance method and when to use a static method, particularly in the context of a DAL. I have basically got a class which has methods for CRUD, an...
3
by: Reckoner | last post by:
would it be possible to use one of an object's methods without initializing the object? In other words, if I have: class Test: def __init__(self): print 'init' def foo(self): print 'foo'
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.