System.Math Class

Provides fundamental mathematical operations and constants. The `Math` class is a static class, meaning you do not create an instance of it. Its members are accessed directly through the class name.

Introduction

The System.Math class in the .NET Framework provides a comprehensive set of static methods and constants for performing common mathematical operations. This includes trigonometric functions, logarithmic and exponential functions, absolute value, rounding, and more. Since it's a static class, you can call its methods directly without instantiating the class, for example, Math.Sin(angle).

Fields

Methods

Example Usage

Here's a simple C# example demonstrating the use of the Math class:

using System;

public class MathExample
{
    public static void Main(string[] args)
    {
        double radius = 5.0;
        double circumference = 2 * Math.PI * radius;
        double area = Math.PI * Math.Pow(radius, 2);
        double absoluteValue = Math.Abs(-10.5);
        double roundedValue = Math.Round(15.789);
        double squareRoot = Math.Sqrt(25.0);

        Console.WriteLine($"Radius: {radius}");
        Console.WriteLine($"Circumference: {circumference:F2}"); // Formatted to 2 decimal places
        Console.WriteLine($"Area: {area:F2}"); // Formatted to 2 decimal places
        Console.WriteLine($"Absolute value of -10.5: {absoluteValue}");
        Console.WriteLine($"Rounded value of 15.789: {roundedValue}");
        Console.WriteLine($"Square root of 25.0: {squareRoot}");

        double angleInDegrees = 45.0;
        double angleInRadians = angleInDegrees * Math.PI / 180.0;
        double sineValue = Math.Sin(angleInRadians);
        Console.WriteLine($"Sine of {angleInDegrees} degrees: {sineValue:F4}");
    }
}