The 2 C# 8 features that are too confusing to use



Check out my courses: https://nickchapsas.com Become a Patreon and get source code access: …

44 Comments

  1. For the array range indexing, I find it helps to imagine that the .. represents the elements you are pulling from, and I imagine that the ^ is just a fancy negative sign and I'm pulling elements from a circular array

  2. Personally I find two dots perfectly readable because it's how you often use it in maths, I think everybody would recognise it if it would be hand written. BUT not really when it's two dots and one number. And mixing ^ and .. is not very readable too. I sometimes see it in code, but I forget it exists and don't use unless something reminds me of this option. I bet that if we would have it from start, and everyone at the beginning would try to use it, make tutorials with it etc. it would become a default option.

  3. I don't have a personal preference here. Whatever works with what I have is going to be my option, e.g.: arrays I will default to range notation, enumerables where I'm probably already using Linq will probably follow with Skip/Take/etc.
    I'm a little disappointed with the lowered code in the Substring example though… to be fair I would probably normally just use the parameters in the Substring overloads though.

  4. Any time most dev's I know I need to mess with regex, there's a fair chance they'll to bust out notes / documentation as it is just not intuitive. Powerful and effective but annoying as hell at the same time. Not feeling a burning need for array interaction to start down that path too.

  5. I feel 2..4 reads so much better then the other method, I read it as index the values between the indices 2 and for, or get the items in the range of 2 to 4

  6. Rust is the only language I know that does slices well and always explicit and clear.

    Given a vector `v`, let's say defined `let mut v = vec![1, 2, 3, 4, 5]`:

    1. `&v[2..4]` – Immutable slice, freezes v while it exists.
    2. `&mut v[2..4]` – Mutable slice, `v` can only be modified through this mutable slice as long as it exists.
    3. `v[2..4].to_vec()` – Create a copy of the slice as a new vec.

    I don't think there is a "get nth from the end" function, but `v[v.len() – 4]` isn't too bad.
    `v.iter().rev().nth(4)` is also possible and seems to be `O(1)` in a small test I did, but I don't know if that's always true.

  7. I use the range syntax frequently. However, I only really use it on spans, where there isn't really any copy happening. On strings and arrays I either use `.ToSpan()` first, or I use the explicit methods to make the copy obvious

  8. I wanted to use this syntax yesterday, but couldn't remember if the second number was the length or the end position +1. Tried googling and looking in the MS docs, but couldn't find it because I didn't know it was called a range indexer. As it turns out, where I was using it, it was easier to think in terms of position and length, so I wound up using Substring()

  9. Gotta disagree with this video. I use Ranges and slices all the time and they make things so much nicer. As far as "not knowing what it does" after you spend 3 minutes to learn it then you know what it does, and it will be familiar to anyone who uses Python, which is almost every dev I know.

  10. "^4" is completely unintuitive. If I saw that somewhere, I would have to look it up. Definitely a thumbs down from me.
    "2..4" seems intuitive enough. Bash range operators have the same syntax, so it makes sense. Gets an "okay I guess" from me.

    None of these were really needed though. I feel like this was just added so the devs can say "Look at our cool features".

  11. The lasttwo example is confusing to read atm, but the rest seems pretty nice. C# already has a few features like this (symbol syntax) like ??= or ternary operators in general and this seems no different. Have definitely seen similar code in Python, etc, so I think it'll eventually become standard

  12. Although I am not using it currently in C#, but it looks pretty powerful specially if u code with python. back in the day when I was coding with python this syntax was a saver and I think that's why it got introduced in C#

Leave a Reply

© 2023 53GB