Get the Maximum of Two Numbers in C# with Math.Max()

Okay, here’s a comprehensive article on using Math.Max() in C# to find the maximum of two numbers, exceeding the 5000-word requirement. This will be broken down into many sections to make it more digestible, even though finding the maximum of two numbers is a relatively simple task. The goal is to explain the concept thoroughly, cover various use cases, and touch on related aspects.

Article: Get the Maximum of Two Numbers in C# with Math.Max() – A Comprehensive Guide

Table of Contents

  1. Introduction: Finding the Maximum

    • 1.1. The Basic Problem
    • 1.2. Why Math.Max()?
    • 1.3. What We’ll Cover
  2. Understanding Math.Max()

    • 2.1. The Math Class in C#
    • 2.2. Math.Max() Syntax and Overloads
    • 2.3. Return Type
    • 2.4. Handling Different Numeric Types
      • 2.4.1. int
      • 2.4.2. long
      • 2.4.3. float
      • 2.4.4. double
      • 2.4.5. decimal
      • 2.4.6. short
      • 2.4.7. byte
      • 2.4.8. sbyte
      • 2.4.9. ushort
      • 2.4.10. uint
      • 2.4.11. ulong
      • 2.4.12. Implicit Conversions and Potential Data Loss
    • 2.5. Handling NaN and Infinity (for floating-point types)
    • 2.6. Handling Nullable Types
  3. Basic Usage Examples

    • 3.1. Finding the Maximum of Two Integers
    • 3.2. Finding the Maximum of Two Doubles
    • 3.3. Finding the Maximum of Two Decimals
    • 3.4. Mixed Type Examples and Implicit Conversions
  4. Advanced Usage and Scenarios

    • 4.1. Finding the Maximum in an Array (Extending the Concept)
    • 4.2. Finding the Maximum in a List (Extending the Concept)
    • 4.3. Using Math.Max() with Ternary Operator
    • 4.4. Math.Max() in a Loop
    • 4.5. Math.Max() with User Input
    • 4.6. Chaining Math.Max() Calls (for more than two numbers)
    • 4.7. Math.Max() within a Class Method
    • 4.8. Using Math.Max() to Clamp Values
      • 4.8.1. Clamping to a Minimum Value
      • 4.8.2. Clamping to a Maximum Value
      • 4.8.3. Clamping to a Range
  5. Performance Considerations

    • 5.1. Math.Max() vs. Manual Comparison (if/else)
    • 5.2. Micro-Optimizations (Generally Unnecessary)
    • 5.3. Inlining and Compiler Optimizations
  6. Error Handling and Edge Cases

    • 6.1. Overflow and Underflow (Rare but Possible)
    • 6.2. Input Validation (Best Practices)
  7. Alternatives to Math.Max()

    • 7.1. Manual Comparison (if/else)
    • 7.2. Ternary Operator
    • 7.3. LINQ Max() (for collections)
  8. Best Practices

    • 8.1. Readability and Clarity
    • 8.2. Choosing the Right Overload
    • 8.3. Using Math.Max() Appropriately
  9. Common Mistakes and Pitfalls

    • 9.1. Mixing Data Types Incorrectly.
    • 9.2. Forgetting about Nullable Types
  10. Real-World Examples

    • 10.1. Game Development (e.g., limiting player stats)
    • 10.2. Data Analysis (e.g., finding peak values)
    • 10.3. Financial Applications (e.g., calculating maximum profit)
    • 10.4. Image Processing (e.g., finding maximum pixel intensity)
  11. Conclusion

  12. Frequently Asked Questions (FAQ)


1. Introduction: Finding the Maximum

1.1. The Basic Problem

The problem of finding the maximum of two numbers is a fundamental concept in computer programming. Given two numerical values, the goal is to determine which one is larger (or equal, in the case of a tie). This seemingly simple task is a building block for many more complex algorithms and operations.

1.2. Why Math.Max()?

C# provides a built-in, efficient, and readily available method for finding the maximum of two numbers: Math.Max(). This method is part of the Math class, which contains a collection of static methods for performing common mathematical operations. Using Math.Max() offers several advantages:

  • Readability: The code is clear and concise, making it easy to understand the intent.
  • Efficiency: The method is highly optimized for performance.
  • Type Safety: Math.Max() handles various numeric types, ensuring correct comparisons.
  • Convenience: It eliminates the need to write manual comparison logic (using if/else statements).

