-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathUnicodeEncoding.xml
2153 lines (1878 loc) · 159 KB
/
UnicodeEncoding.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<Type Name="UnicodeEncoding" FullName="System.Text.UnicodeEncoding">
<TypeSignature Language="C#" Value="public class UnicodeEncoding : System.Text.Encoding" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi beforefieldinit UnicodeEncoding extends System.Text.Encoding" FrameworkAlternate="dotnet-uwp-10.0;net-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netstandard-1.0;netstandard-1.1;netstandard-1.2;netstandard-1.3;netstandard-1.4;netstandard-1.6;netstandard-2.0;netstandard-2.1" />
<TypeSignature Language="DocId" Value="T:System.Text.UnicodeEncoding" />
<TypeSignature Language="VB.NET" Value="Public Class UnicodeEncoding
Inherits Encoding" />
<TypeSignature Language="F#" Value="type UnicodeEncoding = class
 inherit Encoding" />
<TypeSignature Language="C++ CLI" Value="public ref class UnicodeEncoding : System::Text::Encoding" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi serializable beforefieldinit UnicodeEncoding extends System.Text.Encoding" FrameworkAlternate="netframework-1.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1" />
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<TypeForwardingChain>
<TypeForwarding From="mscorlib" FromVersion="4.0.0.0" To="System.Text.Encoding.Extensions" ToVersion="0.0.0.0" FrameworkAlternate="dotnet-uwp-10.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Text.Encoding.Extensions" ToVersion="10.0.0.0" FrameworkAlternate="net-10.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Text.Encoding.Extensions" ToVersion="5.0.0.0" FrameworkAlternate="net-5.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Text.Encoding.Extensions" ToVersion="6.0.0.0" FrameworkAlternate="net-6.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Text.Encoding.Extensions" ToVersion="7.0.0.0" FrameworkAlternate="net-7.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Text.Encoding.Extensions" ToVersion="8.0.0.0" FrameworkAlternate="net-8.0" />
<TypeForwarding From="netstandard" FromVersion="2.1.0.0" To="System.Text.Encoding.Extensions" ToVersion="9.0.0.0" FrameworkAlternate="net-9.0" />
</TypeForwardingChain>
<Base>
<BaseTypeName>System.Text.Encoding</BaseTypeName>
</Base>
<Interfaces />
<Attributes>
<Attribute FrameworkAlternate="net-10.0;net-8.0;net-9.0">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(0)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(0)>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-1.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.Serializable]</AttributeName>
<AttributeName Language="F#">[<System.Serializable>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.Runtime.InteropServices.ComVisible(true)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.InteropServices.ComVisible(true)>]</AttributeName>
</Attribute>
</Attributes>
<Docs>
<summary>Represents a UTF-16 encoding of Unicode characters.</summary>
<remarks>
<format type="text/markdown">< assigns a code point (a number) to each character in every supported script. A Unicode Transformation Format (UTF) is a way to encode that code point. The [Unicode Standard](https://go.microsoft.com/fwlink/?linkid=37123) uses the following UTFs:
- UTF-8, which represents each code point as a sequence of one to four bytes.
- UTF-16, which represents each code point as a sequence of one to two 16-bit integers.
- UTF-32, which represents each code point as a 32-bit integer.
For more information about the UTFs and other encodings supported by <xref:System.Text>, see [Character Encoding in the .NET Framework](/dotnet/standard/base-types/character-encoding).
The <xref:System.Text.UnicodeEncoding> class represents a UTF-16 encoding. The encoder can use either big endian byte order (most significant byte first) or little endian byte order (least significant byte first). For example, the Latin Capital Letter A (code point U+0041) is serialized as follows (in hexadecimal):
- Big endian byte order: 00 00 00 41
- Little endian byte order: 41 00 00 00
It is generally more efficient to store Unicode characters using the native byte order of a particular platform. For example, it is better to use the little endian byte order on little endian platforms, such as Intel computers. The <xref:System.Text.UnicodeEncoding> class corresponds to the Windows code pages 1200 (little endian byte order) and 1201 (big endian byte order). You can determine the "endianness" of a particular architecture by calling the <xref:System.BitConverter.IsLittleEndian?displayProperty=nameWithType> method.
Optionally, the <xref:System.Text.UnicodeEncoding> object provides a byte order mark (BOM), which is an array of bytes that can be prefixed to the sequence of bytes resulting from the encoding process. If the preamble contains a byte order mark (BOM), it helps the decoder determine the byte order and the transformation format or UTF.
If the <xref:System.Text.UnicodeEncoding> instance is configured to provide a BOM, you can retrieve it by calling the <xref:System.Text.UnicodeEncoding.GetPreamble%2A> method; otherwise, the method returns an empty array. Note that, even if a <xref:System.Text.UnicodeEncoding> object is configured for BOM support, you must include the BOM at the beginning of the encoded byte stream as appropriate; the encoding methods of the <xref:System.Text.UnicodeEncoding> class do not do this automatically.
> [!CAUTION]
> To enable error detection and to make the class instance more secure, you should instantiate a <xref:System.Text.UnicodeEncoding> object by calling the <xref:System.Text.UnicodeEncoding.%23ctor%28System.Boolean%2CSystem.Boolean%2CSystem.Boolean%29> constructor and setting its `throwOnInvalidBytes` argument to `true`. With error detection, a method that detects an invalid sequence of characters or bytes throws a <xref:System.ArgumentException>. Without error detection, no exception is thrown, and the invalid sequence is generally ignored.
You can instantiate a <xref:System.Text.UnicodeEncoding> object in a number of ways, depending on whether you want to it to provide a byte order mark (BOM), whether you want big-endian or little-endian encoding, and whether you want to enable error detection. The following table lists the <xref:System.Text.UnicodeEncoding> constructors and the <xref:System.Text.Encoding> properties that return a <xref:System.Text.UnicodeEncoding> object.
|Member|Endianness|BOM|Error detection|
|------------|----------------|---------|---------------------|
|<xref:System.Text.Encoding.BigEndianUnicode%2A>|Big-endian|Yes|No (Replacement fallback)|
|<xref:System.Text.Encoding.Unicode%2A?displayProperty=nameWithType>|Little-endian|Yes|No (Replacement fallback)|
|<xref:System.Text.UnicodeEncoding.%23ctor?displayProperty=nameWithType>|Little-endian|Yes|No (Replacement fallback)|
|<xref:System.Text.UnicodeEncoding.%23ctor%28System.Boolean%2CSystem.Boolean%29>|Configurable|Configurable|No (Replacement fallback)|
|<xref:System.Text.UnicodeEncoding.%23ctor%28System.Boolean%2CSystem.Boolean%2CSystem.Boolean%29?displayProperty=nameWithType>|Configurable|Configurable|Configurable|
The <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method determines how many bytes result in encoding a set of Unicode characters, and the <xref:System.Text.UnicodeEncoding.GetBytes%2A> method performs the actual encoding.
Likewise, the <xref:System.Text.UnicodeEncoding.GetCharCount%2A> method determines how many characters result in decoding a sequence of bytes, and the <xref:System.Text.UnicodeEncoding.GetChars%2A> and <xref:System.Text.UnicodeEncoding.GetString%2A> methods perform the actual decoding.
For an encoder or decoder that is able to save state information when encoding or decoding data that spans multiple blocks (such as string of 1 million characters that is encoded in 100,000-character segments), use the <xref:System.Text.UnicodeEncoding.GetEncoder%2A> and <xref:System.Text.UnicodeEncoding.GetDecoder%2A> properties, respectively.
## Examples
The following example demonstrates how to encode a string of Unicode characters into a byte array by using a <xref:System.Text.UnicodeEncoding> object. The byte array is decoded into a string to demonstrate that there is no loss of data.
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Text.UnicodeEncoding Example/CPP/snippet.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System.Text/UnicodeEncoding/Overview/snippet.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Text.UnicodeEncoding Example/VB/snippet.vb" id="Snippet1":::
The following example uses the same string as the previous one, except that it writes the encoded bytes to a file and prefixes the byte stream with a byte order mark (BOM). It then reads the file in two different ways: as a text file by using a <xref:System.IO.StreamReader> object; and as a binary file. As you would expect, neither newly-read string includes the BOM.
:::code language="csharp" source="~/snippets/csharp/System.Text/UnicodeEncoding/Overview/bom1.cs" interactive="try-dotnet" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Text.UnicodeEncoding Example/VB/bom1.vb" id="Snippet2":::
]]></format>
</remarks>
<altmember cref="T:System.Text.Encoding" />
<altmember cref="T:System.Text.Encoder" />
<altmember cref="T:System.Text.Decoder" />
<altmember cref="T:System.Text.UTF32Encoding" />
<altmember cref="T:System.Text.UTF8Encoding" />
<altmember cref="T:System.Text.UTF7Encoding" />
<altmember cref="T:System.Text.ASCIIEncoding" />
<altmember cref="T:System.Globalization.UnicodeCategory" />
<altmember cref="T:System.Globalization.CharUnicodeInfo" />
<related type="Article" href="/dotnet/standard/base-types/character-encoding">Understanding Encodings</related>
</Docs>
<Members>
<MemberGroup MemberName=".ctor">
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Docs>
<summary>Initializes a new instance of the <see cref="T:System.Text.UnicodeEncoding" /> class.</summary>
</Docs>
</MemberGroup>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public UnicodeEncoding ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Text.UnicodeEncoding.#ctor" />
<MemberSignature Language="VB.NET" Value="Public Sub New ()" />
<MemberSignature Language="C++ CLI" Value="public:
 UnicodeEncoding();" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters />
