0
 struct MyParameters
    {
        public Bitmap resizedBitmap { get; set; }
        public Bitmap originalBitmap { get; set; }
        public int ratio {get; set;}
        public bool setdata {get; set;}
    }

Can i use struct to pass several images to a method instead of a class? Is there any performance issue ? i referred this SO answer.It mentions that use struct only if It has an instance size smaller than 16 bytes.

https://stackoverflow.com/a/85656/848968

Please advice

techno
  • 6,100
  • 16
  • 86
  • 192
  • I don't have anything concrete, but my thought is that the struct only really holds a pointer to the `Bitmap` instance, so it's probably fine. – ProgrammingLlama May 28 '21 at 05:00
  • As long “several” <= 2 you should be fine... but I don’t see much benefits of writing long explanation why you pick struct just to save one parameter – Alexei Levenkov May 28 '21 at 05:03
  • @AlexeiLevenkov There are lot of other integer and boolean parameters.I'm just concerned about the Images.I have updated the question.If are there like 4 images ? then how does it affect the performance ? – techno May 28 '21 at 05:47
  • @techno, based on the link which you share in the OP, according to "supercat"'s comment in the accepted answer is In designing the JIT compiler, Microsoft decided to optimize the code for copying structs which are 16 bytes or smaller; this means that copying a 17-byte struct may be significantly slower than copying a 16-byte struct. I see no particular reason to expect Microsoft to extend such optimizations to larger structs, but it's important to note that while 17-byte structurs may be slower to copy than 16-byte structs. – Biju Kalanjoor May 28 '21 at 05:51
  • @BijuKalanjoor okay.I'm not copying anything .. just using it to pass parameters.Do you suggest to use a class instead? – techno May 28 '21 at 06:02
  • @techno, when you pass a struct as parameter the framework copies the values, because its immutable. so a struct contains more byte the copying will slower that a reference type (reference type only copies the refrence) – Biju Kalanjoor May 28 '21 at 06:08
  • 1
    @BijuKalanjoor: Immutability has nothing to do with this, and the struct that the OP has shown is *not* immutable. It's not copied because it's immutable - it's copied because that's how value types work. – Jon Skeet May 28 '21 at 06:27
  • @JonSkeet What should i use in this case to pass the parameter? – techno May 28 '21 at 08:07
  • 2
    I'd *default* to using a class, to be honest. (I'd generally avoid mutable structs, and I'd also pay attention to .NET naming conventions.) If you're worried about performance though, you should make sure you have concrete performance requirements and measurements against those requirements. Write the simplest code that works, and see how well that performs - only start making your code harder to read/maintain for the sake of performance when you can prove that the tradeoff is worth it. – Jon Skeet May 28 '21 at 08:21

1 Answers1

0

Points to check while choosing class and struct larger than 16 bytes

  1. reference type assignments copy the reference, whereas value type assignments copy the entire value. Therefore, assignments of large reference types are cheaper than assignments of large value types
  2. boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree. This can take up to 20 times longer than a simple reference assignment
Biju Kalanjoor
  • 532
  • 1
  • 6
  • 12