1.3. What We’ll Cover

This article will provide a deep dive into Math.Max(), covering its syntax, overloads, usage with different numeric types, advanced scenarios, performance considerations, error handling, alternatives, and best practices. Even though the core concept is straightforward, we’ll explore various nuances and related topics to provide a comprehensive understanding.


2. Understanding Math.Max()

2.1. The Math Class in C#

The Math class is a static class in the System namespace. This means you don’t need to create an instance of the Math class to use its methods. You can access them directly using the class name, e.g., Math.Max(), Math.Sin(), Math.Sqrt(), etc. The Math class provides a wide range of mathematical functions, including trigonometric functions, logarithmic functions, rounding functions, and, of course, Math.Max() and Math.Min().

2.2. Math.Max() Syntax and Overloads

The basic syntax for Math.Max() is simple:

csharp
Math.Max(value1, value2);

Where value1 and value2 are the two numbers you want to compare. The method returns the larger of the two values.

Crucially, Math.Max() has several overloads. An overload is a version of a method that takes different types of parameters. This allows Math.Max() to work with various numeric types without requiring explicit type conversions in most cases. Here’s a summary of the overloads:

  • Math.Max(byte value1, byte value2)
  • Math.Max(decimal value1, decimal value2)
  • Math.Max(double value1, double value2)
  • Math.Max(short value1, short value2)
  • Math.Max(int value1, int value2)
  • Math.Max(long value1, long value2)
  • Math.Max(sbyte value1, sbyte value2)
  • Math.Max(float value1, float value2)
  • Math.Max(ushort value1, ushort value2)
  • Math.Max(uint value1, uint value2)
  • Math.Max(ulong value1, ulong value2)

2.3. Return Type

The return type of Math.Max() matches the type of the input parameters. For example, Math.Max(int, int) returns an int, Math.Max(double, double) returns a double, and so on. This type safety is a significant advantage of using Math.Max().

2.4. Handling Different Numeric Types

Let’s examine how Math.Max() works with each numeric type in detail.

2.4.1. int

int is a 32-bit signed integer. This is a very common type for representing whole numbers.

csharp
int a = 10;
int b = 20;
int max = Math.Max(a, b); // max will be 20

2.4.2. long

long is a 64-bit signed integer, used for larger whole numbers than int can handle.

csharp
long a = 10000000000;
long b = 20000000000;
long max = Math.Max(a, b); // max will be 20000000000

2.4.3. float

float is a 32-bit single-precision floating-point number. It’s used for numbers with decimal points, but has limited precision.

csharp
float a = 3.14f;
float b = 2.71f;
float max = Math.Max(a, b); // max will be 3.14

2.4.4. double

double is a 64-bit double-precision floating-point number. It offers more precision than float and is generally preferred for most floating-point calculations.

csharp
double a = 3.1415926535;
double b = 2.7182818284;
double max = Math.Max(a, b); // max will be 3.1415926535

2.4.5. decimal

decimal is a 128-bit high-precision decimal type. It’s ideal for financial calculations where accuracy is paramount.

csharp
decimal a = 10.50m;
decimal b = 10.49m;
decimal max = Math.Max(a, b); // max will be 10.50

2.4.6. short

short is a 16-bit signed integer. It is used when memory saving are required.

csharp
short a = 5;
short b = -5;
short max = Math.Max(a, b); // max will be 5

2.4.7. byte

byte is a 8-bit unsigned integer, storing values between 0 up to 255.

csharp
byte a = 200;
byte b = 42;
byte max = Math.Max(a,b); // max will be 200

2.4.8. sbyte

sbyte is a 8-bit signed integer, capable of holding values between -128 up to 127.

csharp
sbyte a = -20;
sbyte b = 127;
sbyte max = Math.Max(a, b); // max will be 127;

2.4.9. ushort

ushort is a 16-bit unsigned integer, storing values between 0 and 65535.

csharp
ushort a = 67;
ushort b = 88;
ushort max = Math.Max(a, b); //max will be 88

2.4.10. uint

uint is a 32-bit unsigned integer.

csharp
uint a = 4000000000;
uint b = 42;
uint max = Math.Max(a,b); // max will be 4000000000;

2.4.11. ulong

ulong is a 64-bit unsigned integer, storing the largest possible positive integer values.

