Home > Backend Development > C#.Net Tutorial > Byte structures in C#

Byte structures in C#

WBOY
Release: 2023-09-04 10:25:10
forward
1186 people have browsed it

C# 中的字节结构

Byte Struct represents an 8-bit unsigned integer in C#. The following are the fields:

Serial number Fields and descriptions
1 MaxValue

represents the maximum possible value of Byte. This field is a constant.

2 MinValue

represents the smallest possible value of Byte field is constant.

Following are some of the methods −

##3456
Sr.no Field & Description
1 CompareTo(Byte)

Compares this instance to a specified 8-bit unsigned integer and returns an indication of their relative values.

2 CompareTo(Object)

Compare this instance with the specified object compare and returns an indication of their relative values.

Equals(Byte)Returns a value indicating whether this instance and a The specified Byte objects represent the same value.

Equals(Object)Returns a value indicating whether this instance Equal to the specified object.

GetHashCode() Returns the hash code for this instance.

GetTypeCode().Returns the TypeCode of the value type Byte.

Example

Demonstration

using System;
public class Demo {
   public static void Main() {
      string str = "186";
      try {
         byte val = Byte.Parse(str);
         Console.WriteLine(val);
      }
      catch (OverflowException) {
         Console.WriteLine("Out of range of a byte.", str);
      }
      catch (FormatException) {
         Console.WriteLine("Out of range of a byte.", str);
      }

   }
}
Copy after login

Output

This will produce the following output−

186
Copy after login

Example

Let’s look at another example −

Live Demonstration

using System;
public class Demo {
   public static void Main() {
      byte[] arr = { 0, 10, 50, 90, 100, 150 };
      foreach (byte b in arr) {
         Console.Write(" ", b.ToString());
         Console.Write(b.ToString("D4") + " ");
         Console.WriteLine(b.ToString("X4"));
      }
   }
}
Copy after login

Output

This will produce the following output−

 0000   0000
 0010   000A
 0050   0032
 0090   005A
 0100   0064
 0150   0096
Copy after login

The above is the detailed content of Byte structures in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template