>  기사  >  백엔드 개발  >  C# 버퍼

C# 버퍼

王林
王林원래의
2024-09-03 15:16:44802검색

워드 버퍼의 의미는 메모리에 직접적으로 작용하는 것인데, 관리되지 않고 바이트 배열 형태로 표현된 메모리를 조작하는 과정을 C#에서는 버퍼링이라고 합니다. 그리고 C#의 버퍼 클래스 멤버는 주어진 위치에서 시작하는 한 배열의 바이트를 주어진 위치에서 시작하는 다른 배열로 복사하는 BlockCopy()이고, 배열의 총 바이트 수를 얻을 수 있는 ByteLength()입니다. , 주어진 위치의 바이트를 얻을 수 있는 GetByte() 및 배열의 ​​주어진 위치에 바이트를 설정할 수 있는 SetByte()가 있습니다.

구문:

Buffer.Buffer_member_name(parameters);

여기서 Buffer_member_name은 버퍼 클래스 멤버의 이름이고

매개변수는 전달되는 매개변수입니다.

C# 버퍼 작업

  • 메모리에 대해 직접 작업해야 할 때, 더 구체적으로 관리되지 않는 메모리를 바이트 배열 표현 형식으로 조작하려는 경우 C#에서는 Buffer를 사용합니다.
  • Buffer 클래스는 GetByte(), SetByte(), BlockCopy() 및 ByteLength()와 같은 여러 버퍼 멤버로 구성됩니다.
  • GetByte()는 주어진 위치에서 바이트를 얻을 수 있는 Buffer 클래스의 Buffer 멤버입니다.
  • SetByte()는 배열의 지정된 위치에 바이트를 설정할 수 있는 Buffer 클래스의 Buffer 멤버입니다.
  • BlockCopy()는 주어진 위치에서 시작하는 한 배열의 바이트를 주어진 위치에서 시작하는 다른 배열로 복사하는 버퍼 멤버입니다.
  • ByteLength()는 배열의 총 바이트 수를 얻을 수 있는 Buffer 멤버입니다.

C# 버퍼 클래스 멤버

Buffer 클래스에는 4개의 멤버가 있습니다. 그들은:

 1. BlockCopy()

BlockCopy()는 주어진 위치에서 시작하는 한 배열의 바이트를 주어진 위치에서 시작하는 다른 배열로 복사하는 버퍼 멤버입니다.

2. 바이트길이()

ByteLength()는 배열의 총 바이트 수를 얻을 수 있는 Buffer 멤버입니다.

아래는 예시입니다.

지정된 위치에서 시작하는 한 배열의 바이트를 지정된 위치에서 시작하는 다른 배열로 복사하는 ByteCopy() 클래스의 Buffer 멤버와 ByteLength() 멤버를 보여주는 C# 프로그램:

코드:

using System;
//a class called program is defined
public class program
{
//main method is called
public static void Main(string[] args)
{
//an integer array is defined to store 4 integers
int[] strarray1 = new int[4] { 100, 200, 300, 400 };
//another integer array is defined to store 7 integers
int[] strarray2 = new int[7] { 0,0,0,0,500,600,700 };
Console.Write("The contents of the string array one before performing the Block operation is:\n");
//Bytelength() member of buffer class is used to find the bytelength of th given array
Console.Write("The name of the array is strarray1 and the byte length of the array is :{0}\n", Buffer.ByteLength(strarray1));
for (int j = 0; j < strarray1.Length; j++)
{
Console.Write(strarray1[j]);
Console.Write("\n");
}
Console.Write("The contents of the string array two before performing the Block copy operation is:\n");
Console.Write("The name of the array is strarray2 and the byte length of the array is :{0}\n", Buffer.ByteLength(strarray2));
for (int a = 0; a < strarray2.Length; a++)
{
Console.Write(strarray2[a]);
Console.Write("\n");
}
//Blockcopy() member of buffer class is used to copy the contents of one array starting from the location specified by the second parameter to another array starting from the location specified by fourth parameter and last parameter signifies the bytelength of the first array
Buffer.BlockCopy(strarray1, 0, strarray2, 0,Buffer.ByteLength(strarray1));
Console.Write("The contents of the string array one after performing the block copy operation is:\n");
Console.Write("The name of the array is strarray1 and the contents are :\n");
for (int b = 0; b < strarray1.Length; b++)
{
Console.Write(strarray1[b]);
Console.Write("\n");
}
Console.Write("The contents of the string array two after performing the block copy operation is:\n");
Console.Write("The name of the array is strarray2 and the contents are :\n");
for (int d = 0; d < strarray2.Length; d++) {
Console.Write(strarray2[d]);
Console.Write("\n");
}
}
}

