当前位置:网站首页>C data type_ From rookie tutorial

C data type_ From rookie tutorial

2022-07-26 22:07:00 Unity tool man

Value type (Value types)

Value type variables can be assigned directly to a value . They are from class System.ValueType Derived from .

Value types contain data directly . such as int、char、float, They store numbers separately 、 character 、 Floating point numbers . When you declare a int Type , The system allocates memory to store values .

The following table lists them C# 2010 Value types available in :

type describe Range Default
bool Boolean value True or FalseFalse
byte8 Bit unsigned integer 0 To 2550
char16 position Unicode character U +0000 To U +ffff‘\0’
decimal128 The exact decimal value of ,28-29 Effective digits (-7.9 x 1028 To 7.9 x 1028) / 100 To 280.0M
double64 Bit double precision floating point (+/-)5.0 x 10-324 To (+/-)1.7 x 103080.0D
float32 Bit single precision floating point type -3.4 x 1038 To + 3.4 x 10380.0F
int32 Bit signed integer type -2,147,483,648 To 2,147,483,6470
long64 Bit signed integer type -9,223,372,036,854,775,808 To 9,223,372,036,854,775,8070L
sbyte8 Bit signed integer type -128 To 1270
short16 Bit signed integer type -32,768 To 32,7670
uint32 Bit unsigned integer type 0 To 4,294,967,2950
ulong64 Bit unsigned integer type 0 To 18,446,744,073,709,551,6150
ushort16 Bit unsigned integer type 0 To 65,5350

To get the exact size of a type or variable on a specific platform , have access to sizeof Method . expression sizeof(type) Generates a storage size that stores objects or types in bytes . Here's an example to get... On any machine int Storage size of type :

example

using System;

namespace DataTypeApplication
{
    
   class Program
   {
    
      static void Main(string[] args)
      {
    
         Console.WriteLine("Size of int: {0}", sizeof(int));
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed , It will produce the following results :

Size of int: 4

Reference type (Reference types)

object (Object) type

object (Object) type yes C# General type system (Common Type System - CTS) The ultimate base class for all data types in .Object yes System.Object Alias of class . So the object (Object) Type can be assigned to any other type ( Value type 、 Reference type 、 Predefined or user-defined types ) Value . however , Before assigning values , Type conversion is required first .

When a value type is converted to an object type , It's called Packing ; On the other hand , When an object type is converted to a value type , It's called Unpacking .

object obj;
obj = 100; //  This is packing 

dynamic (Dynamic) type

You can store any type of value in a dynamic data type variable . Type checking of these variables occurs at run time .

Syntax for declaring dynamic types :

dynamic <variable_name> = value;

for example :

dynamic d = 20;

Dynamic types are similar to object types , But type checking of object type variables occurs at compile time , Type checking of dynamically typed variables occurs at run time .

character string (String) type

character string (String) type Allows you to assign any string value to a variable . character string (String) The type is System.String Alias of class . It is from the object (Object) Type derived . character string (String) The value of type can be assigned in two forms : Quotation marks and @ quotes .

for example :

String str = "runoob.com";

One @ Quote string :

@"runoob.com";

C# string The string can be preceded by @( Referred to as " Word for word strings ") The escape character (\) Treat as ordinary characters , such as :

string str = @"C:\Windows";

Equivalent to :

string str = "C:\\Windows";

@ You can wrap any line in the string , Line breaks and indented spaces are calculated within the length of the string .

string str = @"<script type=""text/javascript""> <!-- --> </script>";

User defined reference types include :class、interface or delegate.

Pointer types (Pointer types)

Pointer type variables store another type of memory address .C# Pointer in and C or C++ Pointers in have the same function .

Syntax for declaring pointer types :

type* identifier;

for example :

char* cptr;
int* iptr;
原网站

版权声明
本文为[Unity tool man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207262116277730.html