dotnet学习笔记2

 2009.11.16

 

1. 索引器 就是能使访问类跟访问数组一样,通过索引去访问,

实现的方法就:  public type this[index]{get{} set{}}

 

示例代码:using System;

namespace ConsoleApplication3
{
class Student
{

private string[] Name = new string[10];
public Student()
{
for (int i = 0; i < 10; i++)
{
Name[i] = i.ToString();
}
}
public string this[int index] {
get
{
string str;
if (index >= 0 && index < 10)
{
str = Name[index];
}
else
{
str = "null";
}

return str;
}
set
{
if(index >= 0&&index < 10)
Name[index] = value;
}
}

 

static void Main(string[] args)
{
Student s = new Student();
s[3] = "jayc";
s[4] = "yaoming";
s[5] = "houston";
for (int i = 0; i < 12; i++)
{
Console.WriteLine(s[i]);
}
}
}
}

 

 

2.析构函数不能有参数,不能有返回值,不能有修饰符,前面是~

比如:~Student()

 

3.关于virtual override abstract new 的总结

 a. 只是当父类的方法是virtual abstract 或者override的时候才可以去 重写。重写的方法要使用关键字 override.

b. 如果父类的方法没有使用上面三个修饰符的任何一个,那么子类就不能去重写,否则会报错,如果还要重写,以便使用多态的话,那就要使用关键字 new。这个时候new  的作用就是忽略父类中的非虚方法。

   class C
    {
        public void Out()
        {
            Console.WriteLine("11");
        }
    }

    class C1 : C
    {
        public new void Out()
        {
            Console.WriteLine("22");
        }
    }

4.运算符重载

1.使用关键字 operator

2.并非所有的运算符都可以重载,不能重载的运算符有 = ?: is 等

3.重载的运算符可由派生类来继承

 

下面看一个经典的运算符重载的例子:

using System;

namespace ConsoleApplication3
{
    class Test1
    {
        public int real;
        public int img;

        public Test1(int real,int img)
        {
            this.real = real;
            this.img = img;
        }

        public static Test1 operator +(Test1 t1,Test1 t2)
        {
            return new Test1( t1.real+t2.real,t1.img+t2.img);
        }

        public override string ToString()
        {
            return String.Format("the number is {0} and {1}",this.real,this.img);
        }
        static void Main(String[] args)
        {
            Test1 t1 = new Test1(3, 5);
            Test1 t2 = new Test1(4, 5);
            Test1 t3 = t1 + t2;
            Console.WriteLine(t3);
        }
    }
}

the number is 7 and 10

 

5.命名空间

知识点1:

namespace A

{

    namespace B

    {

    }

}

 

作用等同于 namespace A.B

知识点2:可以给命名空间取别名

比如:using ns = NamespaceName;   这就是取别名

 

6.

程序集的引用

 

当一个工程需要用到另外一个工程的方法时候,就需要引入对方程序的程序集。

Add Reference  Browse ..找到对方程序的Debug目录,添加dll即可。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值