출력:

C# 버퍼

설명: 위 프로그램에는 program이라는 클래스가 정의되어 있습니다. 그런 다음 정수를 저장하기 위해 서로 다른 크기의 두 정수 배열이 정의되는 기본 메서드가 호출됩니다. 첫 번째 배열의 내용이 표시되고 첫 번째 배열의 바이트 길이는 Buffer 클래스의 ByteLength 멤버를 사용하여 표시됩니다. 그런 다음 두 번째 배열의 내용이 표시되고 두 번째 배열의 바이트 길이는 Buffer 클래스의 ByteLength 멤버를 사용하여 표시됩니다. 그런 다음 버퍼 클래스의 Blockcopy() 멤버를 사용하여 두 번째 매개변수로 지정된 위치에서 시작하는 한 배열의 내용을 네 번째 매개변수로 지정된 위치에서 시작하는 다른 배열로 복사하고 마지막 매개변수는 첫 번째 배열의 바이트 길이를 나타냅니다. . 그런 다음 블록 복사 작업 후 첫 번째 배열의 내용이 표시됩니다. 그런 다음 블록 복사 작업 후 두 번째 배열의 내용이 표시됩니다.

3. 세트바이트()

SetByte()는 배열의 지정된 위치에 바이트를 설정할 수 있는 Buffer 클래스의 Buffer 멤버입니다.

4. GetByte()

GetByte()는 주어진 위치에서 바이트를 얻을 수 있는 Buffer 클래스의 Buffer 멤버입니다.

아래는 예시입니다.

SetByte() 클래스의 Buffer 멤버와 GetByte() 멤버를 보여주는 C# 프로그램:

코드:

using System;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
//an inetger array is used to store the integers whose byte values are obtained by using GetByte member of buffer class
int[] arrayname = { 0, 1, 512 };
for (inti = 0; i<Buffer.ByteLength(arrayname); i++)
{
Console.WriteLine(Buffer.GetByte(arrayname, i));
}
// SetByte member of buffer class is used to set the byte values of the array
Buffer.SetByte(arrayname, 0, 10);
Buffer.SetByte(arrayname, 4, 20);
Buffer.SetByte(arrayname, 8, 30);
// The modified array after using SetByte member of the Buffer class is displayed
Console.WriteLine("The modified array after using SetByte member of the Buffer class is:");
for (inti = 0; i<Buffer.ByteLength(arrayname); i++)
{
Console.WriteLine(Buffer.GetByte(arrayname, i));
}
}
}

출력:

C# 버퍼

설명: 위 프로그램에는 check라는 클래스가 정의되어 있습니다. 그런 다음 버퍼 클래스의 GetByte 멤버를 사용하여 바이트 값을 얻은 정수를 저장하는 데 정수 배열이 사용되는 기본 메서드가 호출됩니다. 그런 다음 버퍼 클래스의 SetByte 멤버를 사용하여 배열의 바이트 값을 설정합니다. 그런 다음 Buffer 클래스의 SetByte 멤버를 사용한 후 수정된 배열이 표시됩니다. 출력은 위의 스냅샷에 표시됩니다.

결론

이 튜토리얼에서는 프로그래밍 예제와 출력을 통해 정의, 구문, 작업, 버퍼 클래스 멤버를 통해 C#의 버퍼 개념을 이해합니다.

추천기사

C# 버퍼에 대한 안내입니다. 여기에서는 C# 버퍼 소개와 예제 및 코드 구현과 함께 작업하는 방법에 대해 설명합니다. 더 자세히 알아보려면 다른 추천 기사를 살펴보세요. –

  1. C#의 난수 생성기란 무엇인가요?
  2. Java의 정적 생성자 | 근무 | 애플리케이션
  3. C#의 TextWriter | 예시
  4. C#에서 정적 생성자를 어떻게 사용하나요?

위 내용은 C# 버퍼의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C# 이름다음 기사:C# 이름