日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />2008-6-7
学习内容:数组,类型转换,using语句的其他用法
遗留问题:类型转换需要进一步的弄清楚<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
学习总结:
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
求各种数据类型的最大值的方法:数据类型.MAXVALUE
Checked和unchecked操作符只能作用于一条语句checked( ),当时checked和uncheck语句块能作用于多条语句checked{ }
引用类型间的转换:CLR允许将对象强制转换为其他类型或基类型
Is操作符返回的是bool类型的true或false
As操作符返回的是是否和给定的类型兼容,兼容不返回空,不兼容返回空值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())
        //{
        //    连接数据库的代码
        //}
    }
}
遗留问题: 类型转换需要进一步的弄清楚,需要巩固的知识点

转载于:https://blog.51cto.com/xiaoshu838/89245

看陈广老师c#参考视频总结(第二篇)相关推荐

  1. 看陈广老师c#参考视频总结(第十篇 完)

    日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> ...

  2. 看陈广老师c#参考视频总结(第三篇)

    日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> ...

  3. 看陈广老师c#参考视频总结

    http://xiaoshu838.blog.51cto.com/433568/89243 转载于:https://blog.51cto.com/1724802/592450

  4. 看陈广老师c#参考视频总结(第八篇)

    日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> ...

  5. 看陈广老师c#参考视频总结(第六篇)

    日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> ...

  6. 看陈广老师c#参考视频总结(第四篇)

    日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /> ...

  7. 陈广老师C#参考视频 方法的参数传递 总结

    方法的参数传递有三种: 1.值参数 方法名称(参数类型,参数名称) 2.引用参数 方法名称(ref 参数类型,参数名称) 3.输出参数 方法名称(out  参数类型,参数名称) 运行结果: i=0 j ...

  8. 陈广老师 C#语言参考视频打包下载地址

    陈广老师 C#语言参考视频下载地址 http://dl.getdropbox.com/u/97203/chenguang.zip 转载于:https://www.cnblogs.com/ycxyyzw ...

  9. C#语言俄罗斯方块源代码(据陈广老师视频)

    以下是我根据陈广老师视频,自己敲的代码,日后会不断更新. using System; using System.Collections.Generic; using System.ComponentM ...

最新文章

  1. c语言 程序 作文,编程之乐作文600字
  2. Spring-Retry重试实现原理
  3. 工作三年,我没给家里一分钱,还把家里掏空了
  4. 安卓四大组件之Service
  5. jQuery的单引号双引号
  6. 黄金分割圆怎么画matlab,黄金分割线画法图解(操作技巧)
  7. 微型计算机一般只具有定点运算功能对吗,大学计算机基础模拟卷2及答案剖析.doc...
  8. 【ASP.NET】HTTP中的 get 和 post 请求
  9. JQuery_九大选择器
  10. UILabel常见属性
  11. oracle 取表字段,oracle 取多级的表字段
  12. 支持断点续传的大文件传输协议
  13. supervisor 使用文档
  14. java汉字转拼音,pinyin4j简单介绍
  15. 陈文灯对话高分学子 定位07年考研数学复习导向
  16. 《考研-数据结构-哈弗曼树-已知某段通信报文内容,对该报文进行哈弗曼编码,并计算平均码长》
  17. flexbox布局详解
  18. 评测 R7 7735HS和i5 12500h选哪个 锐龙R77735HS和i512500h差距
  19. 棒棒的二维数据可视化分类模型
  20. 如何查看windows 10 神州网信政府版的版本信息

热门文章

  1. 等价类划分法的步骤和示例
  2. sin(x)表面下潜藏的一些不为人知的秘密,你想知道吗?
  3. 【工具】Office2013画甘特图
  4. 西安科技大学学分计算机制,干货 | 关于绩点和学分的二三事
  5. 时间复杂度计算-例题集合
  6. 7. EAL parameters(dpdk参数介绍)
  7. gitkraken汉化
  8. Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
  9. 谷歌浏览器使用谷歌搜索总是在当前页面打开新链接
  10. 嵌入式平台音频播放器设计(数据缓冲)