第2篇 C# 基本数据结构

第2篇 C# 基本数据结构

1、简单变量

1.1 简单变量的类型

 <type>可选的内容和含义

TABLE 3-1: 整型(Integer Types )
 <type>  ALIAS FOR别名  类型  范围between
 sbyte  System.SByte  带符号位字节  -128~127
 byte  System.Byte  字节  0~255
 short  System.Int16  短整型  -32768~32767
 ushort  System.UInt16  无符号位短整型  0~65535
 int  System.Int32  整型  −2147483648 ~ 2147483647
 uint  System.UInt32  无符号位整型  0 ~ 4294967295
 long  System.Int64  长整型  −9223372036854775808 ~9223372036854775807
 ulong   System.UInt64  无符号位长整型  0 ~ 18446744073709551615

 

 

 


  
 

 

 

TABLE 3-2: 浮点型Floating-point Types
<type>  ALIAS FOR  MIN M  MAX M MIN E  MAX E APPROX MIN
VALUE
APPROX
MAX VALUE
float System.Single  0 224  −149  104  1.5 × 10−45 3.4 × 1038
double System.Double 0 253  −1075  970 5.0 × 10−324 1.7 × 10308
decimal  System.Decimal  0  296  −28  0 1.0 × 10−28  7.9 × 1028

 

  

 

 

 

TABLE 3-3: 字符和布尔型(Text and Boolean Types)
<type> ALIAS FOR   ALLOWED VALUES  值的范围
 char   System.Char

 Single Unicode character

一个Unicode字符

 stored as an integer between 0 and 65535

存储0~65535之间的整数

 bool  System.Boolean   Boolean value  true or false
 string  System.String     一组字符A sequence of characters

      

 

 

 

 

 

 

 

 

 

 

1.2 简单变量的类型转换

所有类型的变量,其数值均以0、1代码存储于内存中。由于变量类型的不同,将一个变量的值转移给另一个变量,可能存储空间够用、可能目标类型也有足够的字节数或位数,但结果可能不同。因此需要对数据进行类型转换,而不能将数据从一个变量到另一个变量一对一映射过去。C#中类型转换有两种方式:隐式转换和显式转换。

隐式转换

规则:任何类型A,只要其值域范围完全包含于类型B,则类型A转移到类型B可采用隐式转换。

TABLE 5-1: Implicit Numeric Conversions
TYPE CAN SAFELY BE CONVERTED TO
byte short, ushort, int, uint, long, ulong, float, double, decimal
sbyte short, int, long, float, double, decimal
short int, long, float, double, decimal
ushort int, uint, long, ulong, float, double, decimal
int long, float, double, decimal
uint long, ulong, float, double, decimal
long float, double, decimal
ulong float, double, decimal
float double
char ushort, int, uint, long, ulong, float, double, decimal

 

 

 

 

 

 

 

 

 

 

 

 

显式转换

2、 复杂变量

2.1 枚举

2.1.1 定义枚举

语法一:

//使用enum关键字定义一个枚举,指定枚举名称,列出作为枚举元素的各个变量:
enum <typeName>
{
  <value1>,
  <value2>,
  <value3>,
  ...
  <valueN>
}
//第二步,可以声明这个新类型的变量:
<typeName> <varName>;
//第三步,可以给变量赋值:
<varName> = <typeName>.<value>;

  语法二:默认变量类型为int。下例用<underlyingType>指定其它基本类型。

<underlyingType>可选:byte, sbyte, short, ushort, int, uint, long, and ulong

enum <typeName> : <underlyingType>
{
<value1>,
<value2>,
<value3>,
...
<valueN>
}

 语法三:

上例中,<value1>,<value2>,…,<valueN>默认值为0,1,…,N。下例显示为每个变量赋以新值。

enum <typeName> : <underlyingType>
{
<value1> = <actualVal1>,
<value2> = <actualVal2>,
<value3> = <actualVal3>,
...
<valueN> = <actualValN>
}

 语法四:可以使用一个值作为一个变量的基础值,为多个枚举元素指定相同的值。同时,在下例中,未赋值的变量会自动获得一个初始值,该值等于其上面的变量值+1,连续的则获得以上述初始值开始的一个按1递增的序列。

