数据类型和数据类型转换

1.代码折叠  #region   和   #endregion

#region 数据库连接 sqlconnection
        public void SqlConnection() {
            string constring = "Data Source=.;Initial Catalog=test;Integrated Security=True";
            SqlConnection s = new SqlConnection(constring);
            if (s != null)
            {
                string sql="insert into article values('01文章标题','01作者','01描述','01内容')";
                SqlCommand i = new SqlCommand(sql,s);
                s.Open();
                int r = i.ExecuteNonQuery();
                Console.WriteLine("成功插入了{0}条数据",r);
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("连接SQL数据库失败");
                Console.ReadKey();
            }
        }
#endregion
View Code

2.常量和变量常用类型和结构    先声明-->赋值-->使用

常量 const   数据类型前添加关键字const   常量不允许被重新赋值

static void Main()
        {
           const double t=3.14;
            Console.WriteLine("{0}", t);
            Console.ReadKey();
        }
View Code

字符型 char  最多只能存放一个字符,且不能为空  注意单引号

static void Main() {
            char s='1';//最多只能存放一个字符
            Console.WriteLine(s);
            Console.ReadKey();
        }
  
View Code

金钱类型 decimal   

static void Main() {
            decimal money = 5000m;//结尾加m,不需要加单双引号
            Console.WriteLine(money);
            Console.ReadKey();
        }
View Code

 枚举 enum   不能再函数内声明枚举

  enum gender { 男, 女 };
        static void Main()
        {   
           gender t=gender.男 ;
           gender s = gender.女;
            Console.WriteLine("{0}", t);
            Console.ReadKey();
        }
View Code

结构 struct    可以一次性存储多个不同类型的变量

using System;
namespace jiegou
{
         struct jiegou
        {
        public    string _name;
        public int _age;
        public char _gender;
        }
    class dom_jiegou
    {       
        static void Main(string[] args)
        {
            jiegou t;
            t._name ="张三";        
            Console.WriteLine(t._name);
            Console.ReadKey();
        }
    }
}
View Code

 数组   可以一次性存储多个不同类型的变量

 static void Main(string[] args)
        {
            //数组类型[] 数组名=new 数组类型[数组长度]
            //类型[  ]  数组名 = new 类型[ 长度]{ 1,2,3,4,5}; 
            //类型[  ] 数组名=new 类型 [  ]  {1,2,,3,4,5,6};
            //类型 [  ] 数组名={ 1,2,3,4,5,6,7,8,};
            int[] n ={1,2,3,4,5,6};
            for (int i = 0; i < n.Length;i++ )
            {
                Console.WriteLine(n[i]);
            }
            //使用foreach
          foreach(int s in  n){
              Console.WriteLine(s);
          }
            Console.ReadKey();
        }
View Code

3.数据类型转换

枚举强转int类型

 enum gender { 男, 女 };
        static void Main()
        {   
         int t=(int)gender.男 ;
         int s = (int)gender.女;
            Console.WriteLine("{0}", t);//结果为0
            Console.WriteLine("{0}", s);//结果为1
            Console.ReadKey();
        }
View Code

int强转枚举类型

enum gender { 男, 女 };
        static void Main()
        {   
         int t=0 ;
         gender s = (gender)t;
            Console.WriteLine("{0}", s);//结果为男
            Console.ReadKey();
        }
View Code

枚举转换为string类型         关键语法:string t = gender.男.ToString();

 enum gender { 男, 女 };
        static void Main()
        {

            string t = gender.男.ToString();
            Console.WriteLine("{0}", t);//结果为男
            Console.ReadKey();
        }
View Code

string转换枚举类型    关键语法:gender t= (gender) Enum.Parse(typeof(gender),s);//也可以用作int转换

enum gender { 男, 女 };
        static void Main()
        {
            string s="";//枚举中必须有男这个值
          gender t= (gender) Enum.Parse(typeof(gender),s);
          
            Console.WriteLine("{0}", t);//结果为男
            Console.ReadKey();
        }
View Code

自动类型转换(隐式转换)---int--->double   小范围的值数据类型转大的

 static void Main() {
            int t = 10;
            double s = t;
            Console.WriteLine(s);
            Console.ReadKey();
        }
View Code

强制类型转换(显示类型转换)---double    大范围的值的类型转小的需使用强制类型转换(会丢失精度)

 static void Main()
        {
           double t = 10.12;
            int s = (int)t;//变量钱添加要转换的类型
            Console.WriteLine(s);
            Console.ReadKey();
        }
View Code

4.类型不兼容使用convert类型转换  面上要过的去,得看起来像个数字不能是string="123abc";否则程序会抛异常;可以使用try{可能异常的代码} catch{解决方法}

 static void Main()
        {
            string s = "123";
            int t = Convert.ToInt32(s);
            double i = Convert.ToDouble(s);
            Console.WriteLine("转换为int类型{0}", t);
            Console.WriteLine("转换为double类型{0}", i);

            int ss = 123;
            string tt = Convert.ToString(ss);
            Console.WriteLine("转换为string类型{0}", tt);
            Console.ReadKey();
        }
View Code
时间就像海绵里的水,只要你愿意挤,总还是有的——鲁迅
原文地址:https://www.cnblogs.com/syzly/p/6655267.html