C# features
This short section digs deeper into the class conversion operators briefly discussed in the previous section. Then we explore local functions, allowing us to create functions inside methods. These are general features that we can use everywhere, not just when building web APIs.
Class conversion operators (C#)
Class conversion operators are user-defined functions crafted to implicitly or explicitly convert one type to another. Many built-in types offer such conversions, such as converting an int
to a long
without any cast or method call:
int var1 = 5; long var2 = var1; // This is possible due to a class conversion operator
Next is an example of custom conversion. We convert a string
to an instance of the SomeGenericClass<string>
class without a cast:
using System; using Xunit; namespace ConversionOperator { public class SomeGenericClass<T> { public T Value { get; set; } public static implicit operator SomeGenericClass...