C的基础上学习C# (第五章--变量的更多内容) .

来源:互联网 发布:mac autotools 编辑:程序博客网 时间:2024/04/30 10:09

    变量类型的转换:

隐式转换:编译器自动完成,可将变量容量小的转化为容量大的类型

                 ushort destinationVar ;

                 char sourceVar = 'a';

                 destinationVar = sourceVar;

显示转换:强制转化 或 使用Convert命令

                  (type)variable; Convert.ToDouble(val); 使用Convert命令进行类型转换始终要进行溢出检查,check 和 unchecked关键字以及项目属性设置不起作用。



枚举:

基本形式:

enum <typeName> :<underlyingType>

{

valuel1 = 1;

valuel2 = 2;

...

}

枚举类型传值给其他类型时 需要类型转换,

<underlyingType>是设定枚举的容量的大小


namespace Ch05Ex02
{
    enum orientation : byte
    {
        north = 1,
        south = 2,
        east = 3,
        west = 4
    }
    class Program
    {
        static void Main(string[] args)
        {
            orientation myDirection = orientation.north;
            Console.WriteLine("myDirection = {0}", myDirection);
            Console.ReadKey();
        }
    }
}

输出的是 my Driection = north

想输出其他类型 需要做如下转换:

byte directionByte;

string directionString;

direcyionByte = (byte)myDirection;

directionString = Convert.ToString(myDirection); //还可以使用变量本身的ToString命令 directionString = myDirection.ToString();

 


 结构体:

机构体的数据成员需要注明访问类型。如:

public struct cx

{

public char n;

public int m;

}


C#结构体居然还能这么用!!!

结构体的对象使用new运算符创建(obj)也可以直接创建单个元素赋值(obj2)这是与类不同的因为类只能使用new创建对象

C#结构体定义程序:

  1. public struct student  
  2. {  
  3.    public int x;  
  4.    public int y;  
  5.    public static int z;  
  6.    public student(int a,int b,int c)  
  7. {  
  8. x=a;  
  9. y=b;  
  10.  student.z=c;  
  11. }  
  12.  
  13. };  
  14. class program  
  15. {  
  16.  public static void Main()  
  17. {  
  18.   student obj=new student(100,200,300);  
  19.   student obj2;  
  20.   obj2.x=100;  
  21.   obj2.y=200;  
  22.   student.z=300;  
  23. }  

在使用类对象和函数使用时,使用的是引用传递,所以字段改变

在使用结构对象和函数使用时,是用的是值传递,所以字段没有改变


 C#的数组定义:

int[ ] myIntArray = {1, 2, 3, 4, 5};

或 int[ ] myIntArray = new int[5];

或两者结合 int myIntArray = new int[arraySize] =  {1, 2, 3, 4, 5};

arraySize 必须是常量  如const int arrayIntSize = 5;


但当不设定初始值的的时候可以用变脸如:

int arraySize = 5;

int myIntArray = new int[arraySize];

namespace Ch05Ex03{    class Program    {        static void Main(string[] args)        {            string[] friendNames = { "Robert Barwell", "Mike", "jay", "Green", "cx" };            int i;            Console.WriteLine("Here are {0} of my friends:", friendNames.Length);            for (i = 0; i < friendNames.Length; i++)            {                Console.WriteLine(friendNames[i]);            }            Console.ReadKey();        }    }}

居然可以用这种方式计算元素的个数
Console.WriteLine("Here are {0} of my friends:", friendNames.Length);

C#太没节操了 有没有... 


foreach是个好东西 可以通过循环的方式 可以遍历数组中的所以元素  用法:

foreach(<baseType> <name> in <array>)

{

//可以用name一次表示数组中的每一个元素;

}

上一段代码 可以改成

namespace Ch05Ex03{    class Program    {        static void Main(string[] args)        {            string[] friendNames = { "Robert Barwell", "Mike", "jay", "Green", "cx" };            int i;            Console.WriteLine("Here are {0} of my friends:", friendNames.Length);            foreach (string friendName in friendNames)            {                Console.WriteLine(friendName);            }            Console.ReadKey();        }    }}
注: foreach 对数组里面的内容是只读的 , 即不可这样操作: friendName = "cxxcxcxc";


多维数组

定义方式 和 一维数组一样 : dobule[,] value = new double[4,5];

foreach 照样适用


数组的数组的定义:

doouble[][] value = new double[3][];

value[0] = new double[4];

value[1] = new double[6];

double[][] value = {new double[] = {1, 2, 3}, new double[] = {3, 4, 1}};

适用foreach时应注意:

foreach(double[] valueinvalue in value)

{

foreach(double values in valueinvalue)

{

;

}

}


字符串 处理

字符串 被视为常量,不允许被修改

可以使用一些命令对其修改, 其本质是另外拷贝了一份再进行修改。

如:  该为数组 

string myString = "fsdffefewf";

char[] myChar = myString.ToCharAray ;

其他命令还有:

<stringName>.ToLower();

<stringName>.ToUpper();

<stringName>.Trim(); //删掉首位空格  

还可以指定要删的首位的内容,先得定义char数组如:

char[] trimChars = {' ', 'e', 's'};

string CX = Console.ReadLine();

CX = CX.ToLower();

CX = CX.Trim(trimChars);

<stringName>.PadX(<desiredLength>); //PadX 为PadLeft或PadRight 在首或尾添加空格

可以设置要添加的内容,但不需新建数组   如: CX = CX.PadRight(10, '-');

<stringName>.Split(' '); 将字符串按空格分割开来 村到字符串数组中 如:

namespace CH05Ex04
{
    class Program
    {
        static void Main(string[] args)
        {
            string myString = "This is a test.";
            char[] separator = { ' ' };
            string[] myWords;
            myWords = myString.Split(separator);
            foreach (string word in myWords)
            {
                Console.WriteLine("{0}", word);
            }
            Console.ReadLine();
        }
    }
}


0 0