none
Why Memory and ReadOnlyMemory are struct? RRS feed

  • Question

  • Why the types Memory and ReadOnlyMemory are struct instead of class? As they generally are used on asynchronous calls and are moved to the heap.
    Tuesday, June 8, 2021 2:18 PM

All replies

  • Hi

    Span<Message> valSpan stackalloc Message[1] ;
    valSpan[0] =Message {Header = 123, Data = 456 } ;
    Span<byte> bytes = MemoryMarshal.Cast<Message, byte>(valSpan); // has a length of 8
    Note that I am using Span<T> here. You can do much the same with Memory<T> if necessary - but you need a support array or similar, which will usually require allocation - if not the array, then a custom MemoryManager<T> :

    var arr = new message[1] ;
    arr[0] = new message {Header = 123, Data = 456 } ;
    Memory<byte> bytes = MemoryMarshal.Cast<Message, byte>(arr); // has a length of 8
    Essentially, you are very close here:

    MemoryMarshal.CreateReadOnlySpan(ref message, 1)
    The trick, however, is to use MemoryMarshal.Cast<TFrom

    • Proposed as answer by Serocan67 Sunday, January 16, 2022 10:42 PM
    • Unproposed as answer by Serocan67 Sunday, January 16, 2022 10:43 PM
    • Proposed as answer by Serocan67 Sunday, January 16, 2022 10:43 PM
    • Unproposed as answer by Serocan67 Sunday, January 16, 2022 10:44 PM
    • Proposed as answer by Serocan67 Sunday, January 16, 2022 10:44 PM
    Tuesday, June 8, 2021 2:42 PM
  • The only significant difference between a structure and a class is the default status of members.  In a structure, they are public while in a class they are private.

    Declared objects do not occupy space on the heap.   Only allocated (via new, malloc, etc) objects do.

    For obvious reasons, read only memory can only bu used for declared object defined as const or things like string literals which are non-modifiable.

    Thursday, June 10, 2021 12:26 AM
  • Have you got the answer?
    Saturday, June 26, 2021 6:44 AM
  • Is your problem solved well?
    Monday, June 28, 2021 3:07 AM
  • It wasn't only that though, there were a variety of reasons why they didn't take the alternate route, and given the facts.
    Monday, January 10, 2022 9:58 AM
  • Structs have lower overhead than classes and perform better. Since the goal of the Memory, ReadOnlyMemory, Span, and ReadOnlySpan structs is to help improve code performance, it would be kind of counter productive to use a high overhead mechanism.
    • Edited by Mark_Jonas Tuesday, January 11, 2022 12:03 AM edit
    Tuesday, January 11, 2022 12:02 AM
  • In fact, the difference between class and structural is not very big. In structural members, by default, members are public, and in class ones, they are private. That's all :) And recently I started to get involved in gaming. I find the top games on romsify.com and then I hang out for hours. It helps me to relax after long programming :)
    Tuesday, January 11, 2022 11:17 PM
  • Hello, Structs have lower overhead than classes and perform better. Since the goal of the Memory, ReadOnlyMemory, Span, and ReadOnlySpan structs is to help improve code performance, it would be kind of counter productive to use a high overhead mechanism.
    Thursday, January 13, 2022 12:55 PM
  • Why am I having issues running some games on my PC when there is nothing wrong with the system?
    Saturday, January 15, 2022 7:29 PM
  • It wasn't only that though, there's a variety of reasons why they didn't take the alternate route, and given facts change their direction at that time.
    Wednesday, January 19, 2022 5:58 AM
  • The difference is that structs have lower overhead and perform better. 
    Monday, January 24, 2022 7:57 PM
  • There may be a variety of reasons they'd likely have faced lawsuits from those who's apps they'd broken.



    • Edited by KassaCraven Friday, February 4, 2022 5:17 AM
    Friday, February 4, 2022 5:17 AM
  • The object type from which the contiguous region of memory will be read.


    Tuesday, February 8, 2022 6:27 PM
  • I enjoy the efforts you have put in this, thanks for all the great articles.
    Wednesday, February 9, 2022 9:58 AM
  • Even though your Windows 10 device comes with a Microsoft Defender, you need an antivirus for system protection. With McAfee from mcafee.com/activate, you get additional security features to block unwanted programs and online threats.




    • Edited by johnmartin92 Wednesday, February 9, 2022 10:01 AM
    Wednesday, February 9, 2022 10:00 AM
  • ReadOnlyMemory<T>(T[])
    Creates a new memory region over the entirety of the target array.

    ReadOnlyMemory<T>(T[], Int32, Int32)
    Creates a new memory region over the portion of the target array beginning at a specified position and including a specified number of elements.
    Wednesday, February 9, 2022 1:28 PM
  • Structs have lower overhead than classes and perform better. Since the goal of the Memory, ReadOnlyMemory, Span, and ReadOnlySpan structs is to help improve code performance, it would be kind of counter productive to use a high overhead mechanism.
    Monday, February 14, 2022 5:42 AM
  • Why use a struct instead of a class?
    Tuesday, February 15, 2022 11:07 AM
  • Thanks you so much for your help....
    Saturday, February 19, 2022 5:03 AM
  • As struct is the data type that represents the data structures. Struct includes a constants, fields, methods, parameterized constructor, static constructor, and properties as well. 

    A class is the category of object that defines all the common properties of different object that belongs to it.

    Structs are stored in a stack whereas Classes are stored on the heap. Might for this reasons Memory and Read Only Memory are used for object declaration.

    Friday, February 25, 2022 7:42 AM
  • Is your problem solved well?
    Friday, March 18, 2022 6:33 AM
  • As struct is the data type that represents the data structures. Struct includes a constants, fields, methods, parameterized constructor, static constructor, and properties as well. 

    A class is the category of object that defines all the common properties of different object that belongs to it.

    Structs are stored in a stack whereas Classes are stored on the heap. Might for this reasons Memory and Read Only Memory are used for object declaration.

    Great, thanks for this
    Friday, March 18, 2022 9:30 AM