<Docs>
<summary>Initializes a new instance of the <see cref="T:System.Text.UnicodeEncoding" /> class.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
This constructor creates an instance that uses the little endian byte order, provides a Unicode byte order mark, and does not throw an exception when an invalid encoding is detected.
> [!CAUTION]
> For security reasons, you should enable error detection by calling the <xref:System.Text.UnicodeEncoding.%23ctor%28System.Boolean%2CSystem.Boolean%2CSystem.Boolean%29> constructor and setting its `throwOnInvalidBytes` argument to `true`.
## Examples
The following example demonstrates how to create a new <xref:System.Text.UnicodeEncoding> instance and display the name of the encoding.
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.ctor Example/CPP/ctor.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System.Text/UnicodeEncoding/.ctor/ctor.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.ctor Example/VB/ctor.vb" id="Snippet1":::
]]></format>
</remarks>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public UnicodeEncoding (bool bigEndian, bool byteOrderMark);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(bool bigEndian, bool byteOrderMark) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Text.UnicodeEncoding.#ctor(System.Boolean,System.Boolean)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (bigEndian As Boolean, byteOrderMark As Boolean)" />
<MemberSignature Language="F#" Value="new System.Text.UnicodeEncoding : bool * bool -> System.Text.UnicodeEncoding" Usage="new System.Text.UnicodeEncoding (bigEndian, byteOrderMark)" />
<MemberSignature Language="C++ CLI" Value="public:
 UnicodeEncoding(bool bigEndian, bool byteOrderMark);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="bigEndian" Type="System.Boolean" />
