C# .Net 学习积累 《一》

本文介绍了C#中的多种编程技巧,包括base和this关键字的使用、值传递与引用传递的区别、可变参数函数的定义与使用、类型转换的方法、委托及其实现方式、以及索引器的创建等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

base 关键字

见 msdn:http://msdn.microsoft.com/zh-cn/library/hfw7t1ce.aspx

 this 关键字

见 msdn:http://msdn.microsoft.com/zh-cn/library/dk1507sz.aspx

一. 将class封装成dll

在vs tools中点击vs command prompt,输入如下命令:

csc /target:library 文件名.cs

注意: 文件路径要正确。

二. 将主文件和dll关联

csc /reference:dll文件名.dll  主文件名.cs

会生成 主文件名.exe 文件, 执行 exe文件,就可以看到运行结果。

三. 值传递和引用传递,输出参数的调用实例

 

运行结果:

i=0

j=1

k=1

四. 可变参数函数

五. 类型转换

 

六.  委托 

(1)委托,委托链(静态)

 

(2). 匿名方法

 

(3)委托,委托链(动态 类)

 (4) 实例

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

namespace delegate01
{
    class DelegateSample
    {
        public delegate bool ComparisonHandler(int first, int second);
        public static void BubbleSort(int[] items, ComparisonHandler comparisonMethod)
        {
            int i, j, temp;
            for (i = items.Length - 1; i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    if (comparisonMethod(items[j - 1], items[j]))
                    {
                        temp = items[j - 1];
                        items[j - 1] = items[j];
                        items[j] = temp;
                    }
                }
            }
        }
        public static bool GreaterThan(int first, int second)
        {
            return first > second;
        }
        public static bool AlphabeticalGreaterThan(int first, int second)
        {
            int comparison;
            comparison = (first.ToString().CompareTo(second.ToString()));
            return comparison > 0;
        }
        static void Main(string[] args)
        {
            int i;
            int[] items = new int[5];
            for (i = 0; i < items.Length; i++)
            {
                Console.Write("Enter an integer: ");
                items[i] = int.Parse(Console.ReadLine());
            }
            BubbleSort(items, AlphabeticalGreaterThan);
            for (i = 0; i < items.Length; i++)
            {
                Console.WriteLine(items[i]);
            }
            Console.ReadKey();
        }
    }
}

7 索引器

http://www.cnblogs.com/jarod99/archive/2009/01/09/1372453.html

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

namespace basic01
{
    class IndexClass
    {
        private Hashtable name = new Hashtable();
        public string this[int index]
        {
            get { return name[index].ToString(); }
            set { name.Add(index, value); }
        }
        public int this[string aName]
        {
            get
            {
                foreach (DictionaryEntry d in name)
                {
                    if (d.Value.ToString() == aName)
                    {
                        return Convert.ToInt32(d.Key);
                    }
                }
                return -1;
            }
            set
            { name.Add(value, aName); }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IndexClass b = new IndexClass();
            b[500] = "Zhang San";
            b[300] = "Li Si";
            b[400] = "Wang Wu";
            Console.WriteLine("b[500] = " + b["Zhang San"]);
            Console.WriteLine("b[300] = " + b["Li Si"]);
            Console.WriteLine("b[400] = " + b["Wang Wu"]);

            b[600] = "Li Wu";
            b[700] = "Wang Liu";
            Console.WriteLine("b[600] = " + b[600]);
            Console.WriteLine("b[700] = " + b[700]);
            Console.ReadKey();
        }
    }
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值