c#基础学习

类的修饰符
abstract:抽象类,可以继承,不能被实例化
sealed: 封闭类,不能被继承,相当于java的final类型。C#很多内部类都是sealed修饰,比如string类
static: 静态类,不能被继承

//A.cs
public class A{
    
    
	public string getName(){
    
    }
}
//A2.cs   partial 相当于calss A的分身文件
public partial class A{
    
    
	public string getName2(){
    
    }
}

public class Main{
    
    
	public void init(){
    
    
		//调用
		A a = new A();
		a.getName();
		a.getName2();
	}
}

结构体和类有何区别?

Struct:结构体是一种值类型,不具备类的继承多态特性。必须是public,不能指定初始初始值。
Class:类是引用类型。

委托是什么?
委托就是具有相同签名和返回值类型的有序方法列表。详细参考
1.是一种引用类型
2.方法的列表称为调用列表
3.当委托被调用时,它调用列表中的每个方法。

public class TestDelegate
    {
    
    
        public delegate void MyDelegate(string e);  
        //只能在类内部调用
        public event MyDelegate Event1; 
        //可以在类外调用 
        public MyDelegate Event2;
        //声明一个方法,调用类内部的event,一边外部可以调用类内部的event
        protected void RaiseSampleEvent(string str) 
        {
    
    
            // 调用类内部的event,没有问题
            Event1(str);
        }
    } 
    
	//外部的调用
	public void Main()
	{
    
    
		TestDelegate test= new TestDelegate();
		//给delegate赋值
		test.Event2 += TestEvent;  
		// 调用delegate,没有问题 
		test.Event2("e");          
		// 给event赋值,这没有问题
		publisher.Event1 += TestEvent; 
		//报错了,定义event修饰符的委托,只能在定义类的内部调用
		test.Event1("e"); 
	}

扩展
在C#3.0中,提供了一个扩展方法的新特性。必须是在静态类中定义静态方法,第一个参数前加this作为修饰符,而紧跟其后的就是扩展类的名称。如下:继承object的类都会有了 MyAdd方法。

扩展功能可以解决一些,比如是sealed类不能被继承而又想扩展方法的问题。

public static class Extend
{
    
    
    public static int MyAdd(this object obj,int a) {
    
    
        return a + 1;
    }
} 

Activator.CreateInstance
传入一个类的名称,返回一个实例。

using System;

namespace ActivatorCreateInstanc{
    
    
	//接口
 	public interface IObjcet{
    
    
  		void printName();
 	}
	//实现类
 	public class ClassExam:IObjcet{
    
    
		  private string name="default name";
		  public ClassExam(){
    
    
		  }
		  public ClassExam(string name){
    
    
		   	this.name =name;
		  }
		 
		  public void printName(){
    
    
		   	Console .WriteLine (this.ToString ()+"'s name is:");
		  	Console .WriteLine (this.name );
		  }
  	}

	 //用Activator .CreateInstance创建函数实例,默认的不带参数的构造函数
   IObjcet obj=(IObjcet)Activator .CreateInstance(System.Type .GetType("ActivatorCreateInstance.ClassExam,ActivatorExample" ),null);
   //System.Type .GetType  命名空间.类名,程序集
   obj.printName();
 
   //调用ClassExam类的另一个带参数构造函数
   IObjcet obj2=(IObjcet)System.Activator .CreateInstance (System.Type .GetType ("ActivatorCreateInstance.ClassExam,ActivatorExample" ),new string []{
    
    "seted new name"});
   obj2.printName ();
}

Path
using System.IO;

        string path = "work/test.json";
				//work
        string a = Path.GetDirectoryName(path);
        Debug.Log(a);
			//test.json
        a =Path.GetFileName(path);
        Debug.Log(a);
			//test
        a = Path.GetFileNameWithoutExtension(path);
        Debug.Log(a);
			//.json
        a = Path.GetExtension(path);
        Debug.Log(a);

映射
using System.Reflection;

	//程序集
	Assembly assembly = Assembly.Load("Assembly-CSharp");
	Type[] arrType = assembly.GetTypes();
	Type t = assembly.GetType("Test");
	PropertyInfo[] infos = t.GetProperties();
   for (int i = 0; i < infos.Length; i++)
   {
    
    
       name = infos[i].Name;
           Debug.Log(infos[i].Name);
   }
   
	//class MethodInfo
	class A{
    
    
		public void add(){
    
    }
		public static void add(int a){
    
    }
	}
	
	Type t = (typeof(A));
	MethodInfo[] infos =  t.GetMethods();
	MethodInfo info=infosp[0];
	//属性  IsPublic   IsStatic name
	info.GetParameters()  

ConvertAll List 类型转换

List<string> liststring = new List<string>();
liststring.Add("0");
liststring.Add("1");
liststring.Add("2");

List<Int32> listint = liststring.ConvertAll(t => Convert.ToInt32(t));

ArrayList 和List区别
ArrayList:不安全类型,会把所有插入的数据都当做Object来处理,装箱拆箱的操作会增加耗时。
List:是泛型类,数据是同一类型。

ref和out区别
相同点:参数的效果一样,都是通过关键字找到定义在主函数里面的变量的内存地址,并通过方法体内的语法改变它的大小。
不同点:ref必须初始化,参数是引用;out 参数必须在函数里赋值,参数为输出参数。out主要用于函数返回多个值。

字符串操作

float i=1.6667f;
//保留小数点后两位(四舍五入)
i.ToString("0.00"); //结果1.67

猜你喜欢

转载自blog.csdn.net/wangwen_22/article/details/126467525