Static abstract members nelle interfacce di C# 11 e
dintorni di .NET 7
Sponsors
With the support of:
About me
• Senion Solution Architect @ beanTech
• 1nn0va Community Lead (Pordenone)
• Microsoft Azure MVP
• Linkedin: https://www.linkedin.com/in/marcoparenzan/
• Slideshare: https://www.slideshare.net/marco.parenzan
• GitHub: https://github.com/marcoparenzan
Let’s begin with Azure IoT
Azure IoT Central and IoT PnP
• When you work with IoT Central, you work with IoT PnP
• IoT PnPDigital Twin Definition Language
• InterfaceComponentCapability
• Capability=Telemetry // Property // Command
• When you define a Telemetry, it has
• Semantic
• Unit
• Type
• All described in JSON
• You can do code generation in front of it!
Demo
Generate types from DTDL
Meanwhile in .net space...
C# 1: Value Types and Reference Types
• Reference Type (allocated in heap – referenced in .... Stack...object, polymorphic)
• Value Type (allocated in stack – non polimorphic)
• Struct and Class for custom typesguided by a domain design
• Int, float, double, boolnot guided by a domain
«missing type»=scalars!
• Scalar=Primitive=Struct(ural)=sealed[cannot derive]
• You cannot qualify a scalar
• All class goodies (type checking) are (almost) avoided
• Lot of mess:
• Method signs
• Overload collisions
• Custom Types can introduce more meaning
• How?
• Like triggers in SQL, .NET has structs (missing in action)
Value Types in .NET
• ...or more simpler, structs in C#
• Remember: the formal definition says «allocated into the stack, not into the heap»
• Be more precise
• Reference typea variable, or a parameter, containes a reference to an entity into the heap
• Value type a variable, or a parameter, containes directly the entity
• Value typecopy semantic
• Reference typereference (obviously) semantics
• Value types are fast
C# 3: Extension methods
• Adding methods «externally injected» into objects
• Defined in static methods in static classes, with first argument decorated by «this» keyword
C# 8: default interface methods
• Add support for virtual extension methods - methods in interfaces with concrete implementations. A
class or struct that implements such an interface is required to have a single most
specific implementation for the interface method, either implemented by the class or struct, or
inherited from its base classes or interfaces. Virtual extension methods enable an API author to add
methods to an interface in future versions without breaking source or binary compatibility with
existing implementations of that interface.
C#11: Static abstract members in interfaces
• An interface is allowed to specify abstract static members that implementing classes and structs are
then required to provide an explicit or implicit implementation of. The members can be accessed off
of type parameters that are constrained by the interface.
Parsing Interfaces
Interface name Description
IParsable<TSelf> Exposes support for T.Parse(string, IFormatProvider) and T.TryParse(string,
IFormatProvider, out TSelf).
ISpanParsable<TSelf> Exposes support for T.Parse(ReadOnlySpan<char>,
IFormatProvider) and T.TryParse(ReadOnlySpan<char>, IFormatProvider, out
TSelf).
IFormattable
1
Exposes support for value.ToString(string, IFormatProvider).
ISpanFormattable
1
Exposes support for value.TryFormat(Span<char>, out int,
ReadOnlySpan<char>, IFormatProvider).
C# 11: Generic Math
• .NET 7 introduces new math-related generic interfaces to the base class library. The availability of
these interfaces means you can constrain a type parameter of a generic type or method to be
"number-like". In addition, C# 11 and later lets you define static virtual interface members. Because
operators must be declared as static, this new C# feature lets operators be declared in the new
interfaces for number-like types.
• Together, these innovations allow you to perform mathematical operations generically—that is,
without having to know the exact type you're working with. For example, if you wanted to write a
method that adds two numbers, previously you had to add an overload of the method for each type
(for example, static int Add(int first, int second) and static float Add(float first, float second)).
Numeric Interfaces
Interface name Description
IBinaryFloatingPointIeee754<TSelf> Exposes APIs common to binary floating-point types
1
that implement the IEEE 754 standard.
IBinaryInteger<TSelf> Exposes APIs common to binary integers
2
.
IBinaryNumber<TSelf> Exposes APIs common to binary numbers.
IFloatingPoint<TSelf> Exposes APIs common to floating-point types.
IFloatingPointIeee754<TSelf> Exposes APIs common to floating-point types that implement the IEEE 754 standard.
INumber<TSelf> Exposes APIs common to comparable number types (effectively the "real" number domain).
INumberBase<TSelf> Exposes APIs common to all number types (effectively the "complex" number domain).
ISignedNumber<TSelf> Exposes APIs common to all signed number types (such as the concept of NegativeOne).
IUnsignedNumber<TSelf> Exposes APIs common to all unsigned number types.
IAdditiveIdentity<TSelf,TResult> Exposes the concept of (x + T.AdditiveIdentity) == x.
IMinMaxValue<TSelf> Exposes the concept of T.MinValue and T.MaxValue.
IMultiplicativeIdentity<TSelf,TResult> Exposes the concept of (x * T.MultiplicativeIdentity) == x.
Operator Interfaces
Interface name Defined operators
IAdditionOperators<TSelf,TOther,TResult> x + y
IBitwiseOperators<TSelf,TOther,TResult> x & y, x | y, x ^ y, and ~x
IComparisonOperators<TSelf,TOther,TResult> x < y, x > y, x <= y, and x >= y
IDecrementOperators<TSelf> --x and x--
IDivisionOperators<TSelf,TOther,TResult> x / y
IEqualityOperators<TSelf,TOther,TResult> x == y and x != y
IIncrementOperators<TSelf> ++x and x++
IModulusOperators<TSelf,TOther,TResult> x % y
IMultiplyOperators<TSelf,TOther,TResult> x * y
IShiftOperators<TSelf,TOther,TResult> x << y and x >> y
ISubtractionOperators<TSelf,TOther,TResult> x - y
IUnaryNegationOperators<TSelf,TResult> -x
IUnaryPlusOperators<TSelf,TResult> +x
Function Parsing Interfaces
nterface name Description
IExponentialFunctions<TSelf> Exposes exponential functions supporting e^x, e^x -
1, 2^x, 2^x - 1, 10^x, and 10^x - 1.
IHyperbolicFunctions<TSelf> Exposes hyperbolic functions
supporting acosh(x), asinh(x), atanh(x), cosh(x), sinh(x),
and tanh(x).
ILogarithmicFunctions<TSelf> Exposes logarithmic functions supporting ln(x), ln(x +
1), log2(x), log2(x + 1), log10(x), and log10(x + 1).
IPowerFunctions<TSelf> Exposes power functions supporting x^y.
IRootFunctions<TSelf> Exposes root functions supporting cbrt(x) and sqrt(x).
ITrigonometricFunctions<TSelf> Exposes trigonometric functions
supporting acos(x), asin(x), atan(x), cos(x), sin(x),
and tan(x).
Demo
Let’s see the future with .NET 7
The missing «type» in strongly typed
languages...
Demo
Let’s see how fast are values types
Math with value types
• Another «missing in action»: implicit operator and operator overloading
• Do you remember that you can overload aritmetic operators to use your value types?
• And that you can cast from scalars to structural types (also reference types)?
• Do you know that all of these can become a great math?
Demo
Math with value types
Let’s remember physics
• Volt+Volt=Volt (you can add or subtract homogeneus values)
• Volt*Ampere=Ampere*Volt=Watt (multiplication is commutative)
• Watt/Ampere != Ampere/Watt (division is not commutative)
• ...and remember the plain good old ToString method!
Innovation in JSON Parsing
• Some new features
• Contract customization gives you more control over how types are serialized and deserialized. For more
information, see Customize a JSON contract.
• Polymorphic serialization for user-defined type hierarchies. For more information, see Serialize properties of
derived classes.
• Support for required members, which are properties that must be present in the JSON payload for
deserialization to succeed. For more information, see Required properties.
Demo
Let’s write some physics
Not let’s enter into the scripting
world
«Scripting» in .NET
• Physics and math is about customizing a job (because you have to «express» some laws)
• You have to express calculations
• A couple of choices:
• CSharp Scripting (Roslyn)
• ANTLR
Demo
C# scripting
Using ANTLR
Where Math and science and code met 30yr ago
• Mathematica! From Stephen Wolfram!
• Now we have it also in .NET
• Notebooks
• Kernels
Demo
Write a kernel for your lang
Conclusion
Conclusion
• Math can be a lot of fun
• .NET can be a lot of fun in the science area