enum <typeName> : <underlyingType>
{
<value1> = <actualVal1>,
<value2> = <value1>,
<value3>,
...
<valueN> = <actualValN>
}

 举例:下例中,<value2>和<value2>的值相等。

enum <typeName> : <underlyingType>
{
<value1> = <actualVal1>,
<value2>,
<value3> = <value1>,
<value4>,
...
<valueN> = <actualValN>
}

 注意:以下例中循环的方式赋值可能会产生错误。

enum <typeName> : <underlyingType>
{
<value1> = <value2>,
<value2> = <value1>
}

 2.1.2 应用中的强制类型转换

使用枚举元素必须进行显式转换,即使类型相同亦然。

namespace Ch05Ex02
{   
enum orientation : byte   {     north = 1,     south = 2,     east = 3,     west = 4   }   class Program   {     static void Main(string[] args)     {       byte directionByte;
      string directionString;

      orientation myDirection = orientation.north;
      Console.WriteLine("myDirection = {0}", myDirection);

      directionByte = (byte)myDirection;
      directionString = Convert.ToString(myDirection);
      Console.WriteLine("byte equivalent = {0}", directionByte);
      Console.WriteLine("string equivalent = {0}", directionString);
      Console.ReadKey();
    }
  }
}

要把变量值转移到枚举中,同样必须进行显式转换:

myDirection = (orientation)myByte;

字符串的引用

directionString = Convert.ToString(myDirection);

 

directionString = myDirection.ToString();

 

(enumerationType)Enum.Parse(typeof(enumerationType), enumerationValueString);

 

string myString = "north";
orientation myDirection = (orientation)Enum.Parse(typeof(orientation),myString);

 

 

 

2.2 结构

 struct  定义结构。语法:

struct <typeName>
{
  <accessibility> <type> <name>;    //<memberDeclarations>
}

举例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    enum orientation : byte
    {
        north = 1,
        south = 2,
        east = 3,
        west = 4
    }
    struct route
    {
        public orientation direction;
        public double distance;
    }
    class Program
    {
        static void Main(string[] args)
        {
            route myRoute;
            int myDirection = -1;
            double myDistance;
Console.WriteLine(
"1) North\n2) South\n3) East\n4) West"); do { Console.WriteLine("Select a direction:"); myDirection = Convert.ToInt32(Console.ReadLine()); } while ((myDirection < 1) || (myDirection > 4));
Console.WriteLine(
"Input a distance:"); myDistance = Convert.ToDouble(Console.ReadLine()); myRoute.direction = (orientation)myDirection; myRoute.distance = myDistance;
Console.WriteLine(
"myRoute specifies a direction of {0} and a " + "distance of {1}", myRoute.direction, myRoute.distance); Console.ReadKey(); } } }

 

 

 

2.3 数组

2.3.1 一维数组

声明方式:

<baseType>[] <name>;
int[] myIntArray = new int[arraySize];
const int arraySize = 5;

使用方式:

int[] myIntArray = { 5, 9, 10, 2, 99 };    //使用数组前必须先初始化值
int[] myIntArray = new int[5];                //将5个数据全部更新为5

 

2.3.2 多维数组

语法:

A two-dimensional array such as this is declared as follows:
<baseType>[,] <name>;
Arrays of more dimensions simply require more commas:
<baseType>[,,,] <name>;

定位:

double[,] hillHeight = new double[3,4];
double[,] hillHeight = { { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6 } };

To access individual elements of a multidimensional array, you simply specify the indices separated by commas:
hillHeight[2,1]=4

 

2.3.3 数组的数组

语法:

int[][] jaggedIntArray;

2.3.4 foreach Loops(foreach循环)

语法:

A foreach loop enables you to address each element in an array using this simple syntax:
foreach (<baseType> <name> in <array>)
{
// can use <name> for each element
}

2.4 字符串的处理

 

3、函数

4、集合、转换和比较

5、泛型

 

 

 

 

 



 
 
 
 
 
 
 

显示转换

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

集合

 

 

 

集合

posted @ 2015-10-26 11:22  moiska  阅读(1092)  评论(0编辑  收藏  举报