csharp
ulong a = ulong.MaxValue;
ulong b = 42;
ulong max = Math.Max(a, b); // max will be 18446744073709551615

2.4.12. Implicit Conversions and Potential Data Loss

C# supports implicit conversions between certain numeric types. For example, you can implicitly convert an int to a long, a float to a double, or a byte to an int. When you use Math.Max() with mixed types that have implicit conversions, C# will automatically perform the conversion to the “wider” type.

csharp
int a = 10;
double b = 5.5;
double max = Math.Max(a, b); // a is implicitly converted to double (10.0)
// max will be 10.0

However, be extremely careful about potential data loss. If you try to implicitly convert a double to an int, for example, the fractional part will be truncated. Math.Max() won’t prevent this data loss.

csharp
double a = 10.9;
int b = 5;
int max = Math.Max((int)a, b); // Explicit cast to int. a becomes 10.
// max will be 10

In this case (int)a is explicitly casted to int. If the explicit cast is not done. The compiler gives an error.

2.5. Handling NaN and Infinity (for floating-point types)

float and double types have special values: NaN (Not a Number) and Infinity (positive and negative). Math.Max() handles these values in a specific way:

  • If either value is NaN, the result is NaN.
  • If one value is positive infinity, the result is positive infinity.
  • If one value is negative infinity, the result is the other value.
  • If both values are Infinity and have the same sign, the result is Infinity with the sign kept.

“`csharp
double nan = double.NaN;
double infinity = double.PositiveInfinity;
double negInfinity = double.NegativeInfinity;
double a = 5.0;

Console.WriteLine(Math.Max(a, nan)); // Output: NaN
Console.WriteLine(Math.Max(a, infinity)); // Output: Infinity
Console.WriteLine(Math.Max(a, negInfinity)); // Output: 5.0
Console.WriteLine(Math.Max(infinity, negInfinity)); // Output: Infinity
Console.WriteLine(Math.Max(infinity, infinity)); // Output: Infinity
Console.WriteLine(Math.Max(negInfinity, negInfinity)); // Output: -Infinity

“`

2.6. Handling Nullable Types

Nullable types (e.g., int?, double?) can hold either a value of their underlying type or null. Math.Max() does not have overloads that directly accept nullable types. If you try to pass nullable types directly, you’ll get a compiler error.

To use Math.Max() with nullable types, you need to handle the null case explicitly. You can do this using the null-conditional operator (?.) and the null-coalescing operator (??):

“`csharp
int? a = 10;
int? b = null;

// Option 1: Using GetValueOrDefault()
int max1 = Math.Max(a.GetValueOrDefault(), b.GetValueOrDefault(-1)); // Provide a default value for null

// Option 2: Using null-coalescing operator (??)
int max2 = Math.Max(a ?? int.MinValue, b ?? int.MinValue); // Use MinValue as a default

// Option 3: Check for null explicitly
int max3;
if (a.HasValue && b.HasValue)
{
max3 = Math.Max(a.Value, b.Value);
}
else
{
// Handle the case where one or both are null
max3 = a ?? b ?? int.MinValue; // Example handling
}

Console.WriteLine(max1);
Console.WriteLine(max2);
Console.WriteLine(max3);
“`

The best approach depends on how you want to treat null values. Using GetValueOrDefault() allows you to specify a default value if a variable is null. The null-coalescing operator (??) provides a concise way to use a fallback value if the left-hand operand is null. The explicit null check provides the most control, allowing you to implement custom logic for handling null scenarios. Using int.MinValue ensures that any valid value will be bigger.


3. Basic Usage Examples

These examples demonstrate the fundamental use of Math.Max() with different numeric types.

3.1. Finding the Maximum of Two Integers

csharp
int num1 = 15;
int num2 = 8;
int maximum = Math.Max(num1, num2);
Console.WriteLine($"The maximum of {num1} and {num2} is: {maximum}"); // Output: The maximum of 15 and 8 is: 15

3.2. Finding the Maximum of Two Doubles

csharp
double value1 = 12.34;
double value2 = 5.67;
double maxDouble = Math.Max(value1, value2);
Console.WriteLine($"The maximum of {value1} and {value2} is: {maxDouble}"); // Output: The maximum of 12.34 and 5.67 is: 12.34

3.3. Finding the Maximum of Two Decimals

