Unity basic learning 20, common functions of c# algorithm

1.Math related

Math.Abs() Calculates the absolute
value Math.Acos() Calculates the arc cosine
Math.Asin() Calculates the arc sine
Math.Atan() Calculates the arc tangent
Math.Atan2() Calculates the angle from the x-axis to the point
Math .Ceil() Rounds a number up to the nearest integer
Math.Cos() Calculates the cosine
Math.Exp() Calculates the exponent
Math.Floor() Rounds the number down to the nearest integer
Math.Log( ) Calculates the natural logarithm
Math.Max() Returns the greater of two integers
Math.Min() Returns the smaller of two integers
Math.Pow() Calculates x to the power of y
Math.Random() Returns A pseudorandom number between 0.0 and 1.0
Math.Round() Rounds to the nearest integer
Math.Sin() Computes the sine
Math.Sqrt() Computes the square root
Math.Tan() Computes the tangent
Math.E = 2.718 ohms Euler constant, base of natural logarithm (approx. 2.718)
Math.LN2: natural logarithm of 2 (approximately 0.693)
Math.LOG2E base 2 logarithm of e (approximately 1.442)
Math.LN2 Natural logarithm of 10 (approximately 2.302)
Math.LOG10E Base 10 logarithm of e (approximately 0.434)
Math.PI the ratio of the circumference of a circle to its diameter (approximately 3.14159)
Math.SQRT1_2 the reciprocal of the square root of 1/2 (approximately 0.707)
Math.SQRT2 the square root of 2 (approximately 1.414)

2. Type conversion related

In C#, type casting comes in two flavors:

  1. Implicit type conversions These conversions are C#'s default conversions performed in a safe manner without loss of data. For example: converting from a small integer type to a large integer type, from a derived class to a base class.
  2. Explicit type conversion Explicit type conversion is cast. Explicit conversions require a casting operator, and casting causes data loss.

Implicit numeric conversion is actually a conversion from a low-precision numeric type to a high-precision numeric type. Implicit numeric conversions include the following:

  • From sbyte type to short, int, long, float, double or decimal type
  • From byte type to short, ushort, int, uint, long, ulong, float, double or decimal type
  • From short type to int, long, float, double or decimal type
  • From ushort type to int, uint, long, ulong, float, double or decimal type
  • From int type to long, folat, double, or decimal type
  • From uint type to float, ulong, long, double or decimal type
  • From long type to float, double or decimal type
  • From ulong type to float, double or decimal type
  • From char type to ushort, int, uint, long, ulong, float, double or decimal type
  • From float type to double type

Explicit type conversions are casts. Explicit conversions require a casting operator, and casting causes data loss.

Mandatory type conversion is mainly used to convert a data type with a large storage range into a data type with a small storage range.

Conversion syntax:

DataTypeVariableName = (DataType) VariableName or Value

double dbl_num=123456.456;
int k=(int) dbl_num;//此处运用了强制转换

Data type conversion method

2.1 Parse method

Basic data type.Parse(value of type string);


int num=int.Parse(value);

 2.2  Convert method

Data type variable name = convert.To data type (variable name);

The commonly used type conversion methods of the Convert class are shown in the following table:

 2.3  Boxing and unboxing

Boxing: The operation of converting a value type to a reference type.

Unboxing: Conversion of reference types to value types accordingly.

Boxing and unboxing allow you to connect value types to reference types by allowing any value of the value type to be converted to and from a value of type Object.

int val=100;
object obj=val;
Console.WriteLine("对象的值={0}",obj);
//这是一个装箱的过程,是将值类型转换为引用类型的过程
 
int num=(int)obj;
Console.WriteLine("num:{0}",num);
//这是一个拆箱的过程,有引用类型转换为值类型的过程

 

3. Data structure related

3.1 Array array

double[] balance = new double[10];
double[] balance = { 2340.0, 4523.69, 3421.0};
//不限定长度
int [] marks = new int[] { 99, 98, 92, 97, 95};

Length: marks. Length;

3.2 Two-dimensional array

definition:

int[,] map;

initialization:

//动态初始化
int[,] map = new int[5,3];
int[,] map = new int[,]{
   
   {1,0,1},{3,0,5}};

//静态初始化
int[,] map = {
   
   {1,0,1},{3,0,5}};

//访问
map[3,2]

length:

  • Total length (the number of elements in the two-dimensional array): Array.Length
  • The length of the first dimension: Array.GetLength(0)
  • The length of the second dimension: Array.GetLength(1)
for (int i = 0; i < array.GetLength(0); i++) 
{ 
    for (int j = 0; j < array.GetLength(1); j++) 
    { 
        Console.Write(array[i,j] + "\t"); 
    } 
    //换⾏ 
    Console.WriteLine(); 
}

Guess you like

Origin blog.csdn.net/u013617851/article/details/124978296