Why doesn't the new hat-operator index from the C# 8 array-slicing feature start at 0?












99














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?










share|improve this question




















  • 8




    Note that C++ ranges are also [beginInclusive, endExclusive). It is a common convention.
    – bommelding
    2 days ago








  • 3




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






  • 1




    @bommelding: C++ rbegin() somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
    – DevSolar
    yesterday










  • Oh cool, this looks like the equivalent of negative indexing in python: value[-1] # 13
    – coldspeed
    20 hours ago










  • @coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
    – Wayne Conrad
    18 hours ago
















99














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?










share|improve this question




















  • 8




    Note that C++ ranges are also [beginInclusive, endExclusive). It is a common convention.
    – bommelding
    2 days ago








  • 3




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






  • 1




    @bommelding: C++ rbegin() somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
    – DevSolar
    yesterday










  • Oh cool, this looks like the equivalent of negative indexing in python: value[-1] # 13
    – coldspeed
    20 hours ago










  • @coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
    – Wayne Conrad
    18 hours ago














99












99








99


13





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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Martin Zikmund

23.6k53460




23.6k53460










asked 2 days ago









Michael PittinoMichael Pittino

5701614




5701614








  • 8




    Note that C++ ranges are also [beginInclusive, endExclusive). It is a common convention.
    – bommelding
    2 days ago








  • 3




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






  • 1




    @bommelding: C++ rbegin() somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
    – DevSolar
    yesterday










  • Oh cool, this looks like the equivalent of negative indexing in python: value[-1] # 13
    – coldspeed
    20 hours ago










  • @coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
    – Wayne Conrad
    18 hours ago














  • 8




    Note that C++ ranges are also [beginInclusive, endExclusive). It is a common convention.
    – bommelding
    2 days ago








  • 3




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






  • 1




    @bommelding: C++ rbegin() somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
    – DevSolar
    yesterday










  • Oh cool, this looks like the equivalent of negative indexing in python: value[-1] # 13
    – coldspeed
    20 hours ago










  • @coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
    – Wayne Conrad
    18 hours ago








8




8




Note that C++ ranges are also [beginInclusive, endExclusive). It is a common convention.
– bommelding
2 days ago






Note that C++ ranges are also [beginInclusive, endExclusive). It is a common convention.
– bommelding
2 days ago






3




3




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




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




1




1




@bommelding: C++ rbegin() somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
– DevSolar
yesterday




@bommelding: C++ rbegin() somewhat disagrees with that notion -- the first item out of that range isn't the one-beyond-the-end either. ;-)
– DevSolar
yesterday












Oh cool, this looks like the equivalent of negative indexing in python: value[-1] # 13
– coldspeed
20 hours ago




Oh cool, this looks like the equivalent of negative indexing in python: value[-1] # 13
– coldspeed
20 hours ago












@coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
– Wayne Conrad
18 hours ago




@coldspeed And in Ruby the same as Python. I'm guessing they both borrowed this convention from Perl.
– Wayne Conrad
18 hours ago












1 Answer
1






active

oldest

votes


















131














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. the x 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 from x to the end. In Python there is no explicit index you can give: -0 doesn’t work, because it is equal to 0, 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 to 0. As in x..-y, where y was computed and came out to 0. 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.






share|improve this answer



















  • 11




    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
    yesterday








  • 4




    Wait.. what's wrong with x..? That seems fine and I've never had problem with the python [3:] syntax.
    – mowwwalker
    yesterday






  • 2




    @mowwwalker nothing wrong. I seems that x.. syntax will be supported too. It's in example of ranges proposal
    – Mariusz Pawelski
    yesterday








  • 7




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






  • 3




    It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
    – user2357112
    19 hours ago











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
});


}
});














draft saved

draft discarded


















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









131














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. the x 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 from x to the end. In Python there is no explicit index you can give: -0 doesn’t work, because it is equal to 0, 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 to 0. As in x..-y, where y was computed and came out to 0. 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.






share|improve this answer



















  • 11




    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
    yesterday








  • 4




    Wait.. what's wrong with x..? That seems fine and I've never had problem with the python [3:] syntax.
    – mowwwalker
    yesterday






  • 2




    @mowwwalker nothing wrong. I seems that x.. syntax will be supported too. It's in example of ranges proposal
    – Mariusz Pawelski
    yesterday








  • 7




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






  • 3




    It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
    – user2357112
    19 hours ago
















131














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. the x 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 from x to the end. In Python there is no explicit index you can give: -0 doesn’t work, because it is equal to 0, 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 to 0. As in x..-y, where y was computed and came out to 0. 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.






share|improve this answer



















  • 11




    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
    yesterday








  • 4




    Wait.. what's wrong with x..? That seems fine and I've never had problem with the python [3:] syntax.
    – mowwwalker
    yesterday






  • 2




    @mowwwalker nothing wrong. I seems that x.. syntax will be supported too. It's in example of ranges proposal
    – Mariusz Pawelski
    yesterday








  • 7




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






  • 3




    It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
    – user2357112
    19 hours ago














131












131








131






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. the x 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 from x to the end. In Python there is no explicit index you can give: -0 doesn’t work, because it is equal to 0, 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 to 0. As in x..-y, where y was computed and came out to 0. 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.






share|improve this answer














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. the x 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 from x to the end. In Python there is no explicit index you can give: -0 doesn’t work, because it is equal to 0, 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 to 0. As in x..-y, where y was computed and came out to 0. 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.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered 2 days ago









Martin ZikmundMartin Zikmund

23.6k53460




23.6k53460








  • 11




    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
    yesterday








  • 4




    Wait.. what's wrong with x..? That seems fine and I've never had problem with the python [3:] syntax.
    – mowwwalker
    yesterday






  • 2




    @mowwwalker nothing wrong. I seems that x.. syntax will be supported too. It's in example of ranges proposal
    – Mariusz Pawelski
    yesterday








  • 7




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






  • 3




    It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
    – user2357112
    19 hours ago














  • 11




    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
    yesterday








  • 4




    Wait.. what's wrong with x..? That seems fine and I've never had problem with the python [3:] syntax.
    – mowwwalker
    yesterday






  • 2




    @mowwwalker nothing wrong. I seems that x.. syntax will be supported too. It's in example of ranges proposal
    – Mariusz Pawelski
    yesterday








  • 7




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






  • 3




    It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
    – user2357112
    19 hours ago








11




11




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
yesterday






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
yesterday






4




4




Wait.. what's wrong with x..? That seems fine and I've never had problem with the python [3:] syntax.
– mowwwalker
yesterday




Wait.. what's wrong with x..? That seems fine and I've never had problem with the python [3:] syntax.
– mowwwalker
yesterday




2




2




@mowwwalker nothing wrong. I seems that x.. syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
yesterday






@mowwwalker nothing wrong. I seems that x.. syntax will be supported too. It's in example of ranges proposal
– Mariusz Pawelski
yesterday






7




7




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




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




3




3




It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
– user2357112
19 hours ago




It's good to see they're not repeating Python's mistake with the -0 thing. Handling that special case is a huge hassle and way too easy to forget.
– user2357112
19 hours ago


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

1300-talet

1300-talet

Display a custom attribute below product name in the front-end Magento 1.9.3.8