Skip to content

Commit 36401bd

Browse files
authored
fix pack examples (#9885)
1 parent ad7cb73 commit 36401bd

File tree

11 files changed

+202
-182
lines changed

11 files changed

+202
-182
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//Example.Main();
2+
//Example1.Main();
3+
//Example2.Main();
4+
//Example3.Main();
5+
//Example4.Main();
6+
//Example5.Main();
7+
//Example6.Main();
8+
Example7.Main();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net481</TargetFramework>
6+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7+
<LangVersion>11</LangVersion>
8+
</PropertyGroup>
9+
10+
</Project>

snippets/csharp/System.Runtime.InteropServices/StructLayoutAttribute/Pack/packex0.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33

44
struct ExampleStruct
55
{
6-
public byte b1;
7-
public byte b2;
8-
public int i3;
6+
public byte b1;
7+
public byte b2;
8+
public int i3;
99
}
1010
// </Snippet1>
1111

1212
public class Example
1313
{
14-
public unsafe static void Main()
15-
{
16-
17-
ExampleStruct ex = new ExampleStruct();
18-
byte* addr = (byte*) &ex;
19-
Console.WriteLine("Size: {0}", sizeof(ExampleStruct));
20-
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
21-
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
22-
Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
23-
}
14+
public unsafe static void Main()
15+
{
16+
ExampleStruct ex = new();
17+
byte* addr = (byte*)&ex;
18+
Console.WriteLine("Size: {0}", sizeof(ExampleStruct));
19+
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
20+
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
21+
Console.WriteLine("i3 Offset: {0}", (byte*)&ex.i3 - addr);
22+
}
2423
}

snippets/csharp/System.Runtime.InteropServices/StructLayoutAttribute/Pack/packex1.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22
using System;
33
using System.Runtime.InteropServices;
44

5-
[StructLayout(LayoutKind.Sequential, Pack=0)]
6-
struct ExampleStruct
5+
[StructLayout(LayoutKind.Sequential, Pack = 0)]
6+
struct ExampleStruct1
77
{
8-
public byte b1;
9-
public byte b2;
10-
public int i3;
8+
public byte b1;
9+
public byte b2;
10+
public int i3;
1111
}
1212

13-
public class Example
13+
public class Example1
1414
{
15-
public unsafe static void Main()
16-
{
17-
18-
ExampleStruct ex = new ExampleStruct();
19-
byte* addr = (byte*) &ex;
20-
Console.WriteLine("Size: {0}", sizeof(ExampleStruct));
21-
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
22-
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
23-
Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
24-
}
15+
public unsafe static void Main()
16+
{
17+
ExampleStruct1 ex = new();
18+
byte* addr = (byte*)&ex;
19+
Console.WriteLine("Size: {0}", sizeof(ExampleStruct1));
20+
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
21+
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
22+
Console.WriteLine("i3 Offset: {0}", (byte*)&ex.i3 - addr);
23+
}
2524
}
2625
// The example displays the following output:
2726
// Size: 8
2827
// b1 Offset: 0
2928
// b2 Offset: 1
3029
// i3 Offset: 4
31-
// </Snippet2>
30+
// </Snippet2>

snippets/csharp/System.Runtime.InteropServices/StructLayoutAttribute/Pack/packex2.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22
using System;
33
using System.Runtime.InteropServices;
44

5-
[StructLayout(LayoutKind.Sequential, Pack=2)]
6-
struct ExampleStruct
5+
[StructLayout(LayoutKind.Sequential, Pack = 2)]
6+
struct ExampleStruct2
77
{
8-
public byte b1;
9-
public byte b2;
10-
public int i3;
8+
public byte b1;
9+
public byte b2;
10+
public int i3;
1111
}
1212

13-
public class Example
13+
public class Example2
1414
{
15-
public unsafe static void Main()
16-
{
17-
18-
ExampleStruct ex = new ExampleStruct();
19-
byte* addr = (byte*) &ex;
20-
Console.WriteLine("Size: {0}", sizeof(ExampleStruct));
21-
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
22-
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
23-
Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
24-
}
15+
public unsafe static void Main()
16+
{
17+
ExampleStruct2 ex = new();
18+
byte* addr = (byte*)&ex;
19+
Console.WriteLine("Size: {0}", sizeof(ExampleStruct2));
20+
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
21+
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
22+
Console.WriteLine("i3 Offset: {0}", (byte*)&ex.i3 - addr);
23+
}
2524
}
2625
// The example displays the following output:
2726
// Size: 6
2827
// b1 Offset: 0
2928
// b2 Offset: 1
3029
// i3 Offset: 2
31-
// </Snippet3>
30+
// </Snippet3>

snippets/csharp/System.Runtime.InteropServices/StructLayoutAttribute/Pack/packex3.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22
using System;
33
using System.Runtime.InteropServices;
44

5-
[StructLayout(LayoutKind.Sequential, Pack=4)]
6-
struct ExampleStruct
5+
[StructLayout(LayoutKind.Sequential, Pack = 4)]
6+
struct ExampleStruct3
77
{
8-
public byte b1;
9-
public byte b2;
10-
public int i3;
8+
public byte b1;
9+
public byte b2;
10+
public int i3;
1111
}
1212

13-
public class Example
13+
public class Example3
1414
{
15-
public unsafe static void Main()
16-
{
17-
18-
ExampleStruct ex = new ExampleStruct();
19-
byte* addr = (byte*) &ex;
20-
Console.WriteLine("Size: {0}", sizeof(ExampleStruct));
21-
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
22-
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
23-
Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
24-
}
15+
public unsafe static void Main()
16+
{
17+
ExampleStruct3 ex = new();
18+
byte* addr = (byte*)&ex;
19+
Console.WriteLine("Size: {0}", sizeof(ExampleStruct3));
20+
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
21+
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
22+
Console.WriteLine("i3 Offset: {0}", (byte*)&ex.i3 - addr);
23+
}
2524
}
2625
// The example displays the following output:
2726
// Size: 8
2827
// b1 Offset: 0
2928
// b2 Offset: 1
3029
// i3 Offset: 4
31-
// </Snippet4>
30+
// </Snippet4>

snippets/csharp/System.Runtime.InteropServices/StructLayoutAttribute/Pack/packex4.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22
using System;
33
using System.Runtime.InteropServices;
44

5-
[StructLayout(LayoutKind.Sequential, Pack=8)]
6-
struct ExampleStruct
5+
[StructLayout(LayoutKind.Sequential, Pack = 8)]
6+
struct ExampleStruct4
77
{
8-
public byte b1;
9-
public byte b2;
10-
public int i3;
8+
public byte b1;
9+
public byte b2;
10+
public int i3;
1111
}
1212

13-
public class Example
13+
public class Example4
1414
{
15-
public unsafe static void Main()
16-
{
17-
18-
ExampleStruct ex = new ExampleStruct();
19-
byte* addr = (byte*) &ex;
20-
Console.WriteLine("Size: {0}", sizeof(ExampleStruct));
21-
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
22-
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
23-
Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
24-
}
15+
public unsafe static void Main()
16+
{
17+
ExampleStruct4 ex = new();
18+
byte* addr = (byte*)&ex;
19+
Console.WriteLine("Size: {0}", sizeof(ExampleStruct4));
20+
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
21+
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
22+
Console.WriteLine("i3 Offset: {0}", (byte*)&ex.i3 - addr);
23+
}
2524
}
2625
// The example displays the following output:
2726
// Size: 8
2827
// b1 Offset: 0
2928
// b2 Offset: 1
3029
// i3 Offset: 4
31-
// </Snippet5>
30+
// </Snippet5>
Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
11
// <Snippet6>
22
using System;
3-
using System.Runtime.InteropServices;
43

5-
unsafe struct ExampleStruct2
4+
unsafe struct ExampleStruct5
65
{
76

8-
public byte b1;
9-
public byte b2;
10-
public int i3;
11-
public fixed byte a4[1];
12-
public decimal d5;
7+
public byte b1;
8+
public byte b2;
9+
public int i3;
10+
public fixed byte a4[1];
11+
public decimal d5;
1312
}
1413

15-
public class Example
14+
public class Example5
1615
{
17-
public unsafe static void Main()
18-
{
19-
20-
ExampleStruct2 ex = new ExampleStruct2();
21-
byte* addr = (byte*) &ex;
22-
Console.WriteLine("Size: {0}", sizeof(ExampleStruct2));
23-
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
24-
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
25-
Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
26-
Console.WriteLine("a4 Offset: {0}", ex.a4 - addr);
27-
Console.WriteLine("d5 Offset: {0}", (byte*) &ex.d5 - addr);
28-
}
16+
public unsafe static void Main()
17+
{
18+
ExampleStruct5 ex = new();
19+
byte* addr = (byte*)&ex;
20+
Console.WriteLine("Size: {0}", sizeof(ExampleStruct5));
21+
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
22+
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
23+
Console.WriteLine("i3 Offset: {0}", (byte*)&ex.i3 - addr);
24+
Console.WriteLine("a4 Offset: {0}", ex.a4 - addr);
25+
Console.WriteLine("d5 Offset: {0}", (byte*)&ex.d5 - addr);
26+
}
2927
}
3028
// The example displays the following output:
29+
//
30+
// .NET 5+:
31+
// Size: 32
32+
// b1 Offset: 0
33+
// b2 Offset: 1
34+
// i3 Offset: 4
35+
// a4 Offset: 8
36+
// d5 Offset: 16
37+
//
38+
// .NET Framework:
3139
// Size: 28
3240
// b1 Offset: 0
3341
// b2 Offset: 1
3442
// i3 Offset: 4
3543
// a4 Offset: 8
3644
// d5 Offset: 12
37-
// </Snippet6>
45+
// </Snippet6>

snippets/csharp/System.Runtime.InteropServices/StructLayoutAttribute/Pack/packex6.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,29 @@
33
using System.Runtime.InteropServices;
44

55
[StructLayout(LayoutKind.Sequential, Pack = 2)]
6-
unsafe struct ExampleStruct2
6+
unsafe struct ExampleStruct6
77
{
88

9-
public byte b1;
10-
public byte b2;
11-
public int i3;
12-
public fixed byte a4[1];
13-
public decimal d5;
9+
public byte b1;
10+
public byte b2;
11+
public int i3;
12+
public fixed byte a4[1];
13+
public decimal d5;
1414
}
1515

16-
public class Example
16+
public class Example6
1717
{
18-
public unsafe static void Main()
19-
{
20-
21-
ExampleStruct2 ex = new ExampleStruct2();
22-
byte* addr = (byte*) &ex;
23-
Console.WriteLine("Size: {0}", sizeof(ExampleStruct2));
24-
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
25-
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
26-
Console.WriteLine("i3 Offset: {0}", (byte*) &ex.i3 - addr);
27-
Console.WriteLine("a4 Offset: {0}", ex.a4 - addr);
28-
Console.WriteLine("d5 Offset: {0}", (byte*) &ex.d5 - addr);
29-
}
18+
public unsafe static void Main()
19+
{
20+
ExampleStruct6 ex = new();
21+
byte* addr = (byte*)&ex;
22+
Console.WriteLine("Size: {0}", sizeof(ExampleStruct6));
23+
Console.WriteLine("b1 Offset: {0}", &ex.b1 - addr);
24+
Console.WriteLine("b2 Offset: {0}", &ex.b2 - addr);
25+
Console.WriteLine("i3 Offset: {0}", (byte*)&ex.i3 - addr);
26+
Console.WriteLine("a4 Offset: {0}", ex.a4 - addr);
27+
Console.WriteLine("d5 Offset: {0}", (byte*)&ex.d5 - addr);
28+
}
3029
}
3130
// The example displays the following output:
3231
// Size: 24
@@ -35,4 +34,4 @@ public unsafe static void Main()
3534
// i3 Offset: 2
3635
// a4 Offset: 6
3736
// d5 Offset: 8
38-
// </Snippet7>
37+
// </Snippet7>

0 commit comments

Comments
 (0)