<Parameter Name="byteOrderMark" Type="System.Boolean" />
</Parameters>
<Docs>
<param name="bigEndian">
<see langword="true" /> to use the big endian byte order (most significant byte first), or <see langword="false" /> to use the little endian byte order (least significant byte first).</param>
<param name="byteOrderMark">
<see langword="true" /> to specify that the <see cref="M:System.Text.UnicodeEncoding.GetPreamble" /> method returns a Unicode byte order mark; otherwise, <see langword="false" />.</param>
<summary>Initializes a new instance of the <see cref="T:System.Text.UnicodeEncoding" /> class. Parameters specify whether to use the big endian byte order and whether the <see cref="M:System.Text.UnicodeEncoding.GetPreamble" /> method returns a Unicode byte order mark.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
This constructor creates an instance that does not throw an exception when an invalid encoding is detected.
> [!CAUTION]
> For security reasons, you should enable error detection by calling the <xref:System.Text.UnicodeEncoding.%23ctor%28System.Boolean%2CSystem.Boolean%2CSystem.Boolean%29> constructor and setting its `throwOnInvalidBytes` argument to `true`.
The `byteOrderMark` parameter controls the operation of the <xref:System.Text.UnicodeEncoding.GetPreamble%2A> method. If `true`, the method returns a byte array containing the Unicode byte order mark (BOM) in UTF-16 format. If `false`, it returns a zero-length byte array. However, setting `byteOrderMark` to `true` does not cause the <xref:System.Text.UnicodeEncoding.GetBytes%2A> method to prefix the BOM at the beginning of the byte array, nor does it cause the <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method to include the number of bytes in the BOM in the byte count.
## Examples
The following example demonstrates how to create a new <xref:System.Text.UnicodeEncoding> instance specifying whether to support little endian or big endian byte ordering and the Unicode byte order mark.
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.ctor2 Example/CPP/ctor-boolean-boolean.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System.Text/UnicodeEncoding/.ctor/ctor-boolean-boolean.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.ctor2 Example/VB/ctor-boolean-boolean.vb" id="Snippet1":::
]]></format>
</remarks>
<altmember cref="M:System.Text.UnicodeEncoding.GetPreamble" />
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public UnicodeEncoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Text.UnicodeEncoding.#ctor(System.Boolean,System.Boolean,System.Boolean)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (bigEndian As Boolean, byteOrderMark As Boolean, throwOnInvalidBytes As Boolean)" />
<MemberSignature Language="F#" Value="new System.Text.UnicodeEncoding : bool * bool * bool -> System.Text.UnicodeEncoding" Usage="new System.Text.UnicodeEncoding (bigEndian, byteOrderMark, throwOnInvalidBytes)" />
<MemberSignature Language="C++ CLI" Value="public:
 UnicodeEncoding(bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="bigEndian" Type="System.Boolean" Index="0" FrameworkAlternate="dotnet-uwp-10.0;net-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2;netstandard-1.3;netstandard-1.4;netstandard-1.6;netstandard-2.0;netstandard-2.1" />
<Parameter Name="byteOrderMark" Type="System.Boolean" Index="1" FrameworkAlternate="dotnet-uwp-10.0;net-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2;netstandard-1.3;netstandard-1.4;netstandard-1.6;netstandard-2.0;netstandard-2.1" />
<Parameter Name="throwOnInvalidBytes" Type="System.Boolean" Index="2" FrameworkAlternate="dotnet-uwp-10.0;net-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2;netstandard-1.3;netstandard-1.4;netstandard-1.6;netstandard-2.0;netstandard-2.1" />
</Parameters>
<Docs>
<param name="bigEndian">
<see langword="true" /> to use the big endian byte order (most significant byte first); <see langword="false" /> to use the little endian byte order (least significant byte first).</param>
<param name="byteOrderMark">
<see langword="true" /> to specify that the <see cref="M:System.Text.UnicodeEncoding.GetPreamble" /> method returns a Unicode byte order mark; otherwise, <see langword="false" />.</param>
<param name="throwOnInvalidBytes">
<see langword="true" /> to specify that an exception should be thrown when an invalid encoding is detected; otherwise, <see langword="false" />.</param>
<summary>Initializes a new instance of the <see cref="T:System.Text.UnicodeEncoding" /> class. Parameters specify whether to use the big endian byte order, whether to provide a Unicode byte order mark, and whether to throw an exception when an invalid encoding is detected.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The `byteOrderMark` parameter controls the operation of the <xref:System.Text.UnicodeEncoding.GetPreamble%2A> method. If `true`, the method returns a byte array containing the Unicode byte order mark (BOM) in UTF-16 format. If `false`, it returns a zero-length byte array. However, setting `byteOrderMark` to `true` does not cause the <xref:System.Text.UnicodeEncoding.GetBytes%2A> method to prefix the BOM at the beginning of the byte array, nor does it cause the <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method to include the number of bytes in the BOM in the byte count.
If the `throwOnInvalidBytes` parameter is `true`, a method that detects an invalid byte sequence throws <xref:System.ArgumentException?displayProperty=nameWithType>. Otherwise, the method does not throw an exception, and the invalid sequence is ignored.
> [!CAUTION]
> For security reasons, you should use this constructor to create an instance of the <xref:System.Text.UnicodeEncoding> class and turn on error detection by setting `throwOnInvalidBytes` to `true`.
## Examples
The following example demonstrates the behavior of <xref:System.Text.UnicodeEncoding>, both with error detection enabled and without.
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.ErrorDetection/CPP/errordetection.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System.Text/UnicodeEncoding/.ctor/errordetection.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.ErrorDetection/VB/errordetection.vb" id="Snippet1":::
]]></format>
</remarks>
<altmember cref="M:System.Text.UnicodeEncoding.GetPreamble" />
</Docs>
</Member>
<Member MemberName="CharSize">
<MemberSignature Language="C#" Value="public const int CharSize = 2;" />
<MemberSignature Language="ILAsm" Value=".field public static literal int32 CharSize = (2)" />
<MemberSignature Language="DocId" Value="F:System.Text.UnicodeEncoding.CharSize" />
<MemberSignature Language="VB.NET" Value="Public Const CharSize As Integer = 2" />
<MemberSignature Language="F#" Value="val mutable CharSize : int" Usage="System.Text.UnicodeEncoding.CharSize" />
<MemberSignature Language="C++ CLI" Value="public: int CharSize = 2;" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Int32</ReturnType>
</ReturnValue>
<MemberValue>2</MemberValue>
<Docs>
<summary>Represents the Unicode character size in bytes. This field is a constant.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
The value of this field is a 32-bit signed constant with a value of 2.
## Examples
The following example demonstrates how to return the value of <xref:System.Text.UnicodeEncoding.CharSize> and display it.
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.CharSize Example/CPP/charsize.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System.Text/UnicodeEncoding/CharSize/charsize.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.CharSize Example/VB/charsize.vb" id="Snippet1":::
]]></format>
</remarks>
</Docs>
</Member>
<Member MemberName="Equals">
<MemberSignature Language="C#" Value="public override bool Equals (object value);" FrameworkAlternate="dotnet-uwp-10.0;netcore-1.0;netcore-1.1;netcore-2.0;netcore-2.1;netcore-2.2;netframework-1.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2;netstandard-1.3;netstandard-1.4;netstandard-1.6;netstandard-2.0;netstandard-2.1" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig virtual instance bool Equals(object value) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Text.UnicodeEncoding.Equals(System.Object)" />
<MemberSignature Language="VB.NET" Value="Public Overrides Function Equals (value As Object) As Boolean" />
<MemberSignature Language="F#" Value="override this.Equals : obj -> bool" Usage="unicodeEncoding.Equals value" />
<MemberSignature Language="C++ CLI" Value="public:
 override bool Equals(System::Object ^ value);" />
