Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error CS0171 LayoutKind.Explicit #56512

Closed
sgf opened this issue Sep 18, 2021 · 2 comments
Closed

error CS0171 LayoutKind.Explicit #56512

sgf opened this issue Sep 18, 2021 · 2 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@sgf
Copy link

sgf commented Sep 18, 2021

Version Used:
.net 5,C# 9.0
Steps to Reproduce:

  [StructLayout(LayoutKind.Explicit, Pack = 1)]
   public struct UniUint32 {

       public UniUint32(uint _value) {
           value = _value;
       }
       public UniUint32(byte _b1,byte _b2,byte _b3,byte _b4) {
           b1=_b1;
           b2=_b2;
           b3=_b3;
           b4=_b4;
       }

       [FieldOffset(0)]
       public uint value;
       [FieldOffset(0)]
       public byte b1;
       [FieldOffset(1)]
       public byte b2;
       [FieldOffset(2)]
       public byte b3;
       [FieldOffset(3)]
       public byte b4;
       public static implicit operator uint(UniUint32 ip) => ip.value;
       public static unsafe implicit operator UniUint32(uint ip) => *((UniUint32*)&ip);
   }

Expected Behavior:
Compiled successfully
Actual Behavior:

Error CS0171 UniUint32.b4 D:\MySrc\DLBSvr\DLBSvr\Net\IPv4.cs 15 Active

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 18, 2021
@PathogenDavid
Copy link
Contributor

Duplicate of #7323 #10319 and dotnet/csharplang#1465

As mentioned in dotnet/csharplang#1465 (comment), if you're targeting .NET 5 You can use Unsafe.SkipInit.

If you're targeting older runtimes call the default this() constructor or use this = default (at the expense of a miniscule amount of runtime overhead.)

@sgf
Copy link
Author

sgf commented Sep 18, 2021

thanks

@sgf sgf closed this as completed Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants