C#学习笔记

1、C#程序结构

  C#是利用命名空间组织起来的,using将命名空间名所标识的命名空间内的类型成员导入当前编译单元中,从而可直接使用每个被导入的类型的标识符。

  Main方法是程序的入口点,C#中所有的Main方法都必须是静态的。

static void Main()
{
}
Main Function

2、变量及类型

  装箱:将值类型转换为引用类型;拆箱:将引用类型转换为值类型。

  定义一个局部变量时,一定要对其初始化;尽可能少定义全局变量。

  布尔类型变量的值只能是true或false,不能与其他类型进行转换。

  Char在C#中表示一个Unicode字符。

  string是String的别名,表示Unicode字符的字符串;生成和构建一个长的字符串时,一定使用StringBuilder类型,而不用string类型。String对象是不可改变的,每次使用String类中的方法时都会在内存中创建一个新的字符串对象,这就需为该对象分配新的空间。若要修改字符串而不创建新的对象,则可使用StringBuilder类。

  集合类型:Array、ArrayList、Hashtable,相关代码:

//数组的合并
int[] arr1 = new int[] { 1, 2, 3, 4, 5 };
int[] arr2 = new int[] { 6, 7, 8, 9, 10 };
int n = arr1.Length + arr2.Length;
int[] arr3 = new int[n];
for (int i = 0; i < arr3.Length; i++)
{
    if (i < arr1.Length)
        arr3[i] = arr1[i];
    else
        arr3[i] = arr2[i - arr1.Length];
}
Console.WriteLine("合并后的一维数组:");
foreach (int i in arr3)
    Console.Write("{0}",i+" ");
Console.WriteLine();

int[,] arr4 = new int[2, 5];
for (int i = 0; i< arr4.Rank;i++)
{
    switch (i)
    {
        case 0:
            {
                for (int j = 0; j < arr1.Length; j++)
                    arr4[i, j] = arr1[j];
                break;
            }
        case 1:
            {
                for(int j=0;j<arr2.Length;j++)
                    arr4[i,j]=arr2[j];
                break;
            }
        }
}
Console.WriteLine("合并后的二维数组:");
for (int i = 0; i < arr4.Rank; i++)
{
    for (int j = 0; j <= arr4.GetUpperBound(arr4.Rank - 1); j++)
        Console.Write(arr4[i,j]+" ");
    Console.WriteLine();
}
            
//数组的拆分
int[,] arr=new int[2,3]{{1,3,5},{2,4,6}};
int[] arr_1=new int[3];
int[] arr_2=new int[3];
for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        switch (i)
        {
            case 0: arr_1[j] = arr[i, j]; break;
            case 1: arr_2[j] = arr[i, j]; break;
        }
    }
}
Console.WriteLine("数组一:");
foreach(int k in arr_1)
    Console.Write(k+" ");
Console.WriteLine();
Console.WriteLine("数组二:");
foreach(int k in arr_2)
    Console.Write(k+" ");
Console.WriteLine();

//ArrayList类
ArrayList List1 = new ArrayList();
for (int i = 0; i < 10; i++)
    List1.Add(i);

int[] arrlist = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ArrayList List2 = new ArrayList(arrlist);
List2.Insert(3, 7);

ArrayList List3 = new ArrayList(10);
for (int i = 0; i < List3.Count; i++)
    List3.Add(i);

Console.WriteLine();

//哈希表类
Hashtable hashtable =new Hashtable();
hashtable.Add("id", "BH0001");
hashtable.Add("name","TM");
hashtable.Add("sex","male");
Console.WriteLine(hashtable.Count);
Console.WriteLine("\t 键\t 值");
foreach (DictionaryEntry dicEntry in hashtable)
    Console.WriteLine("\t"+dicEntry.Key+"\t"+dicEntry.Value);

Console.ReadLine();
View Code

3、属性和方法

  C#属性有两种:在公共语言运行库中定义的属性(特性)、自定义属性(get和set访问器),如下所示:

namespace CSharpTest
{
    [DefaultPropertyAttribute("StationName")]
    class Station
    {
        private string _StationName;
        private double _Lon = 103;
        private double _Lat = 38;
        private Color _color;
        private string _file = string.Empty;
        private Font _font;

        [CategoryAttribute("常规"), DescriptionAttribute("文件名"), ReadOnlyAttribute(true)]
        public string FileName
        {
            get { return _file; }
            set { _file = value; }
        }
        [CategoryAttribute("显示"), DescriptionAttribute("颜色"), DisplayNameAttribute("颜色")]
        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }
        [CategoryAttribute("显示"), DescriptionAttribute("字体")]
        public Font Font
        {
            get { return _font; }
            set { _font = value; }
        }

        public string StationName
        {
            get { return _StationName; }
            set { _StationName = value; }
        }
        public double Lon
        {
            get { return _Lon; }
            set { _Lon = value; }
        }
        public double Lat
        {
            get { return _Lat; }
            set { _Lat = value; }
        }  
    }
}
View Code

  get访问器和方法体相似,须返回属性类型的值;而set访问器类似于返回类型为void的方法,它使用称为value的隐式参数。

  类的实例化对象不能调用静态方法,必须直接使用类名调用。

  C#的事件(Event)和委托(Delegate)机制。

