English
Summary
Conclusions
Operator overloading provides a natural notation for mathematical types: operators are declared as public static methods with the operator keyword, and the compiler converts expressions into calls to them. Comparison operators are declared in pairs and kept consistent with Equals, GetHashCode, and IComparable<T>; in C# 14, you can declare your own compound assignments, and checked operators support overflow checking. implicit conversion operators are used only for lossless conversions; otherwise, use explicit. Indexers provide access to an object by index, including with several or string parameters, and a type with Length and an indexer supports indexes from the end. Extension methods add members to existing types without changing them; extension blocks in C# 14 add properties and static members. Instance methods take precedence over extensions.
Self-check questions
- Why are operators overloaded? When should you not do it?
- What is the syntax for declaring an overloaded operator?
- Which operators are overloaded in pairs?
- Why must
==be consistent withEqualsandGetHashCode? - Which operators cannot be overloaded?
- How is compound assignment performed? What changed in C# 14?
- What are
checkedoperators for? - How does an
implicitconversion differ from anexplicitone? When do you choose which? - How do you declare an indexer? How does it differ from a property?
- How do you declare an indexer with several parameters and a string indexer?
- What is needed for your own type to support indexes from the end and ranges?
- What is an extension method? How do you declare and call it?
- What happens if a type has a method with the same signature as an extension method?
- What capabilities do
extensionblocks add in C# 14?
Useful links
- Operator overloading: https://learn.microsoft.com/dotnet/csharp/language-reference/operators/operator-overloading
- Conversion operators: https://learn.microsoft.com/dotnet/csharp/language-reference/operators/user-defined-conversion-operators
- Indexers: https://learn.microsoft.com/dotnet/csharp/programming-guide/indexers/
- Extension methods: https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
- What's new in C# 14: https://learn.microsoft.com/dotnet/csharp/whats-new/csharp-14