<MemberSignature Language="C#" Value="public override bool Equals (object? value);" FrameworkAlternate="net-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-3.0;netcore-3.1" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Boolean</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="value" Type="System.Object">
<Attributes>
<Attribute FrameworkAlternate="net-10.0;net-6.0;net-7.0;net-8.0;net-9.0">
<AttributeName Language="C#">[System.Diagnostics.CodeAnalysis.NotNullWhen(true)]</AttributeName>
<AttributeName Language="F#">[<System.Diagnostics.CodeAnalysis.NotNullWhen(true)>]</AttributeName>
</Attribute>
</Attributes>
</Parameter>
</Parameters>
<Docs>
<param name="value">The object to compare with the current object.</param>
<summary>Determines whether the specified <see cref="T:System.Object" /> is equal to the current <see cref="T:System.Text.UnicodeEncoding" /> object.</summary>
<returns>
<see langword="true" /> if <paramref name="value" /> is an instance of <see cref="T:System.Text.UnicodeEncoding" /> and is equal to the current object; otherwise, <see langword="false" />.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
Two <xref:System.Text.UnicodeEncoding> objects are considered equal if all of the following conditions are true:
- Both objects use the same byte order (little-endian or big-endian).
- Both objects provide the byte order mark, or both do not.
- Both objects use the same encoder fallback.
- Both objects use the same decoder fallback.
## Examples
The following example demonstrates how to use the <xref:System.Text.UnicodeEncoding.Equals%2A> method to test whether the current <xref:System.Text.UnicodeEncoding> object is equal to a different <xref:System.Text.UnicodeEncoding> object. Five <xref:System.Text.UnicodeEncoding> objects are created and compared, and the results of the comparisons are displayed.
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.ctor2 Example/CPP/ctor-boolean-boolean.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System.Text/UnicodeEncoding/.ctor/ctor-boolean-boolean.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.ctor2 Example/VB/ctor-boolean-boolean.vb" id="Snippet1":::
]]></format>
</remarks>
<altmember cref="M:System.Text.UnicodeEncoding.#ctor" />
<altmember cref="M:System.Object.Equals(System.Object)" />
</Docs>
</Member>
<MemberGroup MemberName="GetByteCount">
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Docs>
<summary>Calculates the number of bytes produced by encoding a set of characters.</summary>
</Docs>
</MemberGroup>
<Member MemberName="GetByteCount">
<MemberSignature Language="C#" Value="public override int GetByteCount (string s);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig virtual instance int32 GetByteCount(string s) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Text.UnicodeEncoding.GetByteCount(System.String)" />
<MemberSignature Language="VB.NET" Value="Public Overrides Function GetByteCount (s As String) As Integer" />
<MemberSignature Language="F#" Value="override this.GetByteCount : string -> int" Usage="unicodeEncoding.GetByteCount s" />
<MemberSignature Language="C++ CLI" Value="public:
 override int GetByteCount(System::String ^ s);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2">
