您的位置:首页 > 编程语言 > C#

c#中数组,类型转换,using语句的其他用法

2011-07-14 14:31 501 查看
学习内容:数组,类型转换,using语句的其他用法1. 数组:长度固定的数组的使用方法:using System;public class Arrary{ public static void Main(string[] args) { int[] arr=new int[]{1,2,3,4,5};//声明数组的同时对数组进行初始化 int[] arr1 ={ 1, 2, 3, 4, 5 };//声明数组并初始化的简写方法 int[] arr2 = new int[5]; for (int i = 0; i < arr2.Length; i++) { Console.WriteLine("arr2[{0}]={1}", i, arr[i]); //格式化输出 } foreach (int i in arr1)//通过foreach循环遍历数组 { Console.WriteLine(i); } for (int i = 0; i < arr.Length; i++)//通过下标索引输出数组的值 { Console.WriteLine(arr[i]); } }}数组的长度不固定,需要预先输入的数组的使用方法注意:同一个域中的静态成员只能调用本域中的静态成员using System;public class Arrary{ public static void PrintArr(int ArrLength) //必须将次方法指定为静态的 { int[] arr=new int[ArrLength]; for(int i=0;i<arr.Length;i++)//给数组元素赋值 { arr[i]=i; } Console.WriteLine("打印出数组的值:"); for(int i=0;i<arr.Length;i++)//打印出数组元素的值 { Console.WriteLine("arr[{0}]={1}",i,arr[i]); } } public static void Main(string[] args) { int i=1; while(i>0) { Console.WriteLine("请输入数组的长度:"); i=Int32.Parse(Console.ReadLine());//因为Readline()方法返回的是字符,需要转换成整型然后赋值给i PrintArr(i);//调用PrintArr方法 } }}如果不想把PrintArr声明为静态方法,可以把他放在另外一个类中注意:如果在此段代码中再把PrintArr方法加上static关键字则会报错using System;public class SetArr{ public void PrintArr(int ArrLength) { int[] arr = new int[ArrLength]; for (int i = 0; i < arr.Length; i++)//给数组元素赋值 { arr[i] = i; } Console.WriteLine("打印出数组的值:"); for (int i = 0; i < arr.Length; i++)//打印出数组元素的值 { Console.WriteLine("arr[{0}]={1}", i, arr[i]); } }}public class Arrary{ public static void Main(string[] args) { SetArr arr = new SetArr(); int i=1; while(i>0) { Console.WriteLine("请输入数组的长度:"); i=Int32.Parse(Console.ReadLine());//因为Readline()方法返回的是字符,需要转换成整型然后赋值给i arr.PrintArr(i);//调用PrintArr方法 } }}动态数组使用时要用到ArrayList类:注意:Array类可以定义多维数组,而ArrayList类能定义多为数组;Array类的数组长度固定,ArrayList类的长度是可以改变的;在ArrayList类中能对元素进行添加,删除,修改,排序等操作using System;using System.Collections;//ArrayList类包含在此命名空间下public class Arrary{ public static void Main(string[] args) { ArrayList arr = new ArrayList();//声明ArrayList类的实例 string str; while (true) { Console.WriteLine("请添加一个字符串到ArrayList中:"); str=Console.ReadLine(); if(str=="end") break; arr.Add(str);//调用Add方法将字符串添加到arr中 Console.WriteLine(); for(int i=0;i<arr.Count;i++)//输出arr中的元素 { Console.Write(arr[i]); Console.Write(" "); } Console.Write("\n"); } }}二维数组及多维数组的定义及使用int[,] arr=new int[,]{{1,2,3},{4,5,6}}int[,] arr={{1,2,3},{4,5,6}}int[,] arr=new int[2][3]数组的数组:int[,] arr=new int[2][] 指定义了数组的行或列的维度using System;using System.Collections.Generic;using System.Text; namespace ConsoleApplication1{ class Program { public static void Main(string[] args) { int[,] arr = new int[2,3]; for (int i = 0; i < 2; i++) //给数组元素赋值 { for (int j = 0; j < 3; j++) { arr[i, j] = i + j; } } for (int i = 0; i < 2; i++)//输出数组元素 { for (int j = 0; j < 3; j++) { Console.Write(arr[i, j]); } Console.WriteLine(); } } }}2.类型转换类型转换可以分为:隐式转换和显示转换隐式转换自动执行,转换规则精度提高,容量增大:byte->short->int->long->float->double;显示转换有三种方法:第一种方法直接使用(类型名);第二种方法通过Convert类;第三种方法通过类型自身的Parse方法值类型之间的转换:直接在要转换的数据前面加上要转换的数据类型值类型和引用类型之间的转换:装箱和拆箱(尽量避免装箱和拆箱操作,否则会影响系统的性能)Int类型的数据的原型是:system.int32 long类型的数据的原型是:stystem.int64 求各种数据类型的最大值的方法:数据类型.MAXVALUEChecked和unchecked操作符只能作用于一条语句checked( ),当时checked和uncheck语句块能作用于多条语句checked{ }引用类型间的转换:CLR允许将对象强制转换为其他类型或基类型Is操作符返回的是bool类型的true或falseAs操作符返回的是是否和给定的类型兼容,兼容不返回空,不兼容返回空值null注意:as运算符必须和引用类型一起使用小方法:判断一个数据是何种类型的方法:Type t=a.GetType();Console.WriteLine(t.FullName);使用is操作符和as操作符的好处是应用程序不会引发异常3.Using语句的其他用法Using语句除了引用命名空间外,还有一中方法是为了使unmanged资源尽快释放,防止内存泄漏。当然您可以调用dispose方法,但有时会忘记。C#提供了另外一种机制,用using语句确保永远调用dispose方法,常用于数据库操作using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Data.SqlClient;namespace WindowsApplication1{ public partial class Form1 : Form { string str = @"C:\Documents and Settings\Administrator\桌面\1.txt"; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { using(StreamWriter sw=new StreamWriter(str)) { sw.WriteLine("北京理工大学"); sw.WriteLine("计算机科学与技术系"); } using (StreamReader sr = new StreamReader(str)) { this.textBox1.Text = sr.ReadToEnd(); } } //using(SqlConnection conn=new SqlConnection()) //{ // 连接数据库的代码 //} }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: