This chapter discusses several of the methods you can use to make your classes and other types more user-friendly. By taking advantage of operator overloading and type conversion operators and by supplying properties for your classes and structures, you can simplify the code required to use your types.
As you’ll see in this chapter, operators are symbols or keywords that are used to construct the computations that your C# program performs. Like many object-oriented languages, C# allows you to overload operators to define how operators interact with types that you have defined. (Overloading an operator consists of changing the default behavior of the operator.) C# also has strict rules about how values can be converted to a different type, and in C#, you can overload the behavior of type conversion operators so that your types are as easy to use as the built-in types.
In this chapter, a value type, named BattingAverage, is created and used as an example to demonstrate how operators, type conversions, and properties can be used to enhance user-defined types. The BattingAverage value type implements the basic behavior required for a new value type, including overriding the ToString method to properly format a string that displays a batting average in the expected format. You’ll also see how the BattingAverage structure overrides operators so that it can be used much like the built-in types.