<AttributeName Language="C#">[System.Security.SecuritySafeCritical]</AttributeName>
<AttributeName Language="F#">[<System.Security.SecuritySafeCritical>]</AttributeName>
</Attribute>
</Attributes>
<ReturnValue>
<ReturnType>System.Int32</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="s" Type="System.String" />
</Parameters>
<Docs>
<param name="s">The string that contains the set of characters to encode.</param>
<summary>Calculates the number of bytes produced by encoding the characters in the specified string.</summary>
<returns>The number of bytes produced by encoding the specified characters.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
To calculate the exact array size required by <xref:System.Text.UnicodeEncoding.GetBytes%2A> to store the resulting bytes, you call the <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method. To calculate the maximum array size, you call the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method. The <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method generally allocates less memory, while the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method generally executes faster.
With error detection, an invalid sequence causes this method to throw a <xref:System.ArgumentException>. Without error detection, invalid sequences are ignored, and no exception is thrown.
> [!IMPORTANT]
> To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix a stream of encoded bytes with a preamble. Inserting the preamble at the beginning of a byte stream (such as at the beginning of a series of bytes to be written to a file) is the developer's responsibility, and the number of bytes in the preamble is not reflected in the value returned by the <xref:System.Text.UnicodeEncoding.GetByteCount%28System.String%29> method.
## Examples
The following example calls the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> and <xref:System.Text.UnicodeEncoding.GetByteCount%28System.String%29> methods to calculate the maximum and actual number of bytes required to encode a string. It also displays the actual number of bytes required to store a byte stream with a byte order mark.
:::code language="csharp" source="~/snippets/csharp/System.Text/UnicodeEncoding/GetByteCount/getbytecount1.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.text.unicodeencoding.getbytecount/vb/getbytecount1.vb" id="Snippet1":::
]]></format>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="s" /> is <see langword="null" />.</exception>
<exception cref="T:System.ArgumentOutOfRangeException">The resulting number of bytes is greater than the maximum number that can be returned as an integer.</exception>
<exception cref="T:System.ArgumentException">Error detection is enabled, and <paramref name="s" /> contains an invalid sequence of characters.</exception>
<exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (for more information, see <see href="https://learn.microsoft.com/dotnet/standard/base-types/character-encoding">Character Encoding in .NET</see>)
-and-
<see cref="P:System.Text.Encoding.EncoderFallback" /> is set to <see cref="T:System.Text.EncoderExceptionFallback" />.</exception>
<altmember cref="M:System.Text.UnicodeEncoding.GetBytes(System.String,System.Int32,System.Int32,System.Byte[],System.Int32)" />
<altmember cref="M:System.Text.UnicodeEncoding.GetMaxByteCount(System.Int32)" />
<altmember cref="M:System.Text.UnicodeEncoding.GetEncoder" />
</Docs>
</Member>
<Member MemberName="GetByteCount">
<MemberSignature Language="C#" Value="public override int GetByteCount (char* chars, int count);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig virtual instance int32 GetByteCount(char* chars, int32 count) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Text.UnicodeEncoding.GetByteCount(System.Char*,System.Int32)" />
<MemberSignature Language="F#" Value="override this.GetByteCount : nativeptr<char> * int -> int" Usage="unicodeEncoding.GetByteCount (chars, count)" />
<MemberSignature Language="C++ CLI" Value="public:
 override int GetByteCount(char* chars, int count);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="net-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-2.0;netstandard-2.1">
<AttributeName Language="C#">[System.CLSCompliant(false)]</AttributeName>
<AttributeName Language="F#">[<System.CLSCompliant(false)>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netcore-2.0;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.Security.SecurityCritical]</AttributeName>
<AttributeName Language="F#">[<System.Security.SecurityCritical>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.Runtime.InteropServices.ComVisible(false)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.InteropServices.ComVisible(false)>]</AttributeName>
</Attribute>
</Attributes>
<ReturnValue>
<ReturnType>System.Int32</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="chars" Type="System.Char*" Index="0" FrameworkAlternate="netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netstandard-2.0;netstandard-2.1;netcore-3.1;net-5.0;net-6.0;net-7.0;netframework-4.8.1;net-8.0;net-9.0;net-10.0" />
<Parameter Name="count" Type="System.Int32" Index="1" FrameworkAlternate="netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netstandard-2.0;netstandard-2.1;netcore-3.1;net-5.0;net-6.0;net-7.0;netframework-4.8.1;net-8.0;net-9.0;net-10.0" />
</Parameters>
<Docs>
<param name="chars">A pointer to the first character to encode.</param>
<param name="count">The number of characters to encode.</param>
<summary>Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer.</summary>
<returns>The number of bytes produced by encoding the specified characters.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
To calculate the exact array size that <xref:System.Text.UnicodeEncoding.GetBytes%2A> requires to store the resulting bytes, you call the <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method. To calculate the maximum array size, you call the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method. The <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method generally allocates less memory, while the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method generally executes faster.
With error detection, an invalid sequence causes this method to throw a <xref:System.ArgumentException>. Without error detection, invalid sequences are ignored, and no exception is thrown.
> [!IMPORTANT]
> To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix a stream of encoded bytes with a preamble. Inserting the preamble at the beginning of a byte stream (such as at the beginning of a series of bytes to be written to a file) is the developer's responsibility, and the number of bytes in the preamble is not reflected in the value returned by the <xref:System.Text.UTF8Encoding.GetByteCount%2A> method.
]]></format>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="chars" /> is <see langword="null" />.</exception>
<exception cref="T:System.ArgumentOutOfRangeException">
<paramref name="count" /> is less than zero.
-or-
The resulting number of bytes is greater than the maximum number that can be returned as an integer.</exception>
<exception cref="T:System.ArgumentException">Error detection is enabled and <paramref name="chars" /> contains an invalid sequence of characters.</exception>
<exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (for more information, see <see href="https://learn.microsoft.com/dotnet/standard/base-types/character-encoding">Character Encoding in .NET</see>)
-and-
<see cref="P:System.Text.Encoding.EncoderFallback" /> is set to <see cref="T:System.Text.EncoderExceptionFallback" />.</exception>
<altmember cref="M:System.Text.UnicodeEncoding.GetBytes(System.String,System.Int32,System.Int32,System.Byte[],System.Int32)" />
<altmember cref="M:System.Text.UnicodeEncoding.GetMaxByteCount(System.Int32)" />
<altmember cref="M:System.Text.UnicodeEncoding.GetEncoder" />
</Docs>
</Member>
<Member MemberName="GetByteCount">
<MemberSignature Language="C#" Value="public override int GetByteCount (char[] chars, int index, int count);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig virtual instance int32 GetByteCount(char[] chars, int32 index, int32 count) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Text.UnicodeEncoding.GetByteCount(System.Char[],System.Int32,System.Int32)" />
<MemberSignature Language="VB.NET" Value="Public Overrides Function GetByteCount (chars As Char(), index As Integer, count As Integer) As Integer" />
<MemberSignature Language="F#" Value="override this.GetByteCount : char[] * int * int -> int" Usage="unicodeEncoding.GetByteCount (chars, index, count)" />
<MemberSignature Language="C++ CLI" Value="public:
 override int GetByteCount(cli::array <char> ^ chars, int index, int count);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2">
