当前位置:网站首页>C foundation in-depth study I

C foundation in-depth study I

2022-07-04 13:41:00 Hua Weiyun

C# Basic in-depth learning 01

Value type , Reference type

Data of value type is stored in the stack of memory , Data of reference type is stored in the heap of memory , The memory unit only stores the address of the object in the heap .

Value type variables directly store the actual data , Variables of reference type store the address of data , That is, the reference to the object .

Value type variables store the value of a variable directly on the stack , Reference type variables store the address of the actual data on the stack , The actual data is stored in the heap .

The value type represents the actual data , The reference type represents a pointer or reference to data stored in the memory heap .

Array

Array Attributes of a class

The following table lists them Array Some of the most commonly used properties in the class :

Serial number attribute & describe
1IsFixedSize Get a value , This value indicates whether the array has a fixed size .
2IsReadOnly Get a value , This value indicates whether the array is read-only .
3Length Get one 32 An integer , This value represents the total number of elements in the array of all dimensions .
4LongLength Get one 64 An integer , This value represents the total number of elements in the array of all dimensions .
5Rank Get the rank of the array ( dimension ).

If you need to know Array A complete list of properties of the class , See Microsoft's C# file .

Array Class method

The following table lists them Array Some of the most commonly used methods in classes :

Serial number Method & describe
1Clear Depending on the type of element , Set the element of a range in the array to zero 、 by false Or for null.
2Copy(Array, Array, Int32) Starting from the first element of the array, copy the elements of a range to the first element position of another array . The length consists of a 32 Bit integers specify .
3CopyTo(Array, Int32) Copies all elements from the current one-dimensional array to the specified index position of a specified one-dimensional array . The index consists of a 32 Bit integers specify .
4GetLength Get one 32 An integer , This value represents the total number of elements in the array of the specified dimension .
5GetLongLength Get one 64 An integer , This value represents the total number of elements in the array of the specified dimension .
6GetLowerBound Gets the lower bound of the specified dimension in the array .
7GetType Gets the type of the current instance . From object (Object) Inherit .
8GetUpperBound Gets the upper bound of the specified dimension in the array .
9GetValue(Int32) Gets the value of a specified position in a one-dimensional array . The index consists of a 32 Bit integers specify .
10IndexOf(Array, Object) Search for the specified object , Returns the first occurrence of the index in the entire one-dimensional array .
11Reverse(Array) Reverse the order of elements in an entire one-dimensional array .
12SetValue(Object, Int32) Sets the value of the element at the specified position in the one-dimensional array . The index consists of a 32 Bit integers specify .
13Sort(Array) Use the... Of each element of the array IComparable Implement to sort the elements of the entire one-dimensional array .
14ToString Returns a string representing the current object . From object (Object) Inherit .

params keyword

When using arrays as formal parameters ,C# Provides params keyword , When calling a method that takes an array as a parameter , You can pass array arguments , You can also pass a set of array elements .params The format used is :

public  Return type   Method name ( params  Type the name []  Array name  )

Structure (Struct)

stay C# in , A structure is a value type data structure . It enables a single variable to store related data of various data types .struct Keyword is used to create a structure .

Structural characteristics

You have used a simple name Books Structure . stay C# The structure in is different from the traditional C or C++ The structure in is different .C# The structure in has the following characteristics :

  • The structure can have methods 、 Field 、 Indexes 、 attribute 、 Operators, methods, and events .
  • Structure defines a constructor , But you can't define destructors . however , You cannot define a parameterless constructor for a structure . Parameter free constructor ( Default ) Is automatically defined , And cannot be changed .
  • Different from class , Structure cannot inherit other structures or classes .
  • Structure cannot be used as the infrastructure of other structures or classes .
  • Structure can implement one or more interfaces .
  • Structure member cannot be specified as abstract、virtual or protected.
  • When you use New When the operator creates a structure object , The appropriate constructor will be called to create the structure . Different from class , The structure may not use New Operators can be instantiated .
  • If not used New The operator , Only after all fields have been initialized , The field is assigned , Objects are used .

class vs structure

Classes and structures have the following basic differences :

  • Class is a reference type , A structure is a value type .
  • Structure does not support inheritance .
  • Structure cannot declare a default constructor .
  • In the constructor of a struct , All fields of the structure must be assigned , Class constructor has no such restriction .
  • A field declared in a structure cannot be given an initial value , Classes can be .
原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041155517424.html