const objects (like in C++) in C#
Is there any better way to have C++-like const objects in C# than coding all checks by yourself?
I know, I can do this:
readonly Whatever a = new Whatever();
But that just stops you from reassigning a
, it doesnt stop you from changing a
:
a = new Whatever(); // nope
a.some_property = 5; // still possible, if property exists and has a setter
I dont want all Whatever
s be constant (in the c-meaning), I just want some ones.
The tedious approach would be this:
public class Whatever
{
private bool _isConst;
private int _some_property = 0;
public Whatever(isConst = false)
{
_isConst = isConst;
}
public int some_property
{
get {return _some_property;}
set
{
if (_isConst)
Debug.Assert(" ... ");
_some_property = value;
}
}
}
Is there any better way to do this?
As the comments suggest that I didnt make myself very clear:
In C++ and in C#:
*const -> readonly
#DEFINE -> const
const(*) -> ???
c# c++ const readonly
|
show 8 more comments
Is there any better way to have C++-like const objects in C# than coding all checks by yourself?
I know, I can do this:
readonly Whatever a = new Whatever();
But that just stops you from reassigning a
, it doesnt stop you from changing a
:
a = new Whatever(); // nope
a.some_property = 5; // still possible, if property exists and has a setter
I dont want all Whatever
s be constant (in the c-meaning), I just want some ones.
The tedious approach would be this:
public class Whatever
{
private bool _isConst;
private int _some_property = 0;
public Whatever(isConst = false)
{
_isConst = isConst;
}
public int some_property
{
get {return _some_property;}
set
{
if (_isConst)
Debug.Assert(" ... ");
_some_property = value;
}
}
}
Is there any better way to do this?
As the comments suggest that I didnt make myself very clear:
In C++ and in C#:
*const -> readonly
#DEFINE -> const
const(*) -> ???
c# c++ const readonly
1
@preciousbetine: no, you cant doconst Whatever a = new Whatever();
– rhavin
Nov 23 '18 at 18:39
2
With properties you can declare public get {} private set {}. Is that good enough?
– Wyck
Nov 23 '18 at 19:01
1
@Wyck No: "I dont want all Whatevers be constant (in the c-meaning), I just want some ones."
– rhavin
Nov 23 '18 at 19:05
1
Just stop treating C# like it is C++, it doesn't get you very far. const in C++ has awful warts with const_cast<> and mutable and no actual enforcement at runtime and a significant number of programmers that gave up on it because they couldn't figure it out. It was intentionally left out of the CLR and C#. Lots of C# programmers live happily without out. Well, all of them :)
– Hans Passant
Nov 23 '18 at 19:56
1
@HansPassant A surprising statement to me, but coming from you it must have merit. In my book const was a good thing, enforceability or not, because it states the programmer's intention. "FYI only." It's all the more important in a language like C# that passes almost everything (sans atomic and other trivial types) by reference, and lacks proper "copy constructor" support at the same time.
– Peter A. Schneider
Nov 23 '18 at 20:08
|
show 8 more comments
Is there any better way to have C++-like const objects in C# than coding all checks by yourself?
I know, I can do this:
readonly Whatever a = new Whatever();
But that just stops you from reassigning a
, it doesnt stop you from changing a
:
a = new Whatever(); // nope
a.some_property = 5; // still possible, if property exists and has a setter
I dont want all Whatever
s be constant (in the c-meaning), I just want some ones.
The tedious approach would be this:
public class Whatever
{
private bool _isConst;
private int _some_property = 0;
public Whatever(isConst = false)
{
_isConst = isConst;
}
public int some_property
{
get {return _some_property;}
set
{
if (_isConst)
Debug.Assert(" ... ");
_some_property = value;
}
}
}
Is there any better way to do this?
As the comments suggest that I didnt make myself very clear:
In C++ and in C#:
*const -> readonly
#DEFINE -> const
const(*) -> ???
c# c++ const readonly
Is there any better way to have C++-like const objects in C# than coding all checks by yourself?
I know, I can do this:
readonly Whatever a = new Whatever();
But that just stops you from reassigning a
, it doesnt stop you from changing a
:
a = new Whatever(); // nope
a.some_property = 5; // still possible, if property exists and has a setter
I dont want all Whatever
s be constant (in the c-meaning), I just want some ones.
The tedious approach would be this:
public class Whatever
{
private bool _isConst;
private int _some_property = 0;
public Whatever(isConst = false)
{
_isConst = isConst;
}
public int some_property
{
get {return _some_property;}
set
{
if (_isConst)
Debug.Assert(" ... ");
_some_property = value;
}
}
}
Is there any better way to do this?
As the comments suggest that I didnt make myself very clear:
In C++ and in C#:
*const -> readonly
#DEFINE -> const
const(*) -> ???
c# c++ const readonly
c# c++ const readonly
edited Nov 23 '18 at 19:02
rhavin
asked Nov 23 '18 at 18:25
rhavinrhavin
6791527
6791527
1
@preciousbetine: no, you cant doconst Whatever a = new Whatever();
– rhavin
Nov 23 '18 at 18:39
2
With properties you can declare public get {} private set {}. Is that good enough?
– Wyck
Nov 23 '18 at 19:01
1
@Wyck No: "I dont want all Whatevers be constant (in the c-meaning), I just want some ones."
– rhavin
Nov 23 '18 at 19:05
1
Just stop treating C# like it is C++, it doesn't get you very far. const in C++ has awful warts with const_cast<> and mutable and no actual enforcement at runtime and a significant number of programmers that gave up on it because they couldn't figure it out. It was intentionally left out of the CLR and C#. Lots of C# programmers live happily without out. Well, all of them :)
– Hans Passant
Nov 23 '18 at 19:56
1
@HansPassant A surprising statement to me, but coming from you it must have merit. In my book const was a good thing, enforceability or not, because it states the programmer's intention. "FYI only." It's all the more important in a language like C# that passes almost everything (sans atomic and other trivial types) by reference, and lacks proper "copy constructor" support at the same time.
– Peter A. Schneider
Nov 23 '18 at 20:08
|
show 8 more comments
1
@preciousbetine: no, you cant doconst Whatever a = new Whatever();
– rhavin
Nov 23 '18 at 18:39
2
With properties you can declare public get {} private set {}. Is that good enough?
– Wyck
Nov 23 '18 at 19:01
1
@Wyck No: "I dont want all Whatevers be constant (in the c-meaning), I just want some ones."
– rhavin
Nov 23 '18 at 19:05
1
Just stop treating C# like it is C++, it doesn't get you very far. const in C++ has awful warts with const_cast<> and mutable and no actual enforcement at runtime and a significant number of programmers that gave up on it because they couldn't figure it out. It was intentionally left out of the CLR and C#. Lots of C# programmers live happily without out. Well, all of them :)
– Hans Passant
Nov 23 '18 at 19:56
1
@HansPassant A surprising statement to me, but coming from you it must have merit. In my book const was a good thing, enforceability or not, because it states the programmer's intention. "FYI only." It's all the more important in a language like C# that passes almost everything (sans atomic and other trivial types) by reference, and lacks proper "copy constructor" support at the same time.
– Peter A. Schneider
Nov 23 '18 at 20:08
1
1
@preciousbetine: no, you cant do
const Whatever a = new Whatever();
– rhavin
Nov 23 '18 at 18:39
@preciousbetine: no, you cant do
const Whatever a = new Whatever();
– rhavin
Nov 23 '18 at 18:39
2
2
With properties you can declare public get {} private set {}. Is that good enough?
– Wyck
Nov 23 '18 at 19:01
With properties you can declare public get {} private set {}. Is that good enough?
– Wyck
Nov 23 '18 at 19:01
1
1
@Wyck No: "I dont want all Whatevers be constant (in the c-meaning), I just want some ones."
– rhavin
Nov 23 '18 at 19:05
@Wyck No: "I dont want all Whatevers be constant (in the c-meaning), I just want some ones."
– rhavin
Nov 23 '18 at 19:05
1
1
Just stop treating C# like it is C++, it doesn't get you very far. const in C++ has awful warts with const_cast<> and mutable and no actual enforcement at runtime and a significant number of programmers that gave up on it because they couldn't figure it out. It was intentionally left out of the CLR and C#. Lots of C# programmers live happily without out. Well, all of them :)
– Hans Passant
Nov 23 '18 at 19:56
Just stop treating C# like it is C++, it doesn't get you very far. const in C++ has awful warts with const_cast<> and mutable and no actual enforcement at runtime and a significant number of programmers that gave up on it because they couldn't figure it out. It was intentionally left out of the CLR and C#. Lots of C# programmers live happily without out. Well, all of them :)
– Hans Passant
Nov 23 '18 at 19:56
1
1
@HansPassant A surprising statement to me, but coming from you it must have merit. In my book const was a good thing, enforceability or not, because it states the programmer's intention. "FYI only." It's all the more important in a language like C# that passes almost everything (sans atomic and other trivial types) by reference, and lacks proper "copy constructor" support at the same time.
– Peter A. Schneider
Nov 23 '18 at 20:08
@HansPassant A surprising statement to me, but coming from you it must have merit. In my book const was a good thing, enforceability or not, because it states the programmer's intention. "FYI only." It's all the more important in a language like C# that passes almost everything (sans atomic and other trivial types) by reference, and lacks proper "copy constructor" support at the same time.
– Peter A. Schneider
Nov 23 '18 at 20:08
|
show 8 more comments
0
active
oldest
votes
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
});
}
});
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%2f53451447%2fconst-objects-like-in-c-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53451447%2fconst-objects-like-in-c-in-c-sharp%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
1
@preciousbetine: no, you cant do
const Whatever a = new Whatever();
– rhavin
Nov 23 '18 at 18:39
2
With properties you can declare public get {} private set {}. Is that good enough?
– Wyck
Nov 23 '18 at 19:01
1
@Wyck No: "I dont want all Whatevers be constant (in the c-meaning), I just want some ones."
– rhavin
Nov 23 '18 at 19:05
1
Just stop treating C# like it is C++, it doesn't get you very far. const in C++ has awful warts with const_cast<> and mutable and no actual enforcement at runtime and a significant number of programmers that gave up on it because they couldn't figure it out. It was intentionally left out of the CLR and C#. Lots of C# programmers live happily without out. Well, all of them :)
– Hans Passant
Nov 23 '18 at 19:56
1
@HansPassant A surprising statement to me, but coming from you it must have merit. In my book const was a good thing, enforceability or not, because it states the programmer's intention. "FYI only." It's all the more important in a language like C# that passes almost everything (sans atomic and other trivial types) by reference, and lacks proper "copy constructor" support at the same time.
– Peter A. Schneider
Nov 23 '18 at 20:08