Why doesn't the new hat-operator index from the C# 8 array-slicing feature start at 0?
C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost.
The syntax to access the last element of an array is
int value = { 10, 11, 12, 13 };
int a = value[^1]; // 13
int b = value[^2]; // 12
I'm wondering why the indexing for accessing the elements backwards starts at 1 instead of 0? Is there a technical reason for this?
c# arrays c#-8.0
add a comment |
C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost.
The syntax to access the last element of an array is
int value = { 10, 11, 12, 13 };
int a = value[^1]; // 13
int b = value[^2]; // 12
I'm wondering why the indexing for accessing the elements backwards starts at 1 instead of 0? Is there a technical reason for this?
c# arrays c#-8.0
6
Note that C++ ranges are also[beginInclusive, endExclusive)
. It is a common convention.
– bommelding
23 hours ago
2
@Sinatr: Based on that blog post, the syntax to return everything would bevalue[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently,value[^i..^0]
will give you the lasti
items.
– BlueRaja - Danny Pflughoeft
18 hours ago
@bommelding: C++rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
– DevSolar
26 mins ago
add a comment |
C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost.
The syntax to access the last element of an array is
int value = { 10, 11, 12, 13 };
int a = value[^1]; // 13
int b = value[^2]; // 12
I'm wondering why the indexing for accessing the elements backwards starts at 1 instead of 0? Is there a technical reason for this?
c# arrays c#-8.0
C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost.
The syntax to access the last element of an array is
int value = { 10, 11, 12, 13 };
int a = value[^1]; // 13
int b = value[^2]; // 12
I'm wondering why the indexing for accessing the elements backwards starts at 1 instead of 0? Is there a technical reason for this?
c# arrays c#-8.0
c# arrays c#-8.0
edited 4 hours ago
Martin Zikmund
23.4k43460
23.4k43460
asked 23 hours ago
Michael PittinoMichael Pittino
4251614
4251614
6
Note that C++ ranges are also[beginInclusive, endExclusive)
. It is a common convention.
– bommelding
23 hours ago
2
@Sinatr: Based on that blog post, the syntax to return everything would bevalue[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently,value[^i..^0]
will give you the lasti
items.
– BlueRaja - Danny Pflughoeft
18 hours ago
@bommelding: C++rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
– DevSolar
26 mins ago
add a comment |
6
Note that C++ ranges are also[beginInclusive, endExclusive)
. It is a common convention.
– bommelding
23 hours ago
2
@Sinatr: Based on that blog post, the syntax to return everything would bevalue[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently,value[^i..^0]
will give you the lasti
items.
– BlueRaja - Danny Pflughoeft
18 hours ago
@bommelding: C++rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
– DevSolar
26 mins ago
6
6
Note that C++ ranges are also
[beginInclusive, endExclusive)
. It is a common convention.– bommelding
23 hours ago
Note that C++ ranges are also
[beginInclusive, endExclusive)
. It is a common convention.– bommelding
23 hours ago
2
2
@Sinatr: Based on that blog post, the syntax to return everything would be
value[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently, value[^i..^0]
will give you the last i
items.– BlueRaja - Danny Pflughoeft
18 hours ago
@Sinatr: Based on that blog post, the syntax to return everything would be
value[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently, value[^i..^0]
will give you the last i
items.– BlueRaja - Danny Pflughoeft
18 hours ago
@bommelding: C++
rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)– DevSolar
26 mins ago
@bommelding: C++
rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)– DevSolar
26 mins ago
add a comment |
1 Answer
1
active
oldest
votes
Official answer
For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blogpost:
We decided to follow Python when it comes to the from-beginning and from-end arithmetic.
0
designates the first element (as always), and^0
the “length’th” element, i.e. the one right off the end. That way you get a simple relationship, where an elements position from beginning plus its position from end equals the length. thex
in^x
is what you would have subtracted from the length if you’d done the math yourself.
Why not use minus (
-
) instead of the new hat (^
) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple:x..^0
goes fromx
to the end. In Python there is no explicit index you can give:-0
doesn’t work, because it is equal to0
, the first element! So in Python you have to leave the end index off completely to express a range that goes to the end:x..
. If the end of the range is computed, then you need to remember to have special logic in case it comes out to0
. As inx..-y
, wherey
was computed and came out to0
. This is a common nuisance and source of bugs.
Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to being used in an indexer. You can totally define your own indexer that takes Index and another one that takes
Range
– and we’re going to add such indexers to e.g.Span
. But you can also have methods that take ranges, for instance.
My answer
I think this is to match the classic syntax we are used to:
value[^1] == value[value.Length - 1]
If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.
Other languages like Python also use the same convention.
8
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can useNone
in place of a number:[0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).
– Giacomo Alzetta
20 hours ago
4
Wait.. what's wrong withx..
? That seems fine and I've never had problem with the python[3:]
syntax.
– mowwwalker
15 hours ago
2
@mowwwalker nothing wrong. I seems thatx..
syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
12 hours ago
1
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
4 hours ago
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%2f54092458%2fwhy-doesnt-the-new-hat-operator-index-from-the-c-sharp-8-array-slicing-feature%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
Official answer
For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blogpost:
We decided to follow Python when it comes to the from-beginning and from-end arithmetic.
0
designates the first element (as always), and^0
the “length’th” element, i.e. the one right off the end. That way you get a simple relationship, where an elements position from beginning plus its position from end equals the length. thex
in^x
is what you would have subtracted from the length if you’d done the math yourself.
Why not use minus (
-
) instead of the new hat (^
) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple:x..^0
goes fromx
to the end. In Python there is no explicit index you can give:-0
doesn’t work, because it is equal to0
, the first element! So in Python you have to leave the end index off completely to express a range that goes to the end:x..
. If the end of the range is computed, then you need to remember to have special logic in case it comes out to0
. As inx..-y
, wherey
was computed and came out to0
. This is a common nuisance and source of bugs.
Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to being used in an indexer. You can totally define your own indexer that takes Index and another one that takes
Range
– and we’re going to add such indexers to e.g.Span
. But you can also have methods that take ranges, for instance.
My answer
I think this is to match the classic syntax we are used to:
value[^1] == value[value.Length - 1]
If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.
Other languages like Python also use the same convention.
8
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can useNone
in place of a number:[0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).
– Giacomo Alzetta
20 hours ago
4
Wait.. what's wrong withx..
? That seems fine and I've never had problem with the python[3:]
syntax.
– mowwwalker
15 hours ago
2
@mowwwalker nothing wrong. I seems thatx..
syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
12 hours ago
1
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
4 hours ago
add a comment |
Official answer
For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blogpost:
We decided to follow Python when it comes to the from-beginning and from-end arithmetic.
0
designates the first element (as always), and^0
the “length’th” element, i.e. the one right off the end. That way you get a simple relationship, where an elements position from beginning plus its position from end equals the length. thex
in^x
is what you would have subtracted from the length if you’d done the math yourself.
Why not use minus (
-
) instead of the new hat (^
) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple:x..^0
goes fromx
to the end. In Python there is no explicit index you can give:-0
doesn’t work, because it is equal to0
, the first element! So in Python you have to leave the end index off completely to express a range that goes to the end:x..
. If the end of the range is computed, then you need to remember to have special logic in case it comes out to0
. As inx..-y
, wherey
was computed and came out to0
. This is a common nuisance and source of bugs.
Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to being used in an indexer. You can totally define your own indexer that takes Index and another one that takes
Range
– and we’re going to add such indexers to e.g.Span
. But you can also have methods that take ranges, for instance.
My answer
I think this is to match the classic syntax we are used to:
value[^1] == value[value.Length - 1]
If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.
Other languages like Python also use the same convention.
8
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can useNone
in place of a number:[0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).
– Giacomo Alzetta
20 hours ago
4
Wait.. what's wrong withx..
? That seems fine and I've never had problem with the python[3:]
syntax.
– mowwwalker
15 hours ago
2
@mowwwalker nothing wrong. I seems thatx..
syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
12 hours ago
1
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
4 hours ago
add a comment |
Official answer
For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blogpost:
We decided to follow Python when it comes to the from-beginning and from-end arithmetic.
0
designates the first element (as always), and^0
the “length’th” element, i.e. the one right off the end. That way you get a simple relationship, where an elements position from beginning plus its position from end equals the length. thex
in^x
is what you would have subtracted from the length if you’d done the math yourself.
Why not use minus (
-
) instead of the new hat (^
) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple:x..^0
goes fromx
to the end. In Python there is no explicit index you can give:-0
doesn’t work, because it is equal to0
, the first element! So in Python you have to leave the end index off completely to express a range that goes to the end:x..
. If the end of the range is computed, then you need to remember to have special logic in case it comes out to0
. As inx..-y
, wherey
was computed and came out to0
. This is a common nuisance and source of bugs.
Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to being used in an indexer. You can totally define your own indexer that takes Index and another one that takes
Range
– and we’re going to add such indexers to e.g.Span
. But you can also have methods that take ranges, for instance.
My answer
I think this is to match the classic syntax we are used to:
value[^1] == value[value.Length - 1]
If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.
Other languages like Python also use the same convention.
Official answer
For better visibility, here is a comment from Mads Torgersen explaining this design decision from the C# 8 blogpost:
We decided to follow Python when it comes to the from-beginning and from-end arithmetic.
0
designates the first element (as always), and^0
the “length’th” element, i.e. the one right off the end. That way you get a simple relationship, where an elements position from beginning plus its position from end equals the length. thex
in^x
is what you would have subtracted from the length if you’d done the math yourself.
Why not use minus (
-
) instead of the new hat (^
) operator? This primarily has to do with ranges. Again in keeping with Python and most of the industry, we want our ranges to be inclusive at the beginning, exclusive at the end. What is the index you pass to say that a range should go all the way to the end? In C# the answer is simple:x..^0
goes fromx
to the end. In Python there is no explicit index you can give:-0
doesn’t work, because it is equal to0
, the first element! So in Python you have to leave the end index off completely to express a range that goes to the end:x..
. If the end of the range is computed, then you need to remember to have special logic in case it comes out to0
. As inx..-y
, wherey
was computed and came out to0
. This is a common nuisance and source of bugs.
Finally, note that indices and ranges are first class types in .NET/C#. Their behavior is not tied to what they are applied to, or even to being used in an indexer. You can totally define your own indexer that takes Index and another one that takes
Range
– and we’re going to add such indexers to e.g.Span
. But you can also have methods that take ranges, for instance.
My answer
I think this is to match the classic syntax we are used to:
value[^1] == value[value.Length - 1]
If it used 0, it would be confusing when the two syntaxes were used side-by-side. This way it has lower cognitive load.
Other languages like Python also use the same convention.
edited 4 hours ago
answered 23 hours ago
Martin ZikmundMartin Zikmund
23.4k43460
23.4k43460
8
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can useNone
in place of a number:[0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).
– Giacomo Alzetta
20 hours ago
4
Wait.. what's wrong withx..
? That seems fine and I've never had problem with the python[3:]
syntax.
– mowwwalker
15 hours ago
2
@mowwwalker nothing wrong. I seems thatx..
syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
12 hours ago
1
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
4 hours ago
add a comment |
8
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can useNone
in place of a number:[0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).
– Giacomo Alzetta
20 hours ago
4
Wait.. what's wrong withx..
? That seems fine and I've never had problem with the python[3:]
syntax.
– mowwwalker
15 hours ago
2
@mowwwalker nothing wrong. I seems thatx..
syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
12 hours ago
1
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
4 hours ago
8
8
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can use
None
in place of a number: [0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).– Giacomo Alzetta
20 hours ago
Minor correction to Mads comment: you do not have to leave off the end index completely in python. You can use
None
in place of a number: [0,1,2,3,4][2:None] == [2,3,4]
. But, yes you cannot use an integer as end index (without computing the length obviously).– Giacomo Alzetta
20 hours ago
4
4
Wait.. what's wrong with
x..
? That seems fine and I've never had problem with the python [3:]
syntax.– mowwwalker
15 hours ago
Wait.. what's wrong with
x..
? That seems fine and I've never had problem with the python [3:]
syntax.– mowwwalker
15 hours ago
2
2
@mowwwalker nothing wrong. I seems that
x..
syntax will be supported too. It's in example of ranges proposal– Mariusz Pawelski
12 hours ago
@mowwwalker nothing wrong. I seems that
x..
syntax will be supported too. It's in example of ranges proposal– Mariusz Pawelski
12 hours ago
1
1
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
4 hours ago
@mowwwalker - isn't that already covered in the quote? "So in Python ... If the end of the range is computed, then you need to remember to have special logic in case it comes out to 0"
– Damien_The_Unbeliever
4 hours ago
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f54092458%2fwhy-doesnt-the-new-hat-operator-index-from-the-c-sharp-8-array-slicing-feature%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
Note that C++ ranges are also
[beginInclusive, endExclusive)
. It is a common convention.– bommelding
23 hours ago
2
@Sinatr: Based on that blog post, the syntax to return everything would be
value[0..^0]
, since the ending index is exclusive (which is how most other languages work, too). Also, conveniently,value[^i..^0]
will give you the lasti
items.– BlueRaja - Danny Pflughoeft
18 hours ago
@bommelding: C++
rbegin()
somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)– DevSolar
26 mins ago