Byte 構造体
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
8 ビット符号なし整数を表します。
public value class System::Byte : IComparable, IComparable<System::Byte>, IConvertible, IEquatable<System::Byte>, IFormattable
public value class System::Byte : IComparable, IComparable<System::Byte>, IConvertible, IEquatable<System::Byte>, ISpanFormattable
public value class System::Byte : IComparable, IConvertible, IFormattable
public value class System::Byte : IComparable, IComparable<System::Byte>, IEquatable<System::Byte>, IFormattable
public struct Byte : IComparable, IComparable<byte>, IConvertible, IEquatable<byte>, IFormattable
public struct Byte : IComparable, IComparable<byte>, IConvertible, IEquatable<byte>, ISpanFormattable
[System.Serializable]
public struct Byte : IComparable, IConvertible, IFormattable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Byte : IComparable, IComparable<byte>, IConvertible, IEquatable<byte>, IFormattable
public struct Byte : IComparable, IComparable<byte>, IEquatable<byte>, IFormattable
type byte = struct
interface IConvertible
interface IFormattable
type byte = struct
interface IConvertible
interface ISpanFormattable
interface IFormattable
[<System.Serializable>]
type byte = struct
interface IFormattable
interface IConvertible
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type byte = struct
interface IFormattable
interface IConvertible
type byte = struct
interface IFormattable
Public Structure Byte
Implements IComparable, IComparable(Of Byte), IConvertible, IEquatable(Of Byte), IFormattable
Public Structure Byte
Implements IComparable, IComparable(Of Byte), IConvertible, IEquatable(Of Byte), ISpanFormattable
Public Structure Byte
Implements IComparable, IConvertible, IFormattable
Public Structure Byte
Implements IComparable, IComparable(Of Byte), IEquatable(Of Byte), IFormattable
- 継承
- 属性
- 実装
注釈
Byte は、0 (定数で表されます Byte.MinValue ) から 255 (定数で表されます) までの値を持つ符号なし整数を表す、変更できない値型です Byte.MaxValue 。 .NET には、-128 ~ 127 の範囲の値を表す、符号付き8ビット整数値型も含まれてい SByte ます。
バイト値のインスタンス化
Byte値はいくつかの方法でインスタンス化できます。
変数を宣言 Byte し、データ型の範囲内のリテラル整数値を割り当てることができ Byte ます。 次の例では、2つの Byte 変数を宣言し、その値をこのように代入します。
byte value1 = 64; byte value2 = 255;
let value1 = 64uy let value2 = 255uy
Dim value1 As Byte = 64 Dim value2 As Byte = 255
バイト以外の数値をバイトに割り当てることができます。 これは縮小変換であるため、C# と F # の cast 演算子、またはがオンの場合は Visual Basic の変換メソッドが必要です
Option Strict
。 バイト以外の値が Single 、 Double 小数部分を含む、、またはの値の場合 Decimal 、その小数部の処理は、変換を実行しているコンパイラによって異なります。 次の例では、変数にいくつかの数値を代入し Byte ます。int int1 = 128; try { byte value1 = (byte) int1; Console.WriteLine(value1); } catch (OverflowException) { Console.WriteLine("{0} is out of range of a byte.", int1); } double dbl2 = 3.997; try { byte value2 = (byte) dbl2; Console.WriteLine(value2); } catch (OverflowException) { Console.WriteLine("{0} is out of range of a byte.", dbl2); } // The example displays the following output: // 128 // 3
let int1 = 128 try let value1 = byte int1 printfn $"{value1}" with :? OverflowException -> printfn $"{int1} is out of range of a byte." let dbl2 = 3.997 try let value2 = byte dbl2 printfn $"{value2}" with :? OverflowException -> printfn $"{dbl2} is out of range of a byte." // The example displays the following output: // 128 // 3
Dim int1 As Integer = 128 Try Dim value1 As Byte = CByte(int1) Console.WriteLine(value1) Catch e As OverflowException Console.WriteLine("{0} is out of range of a byte.", int1) End Try Dim dbl2 As Double = 3.997 Try Dim value2 As Byte = CByte(dbl2) Console.WriteLine(value2) Catch e As OverflowException Console.WriteLine("{0} is out of range of a byte.", dbl2) End Try ' The example displays the following output: ' 128 ' 4
クラスのメソッドを呼び出して、 Convert サポートされている任意の型を値に変換でき Byte ます。 ではインターフェイスがサポートされているため、これが可能です Byte IConvertible 。 次の例は、値の配列を値に変換する方法を示してい Int32 Byte ます。
int[] numbers = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue }; byte result; foreach (int number in numbers) { try { result = Convert.ToByte(number); Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", number.GetType().Name, number, result.GetType().Name, result); } catch (OverflowException) { Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", number.GetType().Name, number); } } // The example displays the following output: // The Int32 value -2147483648 is outside the range of the Byte type. // The Int32 value -1 is outside the range of the Byte type. // Converted the Int32 value 0 to the Byte value 0. // Converted the Int32 value 121 to the Byte value 121. // The Int32 value 340 is outside the range of the Byte type. // The Int32 value 2147483647 is outside the range of the Byte type.
open System let numbers = [| Int32.MinValue; -1; 0; 121; 340; Int32.MaxValue |] for number in numbers do try let result = Convert.ToByte number printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}." with :? OverflowException -> printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type." // The example displays the following output: // The Int32 value -2147483648 is outside the range of the Byte type. // The Int32 value -1 is outside the range of the Byte type. // Converted the Int32 value 0 to the Byte value 0. // Converted the Int32 value 121 to the Byte value 121. // The Int32 value 340 is outside the range of the Byte type. // The Int32 value 2147483647 is outside the range of the Byte type.
Dim numbers() As Integer = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue } Dim result As Byte For Each number As Integer In numbers Try result = Convert.ToByte(number) Console.WriteLIne("Converted the {0} value {1} to the {2} value {3}.", _ number.GetType().Name, number, _ result.GetType().Name, result) Catch e As OverflowException Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", _ number.GetType().Name, number) End Try Next ' The example displays the following output: ' The Int32 value -2147483648 is outside the range of the Byte type. ' The Int32 value -1 is outside the range of the Byte type. ' Converted the Int32 value 0 to the Byte value 0. ' Converted the Int32 value 121 to the Byte value 121. ' The Int32 value 340 is outside the range of the Byte type. ' The Int32 value 2147483647 is outside the range of the Byte type.
またはメソッドを呼び出して、 Parse TryParse 値の文字列形式をに変換でき Byte Byte ます。 文字列には、10進数または16進数のいずれかを含めることができます。 次の例は、10進数と16進数の文字列の両方を使用した解析操作を示しています。
string string1 = "244"; try { byte byte1 = Byte.Parse(string1); Console.WriteLine(byte1); } catch (OverflowException) { Console.WriteLine("'{0}' is out of range of a byte.", string1); } catch (FormatException) { Console.WriteLine("'{0}' is out of range of a byte.", string1); } string string2 = "F9"; try { byte byte2 = Byte.Parse(string2, System.Globalization.NumberStyles.HexNumber); Console.WriteLine(byte2); } catch (OverflowException) { Console.WriteLine("'{0}' is out of range of a byte.", string2); } catch (FormatException) { Console.WriteLine("'{0}' is out of range of a byte.", string2); } // The example displays the following output: // 244 // 249
let string1 = "244" try let byte1 = Byte.Parse string1 printfn $"{byte1}" with | :? OverflowException -> printfn $"'{string1}' is out of range of a byte." | :? FormatException -> printfn $"'{string1}' is out of range of a byte." let string2 = "F9" try let byte2 = Byte.Parse(string2, System.Globalization.NumberStyles.HexNumber) printfn $"{byte2}" with | :? OverflowException -> printfn $"'{string2}' is out of range of a byte." | :? FormatException -> printfn $"'{string2}' is out of range of a byte." // The example displays the following output: // 244 // 249
Dim string1 As String = "244" Try Dim byte1 As Byte = Byte.Parse(string1) Console.WriteLine(byte1) Catch e As OverflowException Console.WriteLine("'{0}' is out of range of a byte.", string1) Catch e As FormatException Console.WriteLine("'{0}' is out of range of a byte.", string1) End Try Dim string2 As String = "F9" Try Dim byte2 As Byte = Byte.Parse(string2, System.Globalization.NumberStyles.HexNumber) Console.WriteLine(byte2) Catch e As OverflowException Console.WriteLine("'{0}' is out of range of a byte.", string2) Catch e As FormatException Console.WriteLine("'{0}' is out of range of a byte.", string2) End Try ' The example displays the following output: ' 244 ' 249
バイト値に対する操作の実行
型は、 Byte 加算、減算、除算、乗算、減算、否定、単項否定などの標準的な数値演算をサポートします。 他の整数型と同様に、型では、 Byte ビットごと AND
の、、 OR
XOR
、左シフト、および右シフト演算子もサポートされています。
標準の数値演算子を使用して2つの値を比較することも、 Byte メソッドまたはメソッドを呼び出すこともでき CompareTo Equals ます。
また、クラスのメンバーを呼び出して、数値 Math の絶対値を取得する、整数除算から商と剰余を計算する、2つの整数の最大値または最小値を決定する、数値の符号を取得する、数値を丸めるなど、さまざまな数値演算を実行することもできます。
バイトを文字列として表す
型は、 Byte 標準およびカスタムの数値書式指定文字列を完全にサポートします。 (詳細については、「 書式設定型」、「 標準の数値書式指定文字列」、および「 カスタム数値書式指定文字列」を参照してください)。ただし、ほとんどの場合、バイト値は、追加の書式設定や2桁の16進値としてではなく、1桁から3桁の値として表されます。
値を、 Byte 先行ゼロを含まない整数文字列として書式設定するには、パラメーターなしのメソッドを呼び出すことができ ToString() ます。 "D" 書式指定子を使用すると、指定した数の先行ゼロを文字列形式で含めることもできます。 "X" 書式指定子を使用すると、値を Byte 16 進文字列として表すことができます。 次の例では、これら3つの方法で、値の配列の要素の書式を設定 Byte します。
byte[] numbers = { 0, 16, 104, 213 };
foreach (byte number in numbers) {
// Display value using default formatting.
Console.Write("{0,-3} --> ", number.ToString());
// Display value with 3 digits and leading zeros.
Console.Write(number.ToString("D3") + " ");
// Display value with hexadecimal.
Console.Write(number.ToString("X2") + " ");
// Display value with four hexadecimal digits.
Console.WriteLine(number.ToString("X4"));
}
// The example displays the following output:
// 0 --> 000 00 0000
// 16 --> 016 10 0010
// 104 --> 104 68 0068
// 213 --> 213 D5 00D5
let numbers = [| 0; 16; 104; 213 |]
for number in numbers do
// Display value using default formatting.
number.ToString()
|> printf "%-3s --> "
// Display value with 3 digits and leading zeros.
number.ToString "D3"
|> printf "%s "
// Display value with hexadecimal.
number.ToString "X2"
|> printf "%s "
// Display value with four hexadecimal digits.
number.ToString "X4"
|> printfn "%s"
// The example displays the following output:
// 0 --> 000 00 0000
// 16 --> 016 10 0010
// 104 --> 104 68 0068
// 213 --> 213 D5 00D5
Dim numbers() As Byte = { 0, 16, 104, 213 }
For Each number As Byte In numbers
' Display value using default formatting.
Console.Write("{0,-3} --> ", number.ToString())
' Display value with 3 digits and leading zeros.
Console.Write(number.ToString("D3") + " ")
' Display value with hexadecimal.
Console.Write(number.ToString("X2") + " ")
' Display value with four hexadecimal digits.
Console.WriteLine(number.ToString("X4"))
Next
' The example displays the following output:
' 0 --> 000 00 0000
' 16 --> 016 10 0010
' 104 --> 104 68 0068
' 213 --> 213 D5 00D5
また、 Byte メソッドを呼び出し、 ToString(Byte, Int32) メソッドの2番目のパラメーターとして base を指定することで、バイナリ、8進数、10進数、または16進数の文字列として値を書式設定することもできます。 次の例では、このメソッドを呼び出して、バイト値の配列のバイナリ、8進数、および16進数表現を表示します。
byte[] numbers ={ 0, 16, 104, 213 };
Console.WriteLine("{0} {1,8} {2,5} {3,5}",
"Value", "Binary", "Octal", "Hex");
foreach (byte number in numbers) {
Console.WriteLine("{0,5} {1,8} {2,5} {3,5}",
number, Convert.ToString(number, 2),
Convert.ToString(number, 8),
Convert.ToString(number, 16));
}
// The example displays the following output:
// Value Binary Octal Hex
// 0 0 0 0
// 16 10000 20 10
// 104 1101000 150 68
// 213 11010101 325 d5
let numbers = [| 0; 16; 104; 213 |]
printfn "%s %8s %5s %5s" "Value" "Binary" "Octal" "Hex"
for number in numbers do
printfn $"%5i{number} %8s{Convert.ToString(number, 2)} %5s{Convert.ToString(number, 8)} %5s{Convert.ToString(number, 16)}"
// The example displays the following output:
// Value Binary Octal Hex
// 0 0 0 0
// 16 10000 20 10
// 104 1101000 150 68
// 213 11010101 325 d5
Dim numbers() As Byte = { 0, 16, 104, 213 }
Console.WriteLine("{0} {1,8} {2,5} {3,5}", _
"Value", "Binary", "Octal", "Hex")
For Each number As Byte In numbers
Console.WriteLine("{0,5} {1,8} {2,5} {3,5}", _
number, Convert.ToString(number, 2), _
Convert.ToString(number, 8), _
Convert.ToString(number, 16))
Next
' The example displays the following output:
' Value Binary Octal Hex
' 0 0 0 0
' 16 10000 20 10
' 104 1101000 150 68
' 213 11010101 325 d5
10進数以外のバイト値の使用
個々のバイトを10進値として操作するだけでなく、バイト値を使用してビットごとの演算を実行したり、バイト配列を使用したり、バイト値のバイナリ表現または16進表現で操作したりすることもできます。 たとえば、メソッドのオーバーロードは、 BitConverter.GetBytes 各プリミティブデータ型をバイト配列に変換し、 BigInteger.ToByteArray メソッドは BigInteger 値をバイト配列に変換します。
Byte 値は、符号ビットのない大きさだけで、8ビットで表されます。 このことは、値に対してビットごとの演算を実行する場合 Byte や、個々のビットを操作する場合に留意することが重要です。 数値、ブール値、または比較演算を任意の2つの非10進値に対して実行するには、両方の値が同じ表現を使用する必要があります。
2つの値に対して操作を実行すると、 Byte 値は同じ表現を共有するので、結果は正確になります。 これを次の例に示します。この例では、値の最下位ビットがマスクされていることを確認してい Byte ます。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] values = { Convert.ToString(12, 16),
Convert.ToString(123, 16),
Convert.ToString(245, 16) };
byte mask = 0xFE;
foreach (string value in values) {
Byte byteValue = Byte.Parse(value, NumberStyles.AllowHexSpecifier);
Console.WriteLine("{0} And {1} = {2}", byteValue, mask,
byteValue & mask);
}
}
}
// The example displays the following output:
// 12 And 254 = 12
// 123 And 254 = 122
// 245 And 254 = 244
open System
open System.Globalization
let values =
[ Convert.ToString(12, 16)
Convert.ToString(123, 16)
Convert.ToString(245, 16) ]
let mask = 0xFEuy
for value in values do
let byteValue = Byte.Parse(value, NumberStyles.AllowHexSpecifier)
printfn $"{byteValue} And {mask} = {byteValue &&& mask}"
// The example displays the following output:
// 12 And 254 = 12
// 123 And 254 = 122
// 245 And 254 = 244
Imports System.Globalization
Module Example
Public Sub Main()
Dim values() As String = { Convert.ToString(12, 16), _
Convert.ToString(123, 16), _
Convert.ToString(245, 16) }
Dim mask As Byte = &hFE
For Each value As String In values
Dim byteValue As Byte = Byte.Parse(value, NumberStyles.AllowHexSpecifier)
Console.WriteLine("{0} And {1} = {2}", byteValue, mask, _
byteValue And mask)
Next
End Sub
End Module
' The example displays the following output:
' 12 And 254 = 12
' 123 And 254 = 122
' 245 And 254 = 244
一方、符号なしのビットと符号付きビットの両方を扱う場合、 SByte 値が正の値に対して符号と大きさの表現を使用し、負の値に対して2つの補数表現が使用されるため、ビットごとの演算は複雑になります。 意味のあるビットごとの演算を実行するには、値を等価の2つの表現に変換し、符号ビットに関する情報を保持する必要があります。 次の例では、8ビットの符号付きおよび符号なしの値の配列のビット2と4をマスクアウトします。
using System;
using System.Collections.Generic;
using System.Globalization;
public struct ByteString
{
public string Value;
public int Sign;
}
public class Example
{
public static void Main()
{
ByteString[] values = CreateArray(-15, 123, 245);
byte mask = 0x14; // Mask all bits but 2 and 4.
foreach (ByteString strValue in values) {
byte byteValue = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier);
Console.WriteLine("{0} ({1}) And {2} ({3}) = {4} ({5})",
strValue.Sign * byteValue,
Convert.ToString(byteValue, 2),
mask, Convert.ToString(mask, 2),
(strValue.Sign & Math.Sign(mask)) * (byteValue & mask),
Convert.ToString(byteValue & mask, 2));
}
}
private static ByteString[] CreateArray(params int[] values)
{
List<ByteString> byteStrings = new List<ByteString>();
foreach (object value in values) {
ByteString temp = new ByteString();
int sign = Math.Sign((int) value);
temp.Sign = sign;
// Change two's complement to magnitude-only representation.
temp.Value = Convert.ToString(((int) value) * sign, 16);
byteStrings.Add(temp);
}
return byteStrings.ToArray();
}
}
// The example displays the following output:
// -15 (1111) And 20 (10100) = 4 (100)
// 123 (1111011) And 20 (10100) = 16 (10000)
// 245 (11110101) And 20 (10100) = 20 (10100)
open System
open System.Collections.Generic
open System.Globalization
[<Struct>]
type ByteString =
{ Sign: int
Value: string }
let createArray values =
[ for value in values do
let sign = sign value
{ Sign = sign
// Change two's complement to magnitude-only representation.
Value = Convert.ToString(value * sign, 16)} ]
let values = createArray [ -15; 123; 245 ]
let mask = 0x14uy // Mask all bits but 2 and 4.
for strValue in values do
let byteValue = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier)
printfn $"{strValue.Sign * int byteValue} ({Convert.ToString(byteValue, 2)}) And {mask} ({Convert.ToString(mask, 2)}) = {(strValue.Sign &&& (int mask |> sign)) * int (byteValue &&& mask)} ({Convert.ToString(byteValue &&& mask, 2)})"
// The example displays the following output:
// -15 (1111) And 20 (10100) = 4 (100)
// 123 (1111011) And 20 (10100) = 16 (10000)
// 245 (11110101) And 20 (10100) = 20 (10100)
Imports System.Collections.Generic
Imports System.Globalization
Public Structure ByteString
Public Value As String
Public Sign As Integer
End Structure
Module Example
Public Sub Main()
Dim values() As ByteString = CreateArray(-15, 123, 245)
Dim mask As Byte = &h14 ' Mask all bits but 2 and 4.
For Each strValue As ByteString In values
Dim byteValue As Byte = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier)
Console.WriteLine("{0} ({1}) And {2} ({3}) = {4} ({5})", _
strValue.Sign * byteValue, _
Convert.ToString(byteValue, 2), _
mask, Convert.ToString(mask, 2), _
(strValue.Sign And Math.Sign(mask)) * (byteValue And mask), _
Convert.ToString(byteValue And mask, 2))
Next
End Sub
Private Function CreateArray(ParamArray values() As Object) As ByteString()
Dim byteStrings As New List(Of ByteString)
For Each value As Object In values
Dim temp As New ByteString()
Dim sign As Integer = Math.Sign(value)
temp.Sign = sign
' Change two's complement to magnitude-only representation.
value = value * sign
temp.Value = Convert.ToString(value, 16)
byteStrings.Add(temp)
Next
Return byteStrings.ToArray()
End Function
End Module
' The example displays the following output:
' -15 (1111) And 20 (10100) = 4 (100)
' 123 (1111011) And 20 (10100) = 16 (10000)
' 245 (11110101) And 20 (10100) = 20 (10100)
フィールド
MaxValue |
Byte の最大有効値を表します。 このフィールドは定数です。 |
MinValue |
Byte の最小有効値を表します。 このフィールドは定数です。 |
メソッド
CompareTo(Byte) |
指定した 8 ビット符号なし整数とこのインスタンスを比較し、これらの相対値を示す値を返します。 |
CompareTo(Object) |
指定したオブジェクトとこのインスタンスを比較し、これらの相対値を示す値を返します。 |
Equals(Byte) |
このインスタンスと指定した Byte オブジェクトが同じ値を表しているかどうかを示す値を返します。 |
Equals(Object) |
このインスタンスが指定されたオブジェクトに等しいかどうかを示す値を返します。 |
GetHashCode() |
このインスタンスのハッシュ コードを返します。 |
GetTypeCode() | |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャ固有の書式の数値のスパン表現を、それと等価の Byte に変換します。 |
Parse(String) |
数値の文字列形式を、それと等価の Byte に変換します。 |
Parse(String, IFormatProvider) |
指定されたカルチャ固有の書式で表現された文字列形式の数値を、それと等価の Byte に変換します。 |
Parse(String, NumberStyles) |
指定のスタイルで表現された数値の文字列形式を、それと等価な Byte に変換します。 |
Parse(String, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャ固有の書式の数値の文字列形式を、それと等価の Byte に変換します。 |
ToString() |
現在の Byte オブジェクトの値を等価の文字列形式に変換します。 |
ToString(IFormatProvider) |
指定したカルチャ固有の書式設定情報を使用して、現在の Byte オブジェクトの値をそれと等価な文字列形式に変換します。 |
ToString(String) |
指定した書式を使用して、現在の Byte オブジェクトの値をそれと等価な文字列形式に変換します。 |
ToString(String, IFormatProvider) |
指定した形式およびカルチャ固有の書式設定情報を使用して、現在の Byte オブジェクトの値をそれと等価の文字列形式に変換します。 |
TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在の 8 ビットの符号なし整数インスタンスの値を、指定した文字スパンに書式設定しようとします。 |
TryParse(ReadOnlySpan<Char>, Byte) |
数値のスパン表現の、等価の Byte への変換を試み、変換に成功したかどうかを示す値を返します。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte) |
指定したスタイルおよびカルチャ固有の書式の数値のスパン表現を、それと等価の Byte に変換します。 戻り値は変換が成功したか失敗したかを示します。 |
TryParse(String, Byte) |
数値の文字列形式を対応する Byte 表現に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。 |
TryParse(String, NumberStyles, IFormatProvider, Byte) |
指定したスタイルおよびカルチャ固有の書式の数値の文字列形式を、それと等価の Byte に変換します。 戻り値は変換が成功したか失敗したかを示します。 |
明示的なインターフェイスの実装
適用対象
スレッド セーフ
この型のすべてのメンバーは、スレッドセーフです。 インスタンスの状態を変更するように見えるメンバーは、実際には新しい値で初期化された新しいインスタンスを返します。 他の型と同様に、この型のインスタンスを含む共有変数の読み取りと書き込みは、スレッドセーフを保証するためにロックによって保護される必要があります。