<AttributeName Language="C#">[System.Security.SecuritySafeCritical]</AttributeName>
<AttributeName Language="F#">[<System.Security.SecuritySafeCritical>]</AttributeName>
</Attribute>
</Attributes>
<ReturnValue>
<ReturnType>System.Int32</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="chars" Type="System.Char[]" />
<Parameter Name="index" Type="System.Int32" />
<Parameter Name="count" Type="System.Int32" />
</Parameters>
<Docs>
<param name="chars">The character array containing the set of characters to encode.</param>
<param name="index">The index of the first character to encode.</param>
<param name="count">The number of characters to encode.</param>
<summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
<returns>The number of bytes produced by encoding the specified characters.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
To calculate the exact array size required by <xref:System.Text.UnicodeEncoding.GetBytes%2A> to store the resulting bytes, the application uses <xref:System.Text.UnicodeEncoding.GetByteCount%2A>. To calculate the maximum array size, you call the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method. The <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method generally allocates less memory, while the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method generally executes faster.
With error detection enabled, an invalid sequence causes this method to throw an <xref:System.ArgumentException>. Without error detection, invalid sequences are ignored, and no exception is thrown.
To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix a stream of encoded bytes with a preamble. Inserting the preamble at the beginning of a byte stream (such as at the beginning of a series of bytes to be written to a file) is the developer's responsibility, and the number of bytes in the preamble Is not reflected in the value returned by the <xref:System.Text.UnicodeEncoding.GetByteCount%28System.Char%5B%5D%2CSystem.Int32%2CSystem.Int32%29> method.
## Examples
The following example populates an array with a Latin uppercase and lowercase characters and calls the <xref:System.Text.UnicodeEncoding.GetByteCount%28System.Char%5B%5D%2CSystem.Int32%2CSystem.Int32%29> method to determine the number of bytes needed to encode the Latin lowercase characters. It then displays this information along with the total number of bytes needed if a byte order mark is added. It compares this number with the value returned by the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method, which indicates maximum number of bytes needed to encode the Latin lowercase characters. The following example populates an array with a combination of Greek and Cyrillic characters and calls the <xref:System.Text.UnicodeEncoding.GetByteCount%28System.Char%5B%5D%2CSystem.Int32%2CSystem.Int32%29> method to determine the number of bytes needed to encode the Cyrillic characters. It then displays this information along with the total number of bytes needed if a byte order mark is added. It compares this number with the value returned by the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method, which indicates maximum number of bytes needed to encode the Cyrillic characters.
:::code language="csharp" source="~/snippets/csharp/System.Text/UnicodeEncoding/GetByteCount/getbytecount2.cs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.text.unicodeencoding.getbytecount/vb/getbytecount2.vb" id="Snippet2":::
]]></format>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="chars" /> is <see langword="null" /> (<see langword="Nothing" />).</exception>
<exception cref="T:System.ArgumentOutOfRangeException">
<paramref name="index" /> or <paramref name="count" /> is less than zero.
-or-
<paramref name="index" /> and <paramref name="count" /> do not denote a valid range in <paramref name="chars" />.
-or-
The resulting number of bytes is greater than the maximum number that can be returned as an integer.</exception>
<exception cref="T:System.ArgumentException">Error detection is enabled, and <paramref name="chars" /> contains an invalid sequence of characters.</exception>
<exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (for more information, see <see href="https://learn.microsoft.com/dotnet/standard/base-types/character-encoding">Character Encoding in .NET</see>)
-and-
<see cref="P:System.Text.Encoding.EncoderFallback" /> is set to <see cref="T:System.Text.EncoderExceptionFallback" />.</exception>
<altmember cref="M:System.Text.UnicodeEncoding.GetBytes(System.String,System.Int32,System.Int32,System.Byte[],System.Int32)" />
<altmember cref="M:System.Text.UnicodeEncoding.GetMaxByteCount(System.Int32)" />
<altmember cref="M:System.Text.UnicodeEncoding.GetEncoder" />
</Docs>
</Member>
<MemberGroup MemberName="GetBytes">
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Docs>
<summary>Encodes a set of characters into a sequence of bytes.</summary>
</Docs>
</MemberGroup>
<Member MemberName="GetBytes">
<MemberSignature Language="C#" Value="public override byte[] GetBytes (string s);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig virtual instance unsigned int8[] GetBytes(string s) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Text.UnicodeEncoding.GetBytes(System.String)" />
<MemberSignature Language="VB.NET" Value="Public Overrides Function GetBytes (s As String) As Byte()" />
<MemberSignature Language="F#" Value="override this.GetBytes : string -> byte[]" Usage="unicodeEncoding.GetBytes s" />
<MemberSignature Language="C++ CLI" Value="public:
 override cli::array <System::Byte> ^ GetBytes(System::String ^ s);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Byte[]</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="s" Type="System.String" Index="0" FrameworkAlternate="netframework-1.1" />
