C# - Difference in behavior when auto properties are given default values vs accessors
I am trying to understand why the order of initialisation changes the values here. Shouldn't the accessor of the property return the value specified i/o the default.
Thanks.
void Main()
{
Console.WriteLine(StartDate);
Console.WriteLine(EndDate);
}
private static DateTime StartDate { get; } = new DateTime(EndDate.Year, 1, 1);
private static DateTime EndDate { get; } = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
This prints
1/1/0001 12:00:00 AM
31/12/2018 12:00:00 AM
While
void Main()
{
Console.WriteLine(StartDate);
Console.WriteLine(EndDate);
}
private static DateTime EndDate { get; } = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
private static DateTime StartDate { get; } = new DateTime(EndDate.Year, 1, 1);
prints
1/1/2018 12:00:00 AM
31/12/2018 12:00:00 AM
If I change the properties to
private static DateTime EndDate { get => new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1); }
private static DateTime StartDate { get => new DateTime(EndDate.Year, 1, 1); }
or
private static DateTime StartDate => new DateTime(EndDate.Year, 1, 1);
private static DateTime EndDate => new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
I get consistent values regardless of the order in which they are specified.
c#
add a comment |
I am trying to understand why the order of initialisation changes the values here. Shouldn't the accessor of the property return the value specified i/o the default.
Thanks.
void Main()
{
Console.WriteLine(StartDate);
Console.WriteLine(EndDate);
}
private static DateTime StartDate { get; } = new DateTime(EndDate.Year, 1, 1);
private static DateTime EndDate { get; } = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
This prints
1/1/0001 12:00:00 AM
31/12/2018 12:00:00 AM
While
void Main()
{
Console.WriteLine(StartDate);
Console.WriteLine(EndDate);
}
private static DateTime EndDate { get; } = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
private static DateTime StartDate { get; } = new DateTime(EndDate.Year, 1, 1);
prints
1/1/2018 12:00:00 AM
31/12/2018 12:00:00 AM
If I change the properties to
private static DateTime EndDate { get => new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1); }
private static DateTime StartDate { get => new DateTime(EndDate.Year, 1, 1); }
or
private static DateTime StartDate => new DateTime(EndDate.Year, 1, 1);
private static DateTime EndDate => new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
I get consistent values regardless of the order in which they are specified.
c#
2nd and 3rd looks same
– Just code
Jan 1 at 4:12
yup, 2 is the expected output. if i use 3 i get the expected output regardless of order. Issue is with 1.
– Senthil Ramanathan
Jan 1 at 4:15
blog.rogatnev.net/2017/09/13/Varieties-of-properties.html
– Backs
Jan 1 at 4:25
Sorry, the link doesnt seem to have anything related to the query above. Did I miss anything ?
– Senthil Ramanathan
Jan 1 at 4:32
add a comment |
I am trying to understand why the order of initialisation changes the values here. Shouldn't the accessor of the property return the value specified i/o the default.
Thanks.
void Main()
{
Console.WriteLine(StartDate);
Console.WriteLine(EndDate);
}
private static DateTime StartDate { get; } = new DateTime(EndDate.Year, 1, 1);
private static DateTime EndDate { get; } = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
This prints
1/1/0001 12:00:00 AM
31/12/2018 12:00:00 AM
While
void Main()
{
Console.WriteLine(StartDate);
Console.WriteLine(EndDate);
}
private static DateTime EndDate { get; } = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
private static DateTime StartDate { get; } = new DateTime(EndDate.Year, 1, 1);
prints
1/1/2018 12:00:00 AM
31/12/2018 12:00:00 AM
If I change the properties to
private static DateTime EndDate { get => new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1); }
private static DateTime StartDate { get => new DateTime(EndDate.Year, 1, 1); }
or
private static DateTime StartDate => new DateTime(EndDate.Year, 1, 1);
private static DateTime EndDate => new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
I get consistent values regardless of the order in which they are specified.
c#
I am trying to understand why the order of initialisation changes the values here. Shouldn't the accessor of the property return the value specified i/o the default.
Thanks.
void Main()
{
Console.WriteLine(StartDate);
Console.WriteLine(EndDate);
}
private static DateTime StartDate { get; } = new DateTime(EndDate.Year, 1, 1);
private static DateTime EndDate { get; } = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
This prints
1/1/0001 12:00:00 AM
31/12/2018 12:00:00 AM
While
void Main()
{
Console.WriteLine(StartDate);
Console.WriteLine(EndDate);
}
private static DateTime EndDate { get; } = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
private static DateTime StartDate { get; } = new DateTime(EndDate.Year, 1, 1);
prints
1/1/2018 12:00:00 AM
31/12/2018 12:00:00 AM
If I change the properties to
private static DateTime EndDate { get => new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1); }
private static DateTime StartDate { get => new DateTime(EndDate.Year, 1, 1); }
or
private static DateTime StartDate => new DateTime(EndDate.Year, 1, 1);
private static DateTime EndDate => new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
I get consistent values regardless of the order in which they are specified.
c#
c#
edited Jan 1 at 4:34
Senthil Ramanathan
asked Jan 1 at 4:04
Senthil RamanathanSenthil Ramanathan
8217
8217
2nd and 3rd looks same
– Just code
Jan 1 at 4:12
yup, 2 is the expected output. if i use 3 i get the expected output regardless of order. Issue is with 1.
– Senthil Ramanathan
Jan 1 at 4:15
blog.rogatnev.net/2017/09/13/Varieties-of-properties.html
– Backs
Jan 1 at 4:25
Sorry, the link doesnt seem to have anything related to the query above. Did I miss anything ?
– Senthil Ramanathan
Jan 1 at 4:32
add a comment |
2nd and 3rd looks same
– Just code
Jan 1 at 4:12
yup, 2 is the expected output. if i use 3 i get the expected output regardless of order. Issue is with 1.
– Senthil Ramanathan
Jan 1 at 4:15
blog.rogatnev.net/2017/09/13/Varieties-of-properties.html
– Backs
Jan 1 at 4:25
Sorry, the link doesnt seem to have anything related to the query above. Did I miss anything ?
– Senthil Ramanathan
Jan 1 at 4:32
2nd and 3rd looks same
– Just code
Jan 1 at 4:12
2nd and 3rd looks same
– Just code
Jan 1 at 4:12
yup, 2 is the expected output. if i use 3 i get the expected output regardless of order. Issue is with 1.
– Senthil Ramanathan
Jan 1 at 4:15
yup, 2 is the expected output. if i use 3 i get the expected output regardless of order. Issue is with 1.
– Senthil Ramanathan
Jan 1 at 4:15
blog.rogatnev.net/2017/09/13/Varieties-of-properties.html
– Backs
Jan 1 at 4:25
blog.rogatnev.net/2017/09/13/Varieties-of-properties.html
– Backs
Jan 1 at 4:25
Sorry, the link doesnt seem to have anything related to the query above. Did I miss anything ?
– Senthil Ramanathan
Jan 1 at 4:32
Sorry, the link doesnt seem to have anything related to the query above. Did I miss anything ?
– Senthil Ramanathan
Jan 1 at 4:32
add a comment |
2 Answers
2
active
oldest
votes
There are two parts to this.
First, .NET is translating your first sample into a static constructor, initializing each variable in the order that they were declared.
Second, all class fields in .NET are initialized to their default value prior to your code running.
So when you use EndDate
it has been set to its default value but your initializer for it hasn't yet run. You're accessing its default value. Essentially, you're getting this code generated:
class App
{
static readonly DateTime _startDate, _endDate;
static DateTime StartDate => _startDate;
static DateTime EndDate => _endDate;
static App()
{
// this code is put here implicitly by .NET
_startDate = default;
_endDate = default;
// and this code is put here by C#,
// translated from your initializers,
// in the order they were declared.
_startDate = new DateTime(_endDate.Year, 1, 1);
_endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
}
}
add a comment |
Static initialisers are executed in the order that they are given in code and execute only once, hence why the first code block sees the EndDate
property as a default value for DateTime
(i.e. 1/1/0001 12:00:00 AM
).
The last code block doesn't have initialisers, it's an expression-bodied member which is really shorthand for a full blown method that executes every time you call the property. For example:
private static DateTime StartDate
{
get
{
return new DateTime(EndDate.Year, 1, 1);
}
}
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
});
}
});
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%2f53992935%2fc-sharp-difference-in-behavior-when-auto-properties-are-given-default-values-v%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are two parts to this.
First, .NET is translating your first sample into a static constructor, initializing each variable in the order that they were declared.
Second, all class fields in .NET are initialized to their default value prior to your code running.
So when you use EndDate
it has been set to its default value but your initializer for it hasn't yet run. You're accessing its default value. Essentially, you're getting this code generated:
class App
{
static readonly DateTime _startDate, _endDate;
static DateTime StartDate => _startDate;
static DateTime EndDate => _endDate;
static App()
{
// this code is put here implicitly by .NET
_startDate = default;
_endDate = default;
// and this code is put here by C#,
// translated from your initializers,
// in the order they were declared.
_startDate = new DateTime(_endDate.Year, 1, 1);
_endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
}
}
add a comment |
There are two parts to this.
First, .NET is translating your first sample into a static constructor, initializing each variable in the order that they were declared.
Second, all class fields in .NET are initialized to their default value prior to your code running.
So when you use EndDate
it has been set to its default value but your initializer for it hasn't yet run. You're accessing its default value. Essentially, you're getting this code generated:
class App
{
static readonly DateTime _startDate, _endDate;
static DateTime StartDate => _startDate;
static DateTime EndDate => _endDate;
static App()
{
// this code is put here implicitly by .NET
_startDate = default;
_endDate = default;
// and this code is put here by C#,
// translated from your initializers,
// in the order they were declared.
_startDate = new DateTime(_endDate.Year, 1, 1);
_endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
}
}
add a comment |
There are two parts to this.
First, .NET is translating your first sample into a static constructor, initializing each variable in the order that they were declared.
Second, all class fields in .NET are initialized to their default value prior to your code running.
So when you use EndDate
it has been set to its default value but your initializer for it hasn't yet run. You're accessing its default value. Essentially, you're getting this code generated:
class App
{
static readonly DateTime _startDate, _endDate;
static DateTime StartDate => _startDate;
static DateTime EndDate => _endDate;
static App()
{
// this code is put here implicitly by .NET
_startDate = default;
_endDate = default;
// and this code is put here by C#,
// translated from your initializers,
// in the order they were declared.
_startDate = new DateTime(_endDate.Year, 1, 1);
_endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
}
}
There are two parts to this.
First, .NET is translating your first sample into a static constructor, initializing each variable in the order that they were declared.
Second, all class fields in .NET are initialized to their default value prior to your code running.
So when you use EndDate
it has been set to its default value but your initializer for it hasn't yet run. You're accessing its default value. Essentially, you're getting this code generated:
class App
{
static readonly DateTime _startDate, _endDate;
static DateTime StartDate => _startDate;
static DateTime EndDate => _endDate;
static App()
{
// this code is put here implicitly by .NET
_startDate = default;
_endDate = default;
// and this code is put here by C#,
// translated from your initializers,
// in the order they were declared.
_startDate = new DateTime(_endDate.Year, 1, 1);
_endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
}
}
answered Jan 1 at 4:31
Cory NelsonCory Nelson
23.1k25285
23.1k25285
add a comment |
add a comment |
Static initialisers are executed in the order that they are given in code and execute only once, hence why the first code block sees the EndDate
property as a default value for DateTime
(i.e. 1/1/0001 12:00:00 AM
).
The last code block doesn't have initialisers, it's an expression-bodied member which is really shorthand for a full blown method that executes every time you call the property. For example:
private static DateTime StartDate
{
get
{
return new DateTime(EndDate.Year, 1, 1);
}
}
add a comment |
Static initialisers are executed in the order that they are given in code and execute only once, hence why the first code block sees the EndDate
property as a default value for DateTime
(i.e. 1/1/0001 12:00:00 AM
).
The last code block doesn't have initialisers, it's an expression-bodied member which is really shorthand for a full blown method that executes every time you call the property. For example:
private static DateTime StartDate
{
get
{
return new DateTime(EndDate.Year, 1, 1);
}
}
add a comment |
Static initialisers are executed in the order that they are given in code and execute only once, hence why the first code block sees the EndDate
property as a default value for DateTime
(i.e. 1/1/0001 12:00:00 AM
).
The last code block doesn't have initialisers, it's an expression-bodied member which is really shorthand for a full blown method that executes every time you call the property. For example:
private static DateTime StartDate
{
get
{
return new DateTime(EndDate.Year, 1, 1);
}
}
Static initialisers are executed in the order that they are given in code and execute only once, hence why the first code block sees the EndDate
property as a default value for DateTime
(i.e. 1/1/0001 12:00:00 AM
).
The last code block doesn't have initialisers, it's an expression-bodied member which is really shorthand for a full blown method that executes every time you call the property. For example:
private static DateTime StartDate
{
get
{
return new DateTime(EndDate.Year, 1, 1);
}
}
answered Jan 1 at 4:33
DavidGDavidG
70.6k9112129
70.6k9112129
add a comment |
add a comment |
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%2f53992935%2fc-sharp-difference-in-behavior-when-auto-properties-are-given-default-values-v%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
2nd and 3rd looks same
– Just code
Jan 1 at 4:12
yup, 2 is the expected output. if i use 3 i get the expected output regardless of order. Issue is with 1.
– Senthil Ramanathan
Jan 1 at 4:15
blog.rogatnev.net/2017/09/13/Varieties-of-properties.html
– Backs
Jan 1 at 4:25
Sorry, the link doesnt seem to have anything related to the query above. Did I miss anything ?
– Senthil Ramanathan
Jan 1 at 4:32