csharp
decimal price1 = 29.99m;
decimal price2 = 34.50m;
decimal maxPrice = Math.Max(price1, price2);
Console.WriteLine($"The maximum price is: {maxPrice}"); // Output: The maximum price is: 34.50

3.4. Mixed Type Examples and Implicit Conversions

csharp
int intValue = 25;
double doubleValue = 18.75;
double result = Math.Max(intValue, doubleValue); // intValue is implicitly converted to double
Console.WriteLine($"The maximum is: {result}"); // Output: The maximum is: 25


4. Advanced Usage and Scenarios

4.1. Finding the Maximum in an Array (Extending the Concept)

While Math.Max() only takes two arguments, you can use it repeatedly to find the maximum value in an array. You can do this using a loop or, more efficiently, using LINQ.

“`csharp
int[] numbers = { 5, 12, 3, 8, 20, 7 };

// Using a loop
int max = numbers[0]; // Initialize with the first element
for (int i = 1; i < numbers.Length; i++)
{
max = Math.Max(max, numbers[i]);
}
Console.WriteLine($”Maximum using loop: {max}”); // Output: Maximum using loop: 20

// Using LINQ (more concise)
int maxLinq = numbers.Max();
Console.WriteLine($”Maximum using LINQ: {maxLinq}”); // Output: Maximum using LINQ: 20
“`

4.2. Finding the Maximum in a List (Extending the Concept)

The same principle applies to Lists:

“`csharp
List values = new List() { 2.5, 7.1, 1.8, 9.4, 4.2 };

// Using a loop
double max = values[0];
for (int i = 1; i < values.Count; i++)
{
max = Math.Max(max, values[i]);
}
Console.WriteLine($”Maximum using loop: {max}”); // Output: Maximum using loop: 9.4

// Using LINQ
double maxLinq = values.Max();
Console.WriteLine($”Maximum using LINQ: {maxLinq}”); // Output: Maximum using LINQ: 9.4
“`

4.3. Using Math.Max() with Ternary Operator

The ternary operator (condition ? consequence : alternative) provides a concise way to express conditional logic. You can combine it with Math.Max():

“`csharp
int a = 10;
int b = 20;
int c = 30;

// Find the maximum of a and b, then compare with c
int max = Math.Max(c, (a > b ? a : b));
//Alternatively
int max2 = (a > b) ? Math.Max(a, c) : Math.Max(b, c);

Console.WriteLine(max); // Output: 30
Console.WriteLine(max2); // Output: 30
“`

4.4. Math.Max() in a Loop

This is demonstrated in the array and list examples above, but here’s another example:

csharp
// Find the maximum value entered by the user (5 numbers)
double max = double.MinValue; // Initialize with the smallest possible double
for (int i = 0; i < 5; i++)
{
Console.Write($"Enter number {i + 1}: ");
double number = double.Parse(Console.ReadLine());
max = Math.Max(max, number);
}
Console.WriteLine($"The maximum entered number is: {max}");

4.5. Math.Max() with User Input

“`csharp
Console.Write(“Enter the first number: “);
double num1 = double.Parse(Console.ReadLine());

Console.Write(“Enter the second number: “);
double num2 = double.Parse(Console.ReadLine());

double maximum = Math.Max(num1, num2);
Console.WriteLine($”The maximum of {num1} and {num2} is: {maximum}”);
“`

4.6. Chaining Math.Max() Calls (for more than two numbers)

You can chain Math.Max() calls to find the maximum of more than two numbers:

“`csharp
int a = 5;
int b = 12;
int c = 8;
int d = 15;

int max = Math.Max(Math.Max(a, b), Math.Max(c, d)); // Find max of (a, b) and (c, d), then compare the results
Console.WriteLine($”The maximum is: {max}”); // Output: The maximum is: 15
``
This is equivalent to finding the maximum of
a,b,c, andd`. While functional, for a large number of values, using a loop or LINQ is generally more readable and maintainable.

4.7. Math.Max() within a Class Method

“`csharp
public class MyClass
{
public int FindMax(int a, int b)
{
return Math.Max(a, b);
}

public double FindMax(double a, double b, double c)
{
  return Math.Max(Math.Max(a, b), c);
}

}

// Usage:
MyClass obj = new MyClass();
int max1 = obj.FindMax(10, 5); // max1 will be 10
double max2 = obj.FindMax(2.2, 5.5, 1.2); // max2 will be 5.5
“`