</Parameters>
<Docs>
<param name="s">To be added.</param>
<summary>Encodes a set of characters from the specified string into the specified byte array.</summary>
<returns>To be added.</returns>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="GetBytes">
<MemberSignature Language="C#" Value="public override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig virtual instance int32 GetBytes(char* chars, int32 charCount, unsigned int8* bytes, int32 byteCount) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Text.UnicodeEncoding.GetBytes(System.Char*,System.Int32,System.Byte*,System.Int32)" />
<MemberSignature Language="F#" Value="override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int" Usage="unicodeEncoding.GetBytes (chars, charCount, bytes, byteCount)" />
<MemberSignature Language="C++ CLI" Value="public:
 override int GetBytes(char* chars, int charCount, System::Byte* bytes, int byteCount);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="net-10.0;net-5.0;net-6.0;net-7.0;net-8.0;net-9.0;netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netcore-3.1;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-2.0;netstandard-2.1">
<AttributeName Language="C#">[System.CLSCompliant(false)]</AttributeName>
<AttributeName Language="F#">[<System.CLSCompliant(false)>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netcore-2.0;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.Security.SecurityCritical]</AttributeName>
<AttributeName Language="F#">[<System.Security.SecurityCritical>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.Runtime.InteropServices.ComVisible(false)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.InteropServices.ComVisible(false)>]</AttributeName>
</Attribute>
</Attributes>
<ReturnValue>
<ReturnType>System.Int32</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="chars" Type="System.Char*" Index="0" FrameworkAlternate="netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netstandard-2.0;netstandard-2.1;netcore-3.1;net-5.0;net-6.0;net-7.0;netframework-4.8.1;net-8.0;net-9.0;net-10.0" />
<Parameter Name="charCount" Type="System.Int32" Index="1" FrameworkAlternate="netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netstandard-2.0;netstandard-2.1;netcore-3.1;net-5.0;net-6.0;net-7.0;netframework-4.8.1;net-8.0;net-9.0;net-10.0" />
<Parameter Name="bytes" Type="System.Byte*" Index="2" FrameworkAlternate="netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netstandard-2.0;netstandard-2.1;netcore-3.1;net-5.0;net-6.0;net-7.0;netframework-4.8.1;net-8.0;net-9.0;net-10.0" />
<Parameter Name="byteCount" Type="System.Int32" Index="3" FrameworkAlternate="netcore-2.0;netcore-2.1;netcore-2.2;netcore-3.0;netframework-2.0;netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netstandard-2.0;netstandard-2.1;netcore-3.1;net-5.0;net-6.0;net-7.0;netframework-4.8.1;net-8.0;net-9.0;net-10.0" />
</Parameters>
<Docs>
<param name="chars">A pointer to the first character to encode.</param>
<param name="charCount">The number of characters to encode.</param>
<param name="bytes">A pointer to the location at which to start writing the resulting sequence of bytes.</param>
<param name="byteCount">The maximum number of bytes to write.</param>
<summary>Encodes a set of characters starting at the specified character pointer into a sequence of bytes that are stored starting at the specified byte pointer.</summary>
<returns>The actual number of bytes written at the location indicated by the <paramref name="bytes" /> parameter.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
To calculate the exact array size that <xref:System.Text.UnicodeEncoding.GetBytes%2A> requires to store the resulting bytes, you call the <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method. To calculate the maximum array size, you call the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method. The <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method generally allocates less memory, while the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method generally executes faster.
With error detection, an invalid sequence causes this method to throw a <xref:System.ArgumentException>. Without error detection, invalid sequences are ignored, and no exception is thrown.
Data to be converted, such as data read from a stream, might be available only in sequential blocks. In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the <xref:System.Text.Decoder> or the <xref:System.Text.Encoder> object provided by the <xref:System.Text.UnicodeEncoding.GetDecoder%2A> or the <xref:System.Text.UnicodeEncoding.GetEncoder%2A> method, respectively.
> [!IMPORTANT]
> To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix a stream of encoded bytes with a preamble. Inserting the preamble at the beginning of a byte stream (such as at the beginning of a series of bytes to be written to a file) is the developer's responsibility. The <xref:System.Text.UnicodeEncoding.GetBytes%2A> method does not prepend a preamble to the beginning of a sequence of encoded bytes.
]]></format>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="chars" /> is <see langword="null" /> (<see langword="Nothing" />).
-or-
<paramref name="bytes" /> is <see langword="null" /> (<see langword="Nothing" />).</exception>
<exception cref="T:System.ArgumentOutOfRangeException">
<paramref name="charCount" /> or <paramref name="byteCount" /> is less than zero.</exception>
<exception cref="T:System.ArgumentException">Error detection is enabled, and <paramref name="chars" /> contains an invalid sequence of characters.
-or-
<paramref name="byteCount" /> is less than the resulting number of bytes.</exception>
<exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (for more information, see <see href="https://learn.microsoft.com/dotnet/standard/base-types/character-encoding">Character Encoding in .NET</see>)
-and-
<see cref="P:System.Text.Encoding.EncoderFallback" /> is set to <see cref="T:System.Text.EncoderExceptionFallback" />.</exception>
<altmember cref="M:System.Text.UnicodeEncoding.GetEncoder" />
<altmember cref="M:System.Text.UnicodeEncoding.GetByteCount(System.Char[],System.Int32,System.Int32)" />
<altmember cref="M:System.Text.UnicodeEncoding.GetMaxByteCount(System.Int32)" />
</Docs>
</Member>
<Member MemberName="GetBytes">
<MemberSignature Language="C#" Value="public override int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig virtual instance int32 GetBytes(char[] chars, int32 charIndex, int32 charCount, unsigned int8[] bytes, int32 byteIndex) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Text.UnicodeEncoding.GetBytes(System.Char[],System.Int32,System.Int32,System.Byte[],System.Int32)" />
<MemberSignature Language="VB.NET" Value="Public Overrides Function GetBytes (chars As Char(), charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer) As Integer" />
<MemberSignature Language="F#" Value="override this.GetBytes : char[] * int * int * byte[] * int -> int" Usage="unicodeEncoding.GetBytes (chars, charIndex, charCount, bytes, byteIndex)" />
<MemberSignature Language="C++ CLI" Value="public:
 override int GetBytes(cli::array <char> ^ chars, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>System.Text.Encoding.Extensions</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.10.0</AssemblyVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.2.0</AssemblyVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>1.0.5000.0</AssemblyVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>netstandard</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1;netstandard-1.0;netstandard-1.1;netstandard-1.2">
<AttributeName Language="C#">[System.Security.SecuritySafeCritical]</AttributeName>
<AttributeName Language="F#">[<System.Security.SecuritySafeCritical>]</AttributeName>
</Attribute>
</Attributes>
<ReturnValue>
<ReturnType>System.Int32</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="chars" Type="System.Char[]" />
<Parameter Name="charIndex" Type="System.Int32" />
<Parameter Name="charCount" Type="System.Int32" />
<Parameter Name="bytes" Type="System.Byte[]" />
<Parameter Name="byteIndex" Type="System.Int32" />
</Parameters>
<Docs>
<param name="chars">The character array containing the set of characters to encode.</param>
<param name="charIndex">The index of the first character to encode.</param>
<param name="charCount">The number of characters to encode.</param>
<param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
<param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
<summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
<returns>The actual number of bytes written into <paramref name="bytes" />.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
To calculate the exact array size required by <xref:System.Text.UnicodeEncoding.GetBytes%2A> to store the resulting bytes, you call the <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method. To calculate the maximum array size, you call the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method. The <xref:System.Text.UnicodeEncoding.GetByteCount%2A> method generally allocates less memory, while the <xref:System.Text.UnicodeEncoding.GetMaxByteCount%2A> method generally executes faster.
With error detection, an invalid sequence causes this method to throw a <xref:System.ArgumentException>. Without error detection, invalid sequences are ignored, and no exception is thrown.
Data to be converted, such as data read from a stream, might be available only in sequential blocks. In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the <xref:System.Text.Decoder> or the <xref:System.Text.Encoder> provided by the <xref:System.Text.UnicodeEncoding.GetDecoder%2A> method or the <xref:System.Text.UnicodeEncoding.GetEncoder%2A> method, respectively.
> [!IMPORTANT]
> To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix a stream of encoded bytes with a preamble. Inserting the preamble at the beginning of a byte stream (such as at the beginning of a series of bytes to be written to a file) is the developer's responsibility. The <xref:System.Text.UnicodeEncoding.GetBytes%2A> method does not prepend a preamble to the beginning of a sequence of encoded bytes.
## Examples
The following example demonstrates how to use the <xref:System.Text.UnicodeEncoding.GetBytes%2A> method to encode a range of characters from a <xref:System.String> and store the encoded bytes in a range of elements in a byte array.
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.GetBytes3 Example/CPP/getbytes-string-int32-int32-byte[]-int32.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System.Text/UnicodeEncoding/GetBytes/getbytes-string-int32-int32-byte[]-int32.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Text.UnicodeEncoding.GetBytes3 Example/VB/getbytes-string-int32-int32-byte[]-int32.vb" id="Snippet1":::
]]></format>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="chars" /> is <see langword="null" /> (<see langword="Nothing" />).
-or-
<paramref name="bytes" /> is <see langword="null" /> (<see langword="Nothing" />).</exception>
<exception cref="T:System.ArgumentOutOfRangeException">
<paramref name="charIndex" /> or <paramref name="charCount" /> or <paramref name="byteIndex" /> is less than zero.
-or-
<paramref name="charIndex" /> and <paramref name="charCount" /> do not denote a valid range in <paramref name="chars" />.
-or-
<paramref name="byteIndex" /> is not a valid index in <paramref name="bytes" />.</exception>