玩命加载中 . . .

C sharp three


C sharp(三)

一.泛型

public class Pig<T>{
    public T x;
    public T y;
    public T z;
    public Pig(T x,T y,T z)
    {
        this.x=x;
        this.y=y;
        this.z=z;
    }
}

Pig<string> p1=new Pig<string>("","1","2");
//new()要求有默认构造函数,可以调用new T();
//函数泛型有约束
public static void Attack<T>()
    where T:Hero{
        
    }

二.容器

1.动态数组

List<int> ListInt = new List<int>();//动态数组

2.HashSet的特点:一组不包含重复元素的集合。

HashSet<string> set = new HashSet<string>();
set.Add("1");
set.Add("2");
set.Add("1");
set.Add("3");
set.Add("4");
foreach(var n in set)
{
    Console.WriteLine(n);
}

1出现两次,只显示一次。

3.Queue:先进先出

Queue q = new Queue();
q.Enqueue("A");
q.Enqueue("B");
q.Enqueue("C");
Console.Write("队列中的元素:\t");
foreach(var n in q)
{
    Console.Write(n + "\t");
}
Console.WriteLine();
q.Enqueue("D");
q.Enqueue("E");
Console.Write("增加后队列中的元素:\t");
foreach (var n in q)
{
    Console.Write(n + "\t");
}
Console.WriteLine();
Console.Write("出队元素:");
Console.WriteLine(q.Dequeue());

4.Stack:后进先出

Stack st = new Stack();
st.Push("A");
st.Push("B");
st.Push("C");
st.Push("D");
Console.Write("当前栈:\t");
foreach(var n in st)
{
    Console.Write(n + "\t");
}
Console.WriteLine();
st.Push("E");
st.Push("F");
Console.Write("当前栈:\t");
foreach (var n in st)
{
    Console.Write(n + "\t");
}
Console.WriteLine();
Console.WriteLine("出栈的元素:" + st.Pop());

5.HashTable:key-value的形式来存取数据的

Hashtable table = new Hashtable();
table.Add("名字", "张三");
table.Add("年龄", 10);
table.Add("爱好", "游泳");
foreach(DictionaryEntry entry in table)
{
    Console.Write(entry.Key + ":");
    Console.WriteLine(entry.Value);
}

6.Dictionary< T >:泛型的HashTable.

class Person
{
    public string name;
    public int age;
    public string hobby;

    public override string ToString()
    {
        return "name=" + name + ",age=" + age + ",hobby=" + hobby;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, Person> dictionary = new Dictionary<string, Person>();
        Person person1 = new Person() { name = "张三", age = 20, hobby = "游泳" };
        Person person2 = new Person() { name = "李四", age = 21, hobby = "篮球" };
        dictionary.Add("学生1", person1);
        dictionary.Add("学生2", person2);
        foreach(KeyValuePair<string, Person> pair in dictionary)
        {
            Console.Write(pair.Key + ":");
            Console.WriteLine(pair.Value.ToString());
        }
    }
}

转自https://blog.csdn.net/weixin_42103026/article/details/89080836

三.特性和反射

反射获取类型变量函数等代码信息。

Type t = tc.GetType();//获得该类的Type

//再用Type.GetProperties获得PropertyInfo[],然后就可以用foreach 遍历了
foreach (PropertyInfo pi in t.GetProperties
{
    object value1 = pi.GetValue(tc, null));//用pi.GetValue获得值
    string name = pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
    //获得属性的类型,进行判断然后进行以后的操作,例如判断获得的属性是整数
   if(value1.GetType() == typeof(int))
   {
       //进行你想要的操作
   }
}
         //转载自https://www.cnblogs.com/xishuqingchun/p/3803116.html

四.宏

#define    
定义宏
#undef      
取消已定义的宏
#if        
如果给定条件为真,则编译下面代码
#ifdef      
如果宏已经定义,则编译下面代码
#ifndef    
如果宏没有定义,则编译下面代码
#elif      
如果前面的
#if
给定条件不为真,当前条件为真,则编译下面代码
#endif      
结束一个
#if……#else
条件编译块
#error      
停止编译并显示错误信息
    //转自https://www.cnblogs.com/kuangwu/archive/2013/06/07/3124007.html

文章作者: Pwl
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Pwl !
  目录