Static abstract members nelle interfacce di C# 11 e dintorni di .NET 7.pptx

  • 1.
    Static abstract membersnelle interfacce di C# 11 e dintorni di .NET 7
  • 2.
  • 3.
    About me • SenionSolution Architect @ beanTech • 1nn0va Community Lead (Pordenone) • Microsoft Azure MVP • Linkedin: https://www.linkedin.com/in/marcoparenzan/ • Slideshare: https://www.slideshare.net/marco.parenzan • GitHub: https://github.com/marcoparenzan
  • 4.
  • 5.
    Azure IoT Centraland IoT PnP • When you work with IoT Central, you work with IoT PnP • IoT PnPDigital Twin Definition Language • InterfaceComponentCapability • Capability=Telemetry // Property // Command • When you define a Telemetry, it has • Semantic • Unit • Type • All described in JSON • You can do code generation in front of it!
  • 6.
  • 7.
  • 8.
    C# 1: ValueTypes and Reference Types • Reference Type (allocated in heap – referenced in .... Stack...object, polymorphic) • Value Type (allocated in stack – non polimorphic) • Struct and Class for custom typesguided by a domain design • Int, float, double, boolnot guided by a domain
  • 9.
    «missing type»=scalars! • Scalar=Primitive=Struct(ural)=sealed[cannotderive] • You cannot qualify a scalar • All class goodies (type checking) are (almost) avoided • Lot of mess: • Method signs • Overload collisions • Custom Types can introduce more meaning • How? • Like triggers in SQL, .NET has structs (missing in action)
  • 10.
    Value Types in.NET • ...or more simpler, structs in C# • Remember: the formal definition says «allocated into the stack, not into the heap» • Be more precise • Reference typea variable, or a parameter, containes a reference to an entity into the heap • Value type a variable, or a parameter, containes directly the entity • Value typecopy semantic • Reference typereference (obviously) semantics • Value types are fast
  • 11.
    C# 3: Extensionmethods • Adding methods «externally injected» into objects • Defined in static methods in static classes, with first argument decorated by «this» keyword
  • 12.
    C# 8: defaultinterface methods • Add support for virtual extension methods - methods in interfaces with concrete implementations. A class or struct that implements such an interface is required to have a single most specific implementation for the interface method, either implemented by the class or struct, or inherited from its base classes or interfaces. Virtual extension methods enable an API author to add methods to an interface in future versions without breaking source or binary compatibility with existing implementations of that interface.
  • 13.
    C#11: Static abstractmembers in interfaces • An interface is allowed to specify abstract static members that implementing classes and structs are then required to provide an explicit or implicit implementation of. The members can be accessed off of type parameters that are constrained by the interface.
  • 14.
    Parsing Interfaces Interface nameDescription IParsable<TSelf> Exposes support for T.Parse(string, IFormatProvider) and T.TryParse(string, IFormatProvider, out TSelf). ISpanParsable<TSelf> Exposes support for T.Parse(ReadOnlySpan<char>, IFormatProvider) and T.TryParse(ReadOnlySpan<char>, IFormatProvider, out TSelf). IFormattable 1 Exposes support for value.ToString(string, IFormatProvider). ISpanFormattable 1 Exposes support for value.TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider).
  • 15.
    C# 11: GenericMath • .NET 7 introduces new math-related generic interfaces to the base class library. The availability of these interfaces means you can constrain a type parameter of a generic type or method to be "number-like". In addition, C# 11 and later lets you define static virtual interface members. Because operators must be declared as static, this new C# feature lets operators be declared in the new interfaces for number-like types. • Together, these innovations allow you to perform mathematical operations generically—that is, without having to know the exact type you're working with. For example, if you wanted to write a method that adds two numbers, previously you had to add an overload of the method for each type (for example, static int Add(int first, int second) and static float Add(float first, float second)).
  • 16.
    Numeric Interfaces Interface nameDescription IBinaryFloatingPointIeee754<TSelf> Exposes APIs common to binary floating-point types 1 that implement the IEEE 754 standard. IBinaryInteger<TSelf> Exposes APIs common to binary integers 2 . IBinaryNumber<TSelf> Exposes APIs common to binary numbers. IFloatingPoint<TSelf> Exposes APIs common to floating-point types. IFloatingPointIeee754<TSelf> Exposes APIs common to floating-point types that implement the IEEE 754 standard. INumber<TSelf> Exposes APIs common to comparable number types (effectively the "real" number domain). INumberBase<TSelf> Exposes APIs common to all number types (effectively the "complex" number domain). ISignedNumber<TSelf> Exposes APIs common to all signed number types (such as the concept of NegativeOne). IUnsignedNumber<TSelf> Exposes APIs common to all unsigned number types. IAdditiveIdentity<TSelf,TResult> Exposes the concept of (x + T.AdditiveIdentity) == x. IMinMaxValue<TSelf> Exposes the concept of T.MinValue and T.MaxValue. IMultiplicativeIdentity<TSelf,TResult> Exposes the concept of (x * T.MultiplicativeIdentity) == x.
  • 17.
    Operator Interfaces Interface nameDefined operators IAdditionOperators<TSelf,TOther,TResult> x + y IBitwiseOperators<TSelf,TOther,TResult> x & y, x | y, x ^ y, and ~x IComparisonOperators<TSelf,TOther,TResult> x < y, x > y, x <= y, and x >= y IDecrementOperators<TSelf> --x and x-- IDivisionOperators<TSelf,TOther,TResult> x / y IEqualityOperators<TSelf,TOther,TResult> x == y and x != y IIncrementOperators<TSelf> ++x and x++ IModulusOperators<TSelf,TOther,TResult> x % y IMultiplyOperators<TSelf,TOther,TResult> x * y IShiftOperators<TSelf,TOther,TResult> x << y and x >> y ISubtractionOperators<TSelf,TOther,TResult> x - y IUnaryNegationOperators<TSelf,TResult> -x IUnaryPlusOperators<TSelf,TResult> +x
  • 18.
    Function Parsing Interfaces nterfacename Description IExponentialFunctions<TSelf> Exposes exponential functions supporting e^x, e^x - 1, 2^x, 2^x - 1, 10^x, and 10^x - 1. IHyperbolicFunctions<TSelf> Exposes hyperbolic functions supporting acosh(x), asinh(x), atanh(x), cosh(x), sinh(x), and tanh(x). ILogarithmicFunctions<TSelf> Exposes logarithmic functions supporting ln(x), ln(x + 1), log2(x), log2(x + 1), log10(x), and log10(x + 1). IPowerFunctions<TSelf> Exposes power functions supporting x^y. IRootFunctions<TSelf> Exposes root functions supporting cbrt(x) and sqrt(x). ITrigonometricFunctions<TSelf> Exposes trigonometric functions supporting acos(x), asin(x), atan(x), cos(x), sin(x), and tan(x).
  • 19.
    Demo Let’s see thefuture with .NET 7
  • 20.
    The missing «type»in strongly typed languages...
  • 21.
    Demo Let’s see howfast are values types
  • 22.
    Math with valuetypes • Another «missing in action»: implicit operator and operator overloading • Do you remember that you can overload aritmetic operators to use your value types? • And that you can cast from scalars to structural types (also reference types)? • Do you know that all of these can become a great math?
  • 23.
  • 24.
    Let’s remember physics •Volt+Volt=Volt (you can add or subtract homogeneus values) • Volt*Ampere=Ampere*Volt=Watt (multiplication is commutative) • Watt/Ampere != Ampere/Watt (division is not commutative) • ...and remember the plain good old ToString method!
  • 25.
    Innovation in JSONParsing • Some new features • Contract customization gives you more control over how types are serialized and deserialized. For more information, see Customize a JSON contract. • Polymorphic serialization for user-defined type hierarchies. For more information, see Serialize properties of derived classes. • Support for required members, which are properties that must be present in the JSON payload for deserialization to succeed. For more information, see Required properties.
  • 26.
  • 27.
    Not let’s enterinto the scripting world
  • 28.
    «Scripting» in .NET •Physics and math is about customizing a job (because you have to «express» some laws) • You have to express calculations • A couple of choices: • CSharp Scripting (Roslyn) • ANTLR
  • 29.
  • 30.
  • 31.
    Where Math andscience and code met 30yr ago • Mathematica! From Stephen Wolfram! • Now we have it also in .NET • Notebooks • Kernels
  • 32.
    Demo Write a kernelfor your lang
  • 33.
  • 34.
    Conclusion • Math canbe a lot of fun • .NET can be a lot of fun in the science area