Readonly vs static readonly clarification
I've run into an interesting situation I am trying to understand. I have a readonly
struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change
method, it will be working with a copy, and the original will remain unchanged.
That is not what I am observing. I only see the expected behavior with a static field. I expected the behavior for both types.
private struct junk
{
public int i;
public void change()
{
i += 1;
}
}
private readonly junk jk;
private static readonly junk jk2;
public Form1()
{
InitializeComponent();
jk.change();
//jk.i is now 1, why? Shouldn't it be changing a copy and not the original jk?
jk2.change();
//jk2.i is 0
}
c#
New contributor
add a comment |
I've run into an interesting situation I am trying to understand. I have a readonly
struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change
method, it will be working with a copy, and the original will remain unchanged.
That is not what I am observing. I only see the expected behavior with a static field. I expected the behavior for both types.
private struct junk
{
public int i;
public void change()
{
i += 1;
}
}
private readonly junk jk;
private static readonly junk jk2;
public Form1()
{
InitializeComponent();
jk.change();
//jk.i is now 1, why? Shouldn't it be changing a copy and not the original jk?
jk2.change();
//jk2.i is 0
}
c#
New contributor
6
Mutable value types - just say no.
– Damien_The_Unbeliever
8 hours ago
3
please get rid of the irrelevant call toInitializeComponent()
and insert code that prints the values ofjk.i
andjk2.i
and show us what it prints. We do not care to read what you think the values are, we want to see what the values are.
– Mike Nakis
8 hours ago
2
(And we also want to see what makes you think the values are what you claim they are.)
– Mike Nakis
8 hours ago
2
A warning would have been nice, but the C# compiler does not have the plumbing that can ensure that change() has an observable side-effect. A counter-example is the C++/CLI compiler, it always assumes and that doesn't make programmers happy either. Addstatic Form1()
to see that it is allowed to mutate the jk2 value.
– Hans Passant
7 hours ago
add a comment |
I've run into an interesting situation I am trying to understand. I have a readonly
struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change
method, it will be working with a copy, and the original will remain unchanged.
That is not what I am observing. I only see the expected behavior with a static field. I expected the behavior for both types.
private struct junk
{
public int i;
public void change()
{
i += 1;
}
}
private readonly junk jk;
private static readonly junk jk2;
public Form1()
{
InitializeComponent();
jk.change();
//jk.i is now 1, why? Shouldn't it be changing a copy and not the original jk?
jk2.change();
//jk2.i is 0
}
c#
New contributor
I've run into an interesting situation I am trying to understand. I have a readonly
struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change
method, it will be working with a copy, and the original will remain unchanged.
That is not what I am observing. I only see the expected behavior with a static field. I expected the behavior for both types.
private struct junk
{
public int i;
public void change()
{
i += 1;
}
}
private readonly junk jk;
private static readonly junk jk2;
public Form1()
{
InitializeComponent();
jk.change();
//jk.i is now 1, why? Shouldn't it be changing a copy and not the original jk?
jk2.change();
//jk2.i is 0
}
c#
c#
New contributor
New contributor
New contributor
asked 8 hours ago
Endel_Endel_
341
341
New contributor
New contributor
6
Mutable value types - just say no.
– Damien_The_Unbeliever
8 hours ago
3
please get rid of the irrelevant call toInitializeComponent()
and insert code that prints the values ofjk.i
andjk2.i
and show us what it prints. We do not care to read what you think the values are, we want to see what the values are.
– Mike Nakis
8 hours ago
2
(And we also want to see what makes you think the values are what you claim they are.)
– Mike Nakis
8 hours ago
2
A warning would have been nice, but the C# compiler does not have the plumbing that can ensure that change() has an observable side-effect. A counter-example is the C++/CLI compiler, it always assumes and that doesn't make programmers happy either. Addstatic Form1()
to see that it is allowed to mutate the jk2 value.
– Hans Passant
7 hours ago
add a comment |
6
Mutable value types - just say no.
– Damien_The_Unbeliever
8 hours ago
3
please get rid of the irrelevant call toInitializeComponent()
and insert code that prints the values ofjk.i
andjk2.i
and show us what it prints. We do not care to read what you think the values are, we want to see what the values are.
– Mike Nakis
8 hours ago
2
(And we also want to see what makes you think the values are what you claim they are.)
– Mike Nakis
8 hours ago
2
A warning would have been nice, but the C# compiler does not have the plumbing that can ensure that change() has an observable side-effect. A counter-example is the C++/CLI compiler, it always assumes and that doesn't make programmers happy either. Addstatic Form1()
to see that it is allowed to mutate the jk2 value.
– Hans Passant
7 hours ago
6
6
Mutable value types - just say no.
– Damien_The_Unbeliever
8 hours ago
Mutable value types - just say no.
– Damien_The_Unbeliever
8 hours ago
3
3
please get rid of the irrelevant call to
InitializeComponent()
and insert code that prints the values of jk.i
and jk2.i
and show us what it prints. We do not care to read what you think the values are, we want to see what the values are.– Mike Nakis
8 hours ago
please get rid of the irrelevant call to
InitializeComponent()
and insert code that prints the values of jk.i
and jk2.i
and show us what it prints. We do not care to read what you think the values are, we want to see what the values are.– Mike Nakis
8 hours ago
2
2
(And we also want to see what makes you think the values are what you claim they are.)
– Mike Nakis
8 hours ago
(And we also want to see what makes you think the values are what you claim they are.)
– Mike Nakis
8 hours ago
2
2
A warning would have been nice, but the C# compiler does not have the plumbing that can ensure that change() has an observable side-effect. A counter-example is the C++/CLI compiler, it always assumes and that doesn't make programmers happy either. Add
static Form1()
to see that it is allowed to mutate the jk2 value.– Hans Passant
7 hours ago
A warning would have been nice, but the C# compiler does not have the plumbing that can ensure that change() has an observable side-effect. A counter-example is the C++/CLI compiler, it always assumes and that doesn't make programmers happy either. Add
static Form1()
to see that it is allowed to mutate the jk2 value.– Hans Passant
7 hours ago
add a comment |
1 Answer
1
active
oldest
votes
I have a readonly struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change method, it will be working with a copy, and the original will remain unchanged.
That's not at all what the readonly
modifier does. The readonly
modifier prevents you from assigning a new value to jk
anywhere but in a constructor. Then, the static
modifier allows you to reuse that value independently of the instance of Form1
you are working with.
That said, neither readonly
nor static
is making the weird behavior you are describing because that exact behavior cannot be reproduced with the code you've posted.
Look at a simpler example in a Console application (which you can try here):
public class Program
{
private readonly junk jk;
private static readonly junk jk2;
public static void Main()
{
var program = new Program();
program.jk.change();
Console.WriteLine(program.jk.i); // prints 0
jk2.change();
Console.WriteLine(jk2.i); // prints 0
}
}
public struct junk
{
public int i;
public void change()
{
i += 1;
}
}
Then, as @Damien_The_Unbeliever commented, try to avoid mutable struct
s as much as you can.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Endel_ is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55090417%2freadonly-vs-static-readonly-clarification%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have a readonly struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change method, it will be working with a copy, and the original will remain unchanged.
That's not at all what the readonly
modifier does. The readonly
modifier prevents you from assigning a new value to jk
anywhere but in a constructor. Then, the static
modifier allows you to reuse that value independently of the instance of Form1
you are working with.
That said, neither readonly
nor static
is making the weird behavior you are describing because that exact behavior cannot be reproduced with the code you've posted.
Look at a simpler example in a Console application (which you can try here):
public class Program
{
private readonly junk jk;
private static readonly junk jk2;
public static void Main()
{
var program = new Program();
program.jk.change();
Console.WriteLine(program.jk.i); // prints 0
jk2.change();
Console.WriteLine(jk2.i); // prints 0
}
}
public struct junk
{
public int i;
public void change()
{
i += 1;
}
}
Then, as @Damien_The_Unbeliever commented, try to avoid mutable struct
s as much as you can.
add a comment |
I have a readonly struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change method, it will be working with a copy, and the original will remain unchanged.
That's not at all what the readonly
modifier does. The readonly
modifier prevents you from assigning a new value to jk
anywhere but in a constructor. Then, the static
modifier allows you to reuse that value independently of the instance of Form1
you are working with.
That said, neither readonly
nor static
is making the weird behavior you are describing because that exact behavior cannot be reproduced with the code you've posted.
Look at a simpler example in a Console application (which you can try here):
public class Program
{
private readonly junk jk;
private static readonly junk jk2;
public static void Main()
{
var program = new Program();
program.jk.change();
Console.WriteLine(program.jk.i); // prints 0
jk2.change();
Console.WriteLine(jk2.i); // prints 0
}
}
public struct junk
{
public int i;
public void change()
{
i += 1;
}
}
Then, as @Damien_The_Unbeliever commented, try to avoid mutable struct
s as much as you can.
add a comment |
I have a readonly struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change method, it will be working with a copy, and the original will remain unchanged.
That's not at all what the readonly
modifier does. The readonly
modifier prevents you from assigning a new value to jk
anywhere but in a constructor. Then, the static
modifier allows you to reuse that value independently of the instance of Form1
you are working with.
That said, neither readonly
nor static
is making the weird behavior you are describing because that exact behavior cannot be reproduced with the code you've posted.
Look at a simpler example in a Console application (which you can try here):
public class Program
{
private readonly junk jk;
private static readonly junk jk2;
public static void Main()
{
var program = new Program();
program.jk.change();
Console.WriteLine(program.jk.i); // prints 0
jk2.change();
Console.WriteLine(jk2.i); // prints 0
}
}
public struct junk
{
public int i;
public void change()
{
i += 1;
}
}
Then, as @Damien_The_Unbeliever commented, try to avoid mutable struct
s as much as you can.
I have a readonly struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change method, it will be working with a copy, and the original will remain unchanged.
That's not at all what the readonly
modifier does. The readonly
modifier prevents you from assigning a new value to jk
anywhere but in a constructor. Then, the static
modifier allows you to reuse that value independently of the instance of Form1
you are working with.
That said, neither readonly
nor static
is making the weird behavior you are describing because that exact behavior cannot be reproduced with the code you've posted.
Look at a simpler example in a Console application (which you can try here):
public class Program
{
private readonly junk jk;
private static readonly junk jk2;
public static void Main()
{
var program = new Program();
program.jk.change();
Console.WriteLine(program.jk.i); // prints 0
jk2.change();
Console.WriteLine(jk2.i); // prints 0
}
}
public struct junk
{
public int i;
public void change()
{
i += 1;
}
}
Then, as @Damien_The_Unbeliever commented, try to avoid mutable struct
s as much as you can.
answered 8 hours ago
Camilo TerevintoCamilo Terevinto
19k63767
19k63767
add a comment |
add a comment |
Endel_ is a new contributor. Be nice, and check out our Code of Conduct.
Endel_ is a new contributor. Be nice, and check out our Code of Conduct.
Endel_ is a new contributor. Be nice, and check out our Code of Conduct.
Endel_ is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55090417%2freadonly-vs-static-readonly-clarification%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
Mutable value types - just say no.
– Damien_The_Unbeliever
8 hours ago
3
please get rid of the irrelevant call to
InitializeComponent()
and insert code that prints the values ofjk.i
andjk2.i
and show us what it prints. We do not care to read what you think the values are, we want to see what the values are.– Mike Nakis
8 hours ago
2
(And we also want to see what makes you think the values are what you claim they are.)
– Mike Nakis
8 hours ago
2
A warning would have been nice, but the C# compiler does not have the plumbing that can ensure that change() has an observable side-effect. A counter-example is the C++/CLI compiler, it always assumes and that doesn't make programmers happy either. Add
static Form1()
to see that it is allowed to mutate the jk2 value.– Hans Passant
7 hours ago