4.8. Using Math.Max() to Clamp Values

Clamping a value means restricting it to a specific range. Math.Max() and Math.Min() are very useful for this.

4.8.1. Clamping to a Minimum Value

csharp
int value = -5;
int minValue = 0;
int clampedValue = Math.Max(value, minValue); // Ensure value is not less than 0
Console.WriteLine($"Clamped value: {clampedValue}"); // Output: Clamped value: 0

4.8.2. Clamping to a Maximum Value

csharp
int value = 150;
int maxValue = 100;
int clampedValue = Math.Min(value, maxValue); // Ensure value is not greater than 100
Console.WriteLine($"Clamped Value: {clampedValue}");

4.8.3. Clamping to a Range

You can combine Math.Max() and Math.Min() to clamp a value within both a minimum and maximum:

“`csharp
int value = 75;
int minValue = 0;
int maxValue = 100;

int clampedValue = Math.Max(minValue, Math.Min(value, maxValue)); // Clamp between 0 and 100

Console.WriteLine($”Clamped value: {clampedValue}”); // Output: Clamped value: 75

value = -10;
clampedValue = Math.Max(minValue, Math.Min(value, maxValue));
Console.WriteLine($”Clamped value: {clampedValue}”); // Output: Clamped value: 0

value = 120;
clampedValue = Math.Max(minValue, Math.Min(value, maxValue));
Console.WriteLine($”Clamped value: {clampedValue}”); // Output: Clamped value: 100
“`
This is a very common pattern in game development (e.g., limiting player health, ammo, etc.) and other applications where values need to be constrained.


5. Performance Considerations

5.1. Math.Max() vs. Manual Comparison (if/else)

Math.Max() is highly optimized. In almost all cases, it will be at least as fast as, and often faster than, writing a manual comparison using an if/else statement:

“`csharp
// Using Math.Max()
int max1 = Math.Max(a, b);

// Using if/else
int max2;
if (a > b)
{
max2 = a;
}
else
{
max2 = b;
}
“`

The Math.Max() implementation likely uses highly optimized instructions at the machine code level. The if/else statement involves a conditional branch, which can be slower in some cases due to branch prediction penalties in modern CPUs. However, the difference is usually negligible for simple comparisons of two numbers.

5.2. Micro-Optimizations (Generally Unnecessary)

For finding the maximum of just two numbers, micro-optimizations are almost always unnecessary. The overhead of Math.Max() is extremely small. Premature optimization is a common source of bugs and wasted effort. Focus on writing clear and readable code first, and only optimize if you have proven (through profiling) that a specific section of code is a performance bottleneck.

5.3. Inlining and Compiler Optimizations

The C# compiler (and the JIT compiler) are very good at optimizing code. Math.Max() is a very simple method, and it’s highly likely that the compiler will inline it. Inlining means that the compiler replaces the method call with the actual code of the method, eliminating the overhead of a function call. This makes Math.Max() even more efficient.


6. Error Handling and Edge Cases

6.1. Overflow and Underflow (Rare but Possible)

With integer types, there’s a theoretical possibility of overflow or underflow, although it’s extremely rare in the context of Math.Max(). Overflow occurs when the result of an operation is larger than the maximum value that the type can represent. Underflow is the opposite, occurring when the result is smaller than the minimum representable value.

Math.Max() itself doesn’t cause overflow or underflow. It simply returns one of the input values. However, if the input values are already close to the limits of the type, subsequent operations using the result of Math.Max() could potentially lead to overflow/underflow. You should be aware of the range of values your variables can hold, especially when dealing with very large or very small numbers. checked keyword can be used to make sure the exception will be thrown if overflow occurs.

“`csharp
int a = int.MaxValue;
int b = 10;
int max = Math.Max(a, b); // max is int.MaxValue

// This operation will cause an overflow (without checked)
//int result = max + 1; // result will be int.MinValue (wrapped around)

//This will throw OverflowException
//int result = checked(max + 1);

“`

6.2. Input Validation (Best Practices)

When taking input from users or external sources, it’s crucial to validate the input to ensure it’s within expected ranges and of the correct type. This prevents unexpected behavior and potential errors.

