当前位置:编程学习 > C#/ASP.NET >>

C#变量的更多内容

一、类型转换

 1、 隐式转换
2、 显式转换
 
二、枚举
 
1、 定义枚举
Enum typename :underlyingType
{
      Value1,
      Value2,
      Value3,
….
}
枚举的基本类型可是bytes,sbyte,short,ushort,int,uint,long,ulong
 例:
  
\
namespace Ch05Ex02

{

    //定义枚举

enum oritentation : byte

    {

        north = 1,

        south = 2,

        east = 3,

        west = 4,

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            byte diByte;

            string diString;

            diByte = (byte)oritentation.north;//显示转换,即使oritentation基本类型是byte,仍需使用(byte)数据类型转换

            diString = Convert.ToString(oritentation.north);//获得枚举的字符串值,也可以使用ToString()命令

            Console.WriteLine("Byte = {0}", diByte);

            Console.WriteLine("String = {0}", diString);

            Console.ReadKey();

        }

    }

}
 
三、结构
 
结构就是由几个数据组成的数据结构,这些数据可能有不同的类型。
 
定义结构
Struct <typename>
{
     <访问修饰符> <基本类型> <名字>
}
 
四、数组
 
声明数组
<basetype>[] <name>
<basetype>可以使任何类型
数组必须在访问之前初始化,初始化分两种
 
1,以字面形式指定数组内容,也可以指定数组大小、
 例:int[] myIntArray = {5,9,,1,30};
2,使用关键字new初始化所有的数组元素
 例:int[] myIntArray = new int[5];
 
多维数组
 
<basetype>[,] <name> //定义二维数组
多维数组只需更能多的逗号,例如:
<basetype>[,,,,] <name> //声明了一个4维数组
 
五、字符串的处理
 
<string>.ToLower() 把字符串转换为大写
<string>.ToUpper() 把字符串转换为小写
<string>.Trim() 删除输入字符串中的空格
<string>.TrimStart() 删除字符串前面的空格
<string>.TrimEnd() 删除字符串后面的空格
<string>.PadLeft() 在字符串的左边添加空格
<string>.PadRight() 在字符串的右边添加空格
<string>.Split() 把字符串转换为string数组
<string>.Replace(string oldValue , string newValue ) 把string替换成其他指定string补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,