1

From what I've noticed in C# when a byte has its max value (255) then adding a value, for example 10, you get a byte with value of 9.

byte number = byte.MaxValue;
number += 10;
Console.WriteLine(number.ToString()); // prints 9

To get around that problem I wrote the Add extention method.

static void Add(this byte b, byte value)
{
  int result = b + value;
  if (result.IsInByteRange())
  {
   b = (byte)result;
   Console.WriteLine(b.ToString());
  }
  else
  {
   b = byte.MaxValue;
  }
}

static bool IsInByteRange(this int i)
{
  var largerEqualsMin = i >= byte.MinValue;
  var lesserEqualsMax = i <= byte.MaxValue;
  return largerEqualsMin && lesserEqualsMax;
}

While debugging I can see that the b inside Add() is the prefered value but if I execute the bellow:

byte number = 0;
number.Add(10);
Console.WriteLine("Calling from Main() "+ number.ToString());

The output is:

Calling from Add() 10
Calling from Main() 0

Why at the end the result is zero?

Themelis
  • 4,048
  • 2
  • 21
  • 45
  • Does https://stackoverflow.com/questions/2618597/impossible-to-use-ref-and-out-for-first-this-parameter-in-extension-methods or https://stackoverflow.com/questions/2326734/how-can-i-get-an-extension-method-to-change-the-original-object sounds like what you are asking about? – Alexei Levenkov Sep 30 '19 at 21:37
  • I thought it could be a refference problem but at the end of the day in when you specify the `this byte b` parameter it's like acting on the reference of the object, isn't it @AlexeiLevenkov? – Themelis Sep 30 '19 at 21:40
  • Clearly your assumption was wrong :D, but thanks for pointing that out. So it's a refference problem, is it possible to solve that through an extention method? – Themelis Sep 30 '19 at 21:47
  • 1
    `byte`, like other primitive types, is a value type. That means that when it's used, the _value_ of the type is _copied_. The original value remains unchanged. Your extension method takes the value as a parameter, but this is just a _copy_ of the original value. So the method modifies the new copy, but leaves the original unchanged. The original value was `0`, and remains `0` even after the call to the method. – Peter Duniho Sep 30 '19 at 21:47
  • 1
    _"when you specify the this byte b parameter it's like acting on the reference of the object"_ -- no, not really. Value types don't have references unless they are passed by-reference, which extension methods don't do (the `this` parameter is always passed by-value). – Peter Duniho Sep 30 '19 at 21:47
  • Ok I thought that the `this` in extention methods means its passed by reference (and that's why I concluded that the `ref` keyword is forbidden) @PeterDuniho – Themelis Sep 30 '19 at 21:49
  • 1
    _"is it possible to solve that through an extention method?"_ -- is what possible to solve? If you're asking whether you can pass the value by reference, the answer is no, not as the `this` parameter anyway (you could reverse the syntax of your extension so that you called it like `10.Add(ref number)`, but IMHO that would be an even bigger mistake than pinning the result of the addition to the max value. Which IMHO is in fact a big mistake, at least as a general approach (maybe some domain-specific scenario requires it, but then you should use domain-specific syntax). – Peter Duniho Sep 30 '19 at 21:49
  • Ok I will create a normal method instead. Thanks guys – Themelis Sep 30 '19 at 21:51
  • You should probably also read through https://stackoverflow.com/questions/29606475/c-sharp-pass-by-value-vs-pass-by-reference and https://stackoverflow.com/questions/3615498/confusion-with-value-type-and-reference-type-in-c-sharp. It's possible you understand the distinction between "reference type", "value type", "passing by reference", and "passing by value", but these are things that many programmers misunderstand, and given your confusion about the extension method, possibly you do as well. These are reasonably good SO questions and answers that address those distinctions. – Peter Duniho Sep 30 '19 at 21:52
  • Will do @PeterDuniho, thanks a lot! – Themelis Sep 30 '19 at 21:57

0 Answers0