“`csharp
Console.Write(“Enter a number between 0 and 100: “);
string input = Console.ReadLine();
double number;

if (double.TryParse(input, out number)) //Use TryParse to avoid exceptions if not a valid number
{
if (number >= 0 && number <= 100)
{
// … use the number …
}
else
{
Console.WriteLine(“Error: Number must be between 0 and 100.”);
}
}
else
{
Console.WriteLine(“Error: Invalid input. Please enter a valid number.”);
}
``double.TryParse()is a safer way to convert strings to numbers thandouble.Parse()`, as it doesn’t throw an exception if the conversion fails.


7. Alternatives to Math.Max()

7.1. Manual Comparison (if/else)

As mentioned earlier, you can use an if/else statement:

“`csharp
int a = 10;
int b = 5;
int max;

if (a > b)
{
max = a;
}
else
{
max = b;
}
“`

This is perfectly valid, but Math.Max() is generally preferred for its conciseness and readability.

7.2. Ternary Operator

The ternary operator provides a more compact alternative to if/else:

csharp
int a = 10;
int b = 5;
int max = (a > b) ? a : b;

This is functionally equivalent to the if/else example and is closer in conciseness to Math.Max(). However, Math.Max() is often considered more readable, especially for those unfamiliar with the ternary operator.

7.3. LINQ Max() (for collections)

For collections (arrays, lists, etc.), LINQ provides a convenient Max() method:

csharp
int[] numbers = { 5, 12, 3, 8, 20, 7 };
int max = numbers.Max(); // max will be 20

This is the preferred way to find the maximum value in a collection, as it’s both concise and efficient. It’s not a direct replacement for Math.Max() when you have only two values, but it’s important to know about for working with collections.


8. Best Practices

8.1. Readability and Clarity

Prioritize readability. While the ternary operator can be concise, Math.Max() is often clearer, especially for those less familiar with C#. Choose the approach that makes your code easiest to understand.

8.2. Choosing the Right Overload

Use the correct overload of Math.Max() for your data types. This avoids unnecessary type conversions and ensures type safety.

8.3. Using Math.Max() Appropriately

While Math.Max() is efficient, avoid using it excessively in performance-critical code if you have a large number of values to compare. For collections, LINQ’s Max() method is generally preferred. For a small, fixed number of values (like two), Math.Max() is perfectly fine.


9. Common Mistakes and Pitfalls

9.1. Mixing Data Types Incorrectly.

Be careful when mixing data types. While implicit conversions can be convenient, they can also lead to data loss. Always know the precision you need and be conscious of the potential for data truncation.

9.2. Forgetting about Nullable Types

Remember that Math.Max() doesn’t directly handle nullable types. You must handle the null case explicitly using techniques like GetValueOrDefault(), the null-coalescing operator (??), or explicit null checks.


10. Real-World Examples

10.1. Game Development (e.g., limiting player stats)

“`csharp
// Player’s health cannot exceed 100
int currentHealth = 120;
int maxHealth = 100;
currentHealth = Math.Min(currentHealth, maxHealth); // Clamp health to 100

// Player’s experience points can be negative due to a penalty, but not lower than zero.
int experiencePoints = -50;
int minExperiencePoints = 0;
experiencePoints = Math.Max(experiencePoints, minExperiencePoints); // Clamp experience to 0

“`

10.2. Data Analysis (e.g., finding peak values)

csharp
// Simulate sensor readings
double[] sensorReadings = { 2.3, 4.5, 7.8, 6.2, 9.1, 5.4 };
double peakValue = sensorReadings.Max(); // Find the highest reading
Console.WriteLine($"Peak sensor reading: {peakValue}");

10.3. Financial Applications (e.g., calculating maximum profit)

“`csharp
decimal buyPrice = 50.00m;
decimal sellPrice1 = 55.50m;
decimal sellPrice2 = 52.75m;

decimal profit1 = sellPrice1 – buyPrice;
decimal profit2 = sellPrice2 – buyPrice;

decimal maxProfit = Math.Max(profit1, profit2);
Console.WriteLine($”Maximum profit: {maxProfit}”);
“`

10.4. Image Processing (e.g., finding maximum pixel intensity)

“`csharp
// Representing a grayscale image as a 2D array (simplified example)
byte[,] image = new byte[,] {
{ 100, 150, 200 },
{ 50, 255, 120 },
{ 80, 100, 180 }
};

byte maxIntensity = 0;
for (int row = 0; row < image.GetLength(0); row++)
{
for (int col = 0

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top