4、结构、类和接口

  结构的实例化可以不使用new运算符;结构可以声明构造函数,但它们须带参数;在结构声明中,除非字段被声明为const或static,否则无法初始化。

  在没有对类实例化前,无法用类名调用类中的方法或字段。

  常用类修饰符:new、private、protected、public、internal(只有其所在的类才能访问)、abstract(抽象类)、sealed(密封类,不允许被继承)。 

  C#类特性:封装、继承(只支持单继承)、多态。

  接口用interface声明,通过类继承来实现。通过接口可实现多重继承,一个类虽只能继承一个基类,但可继承任意接口。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test1
{
    //声明三个接口
    interface IPeople
    {
        string Name
        {
            get;
            set;
        }
        string Sex
        {
            get;
            set;
        }
    }
    interface ITeacher : IPeople
    {
        void teach();
    }
    interface IStudent : IPeople
    {
        void study();
    }
    //类继承自多个接口,并实现接口
    class Program:IPeople,ITeacher,IStudent 
    {
        string name = "";
        string sex = "";
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Sex
        {
            get
            {
                return sex;
            }
            set
            {
                sex = value;
            }
        }
        public void teach()
        {
            Console.WriteLine(Name + " " + Sex +" 教师");
        }
        public void study()
        {        
            Console.WriteLine(Name + " " + Sex +" 学生");
        }
        static void Main(string[] args)
        {
            Program program = new Program();//实例化对象
            ITeacher iteacher = program;
            iteacher.Name = "Tea";
            iteacher.Sex = "";
            iteacher.teach();
            IStudent istudent = program;
            istudent.Name = "Stu";
            istudent.Sex = "";
            istudent.study();
            Console.ReadKey();
        }
    }
}
View Code

  抽象类与接口的:

  • 抽象类与接口都不能直接实例化,但可声明它们的变量;
  • 抽象类中可定义成员的实现,但接口不可以;
  • 抽象类可包含字段、构造和析构函数、静态成员或常量等,但接口中不可以;接口可包含事件、索引器、方法和属性;
  • 接口中的成员必须是公共的;

  声明密封方法时,sealed修饰符总是和override修饰符同时使用。

  使用partial定义分部类,分部类的声明须与其他部分位于同一命名空间。

5、异常处理

  try...catch...finally语句

6、迭代器与泛型

  迭代器(iterator)有时又称游标(cursor),是可返回相同类型的值的有序序列的一段代码,是程序设计的软件设计模式(Design Pattern),可在容器上遍历接口,设计人员无需关心容器的内容。

  C#迭代器代码使用yield return语句依次返回每个元素,yield break语句终止迭代;其返回类型须为IEnumerable或IEnumerator中的任意一种。创建迭代器最常用的方法是对IEnumerator接口实现GetEnumerator方法。迭代器的定义及使用如下所示:

//迭代器的定义
string[] MyFamily = { "父亲", "母亲", "弟弟", "妹妹" };
public System.Collections.IEnumerator GetEnumerator()
{
    for (int i = 0; i < MyFamily.Length; i++)
    {
        yield return MyFamily[i];
    }
}

//迭代器的使用
Family myfamily = new Family();
foreach (string str in myfamily)
{
    richTextBox1.Text += str + "\n";
}
Iterator Code

  泛型是用于处理算法、数据结构的一种编程方法,主要是为了提高代码的重用性。泛型的使用如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Finder
{
    public class Finder
    {
        public static int Find<T>(T[] items, T item)
        {
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i].Equals(item))
                {
                    return i;
                }
            }
            return -1;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int i = Finder.Find<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 6);
            Console.WriteLine("6 在数组中的位置:" + i.ToString());
            Console.ReadKey();
        }
    }
}
View Code

7、IO操作

  文件、文件夹、XML、ini配置文件、注册表、数据库

8、网路编程

  Sockets、Mail、Web

9、线程

10、窗体

  Form窗体、MDI窗体、各种控件(ListBox、ListView、TreeView、PropertyGrid、DockPanel、水晶报表、打印)、GDI+图形图像技术

  ListView控件的使用如下所示:

listView1.View = View.Details;
listView1.GridLines = true;
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); 
this.listView1.Columns.Clear();
this.listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
this.listView1.Columns.Add("编号", 60, HorizontalAlignment.Right);
this.listView1.Columns.Add("姓名", 60, HorizontalAlignment.Right);
this.listView1.Columns.Add("年龄", 60, HorizontalAlignment.Right);
this.listView1.Columns.Add("电话", 60, HorizontalAlignment.Right);
this.listView1.Visible = true;
for (int i = 1; i <= 20; i++)
{
    ListViewItem li = new ListViewItem();
    li.SubItems[0].Text = i.ToString();
    li.SubItems.Add("aaa");
    li.SubItems.Add("25");
    li.SubItems.Add("11223344");
    this.listView1.Items.Add(li);
}
ListView Code

   以上均为本人对《C#从入门到精通》这本书的总结、概括,其中大部分只列出了一些要点。上述若有错误,还请指正,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晨光ABC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值