源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#基础概念二十五问 21-25

  • 时间:2022-05-07 09:46 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#基础概念二十五问 21-25
21.P/Invoke是什么? 答: 在受控代码与非受控代码进行交互时会产生一个事务(transition) ,这通常发生在使用平台调用服务(Platform Invocation Services),即P/Invoke 如调用系统的 API 或与 COM 对象打交道,通过 System.Runtime.InteropServices 命名空间 虽然使用 Interop 非常方便,但据估计每次调用事务都要执行 10 到 40 条指令,算起来开销也不少,所以我们要尽量少调用事务 如果非用不可,建议本着一次调用执行多个动作,而不是多次调用每次只执行少量动作的原则 22.StringBuilder 和 String 的区别? 答: String 在进行运算时(如赋值、拼接等)会产生一个新的实例,而 StringBuilder 则不会。所以在大量字符串拼接或频繁对某一字符串进行操作时最好使用 StringBuilder,不要使用 String 另外,对于 String 我们不得不多说几句: 1.它是引用类型,在堆上分配内存 2.运算时会产生一个新的实例 3.String 对象一旦生成不可改变(Immutable) 3.定义相等运算符(== 和 !=)是为了比较 String 对象(而不是引用)的值 示例:  using System; using System.Collections.Generic; using System.Text; namespace Example22 {     class Program     {         static void Main(string[] args)         {             const int cycle = 10000;             long vTickCount = Environment.TickCount;             String str = null;             for (int i = 0; i < cycle; i++)                 str += i.ToString();             Console.WriteLine("String: {0} MSEL", Environment.TickCount - vTickCount);             vTickCount = Environment.TickCount;             //看到这个变量名我就生气,奇怪为什么大家都使它呢? :)             StringBuilder sb = new StringBuilder();             for (int i = 0; i < cycle; i++)                 sb.Append(i);             Console.WriteLine("StringBuilder: {0} MSEL", Environment.TickCount - vTickCount);             string tmpStr1 = "A";             string tmpStr2 = tmpStr1;             Console.WriteLine(tmpStr1);             Console.WriteLine(tmpStr2);             //注意后面的输出结果,tmpStr1的值改变并未影响到tmpStr2的值             tmpStr1 = "B";             Console.WriteLine(tmpStr1);             Console.WriteLine(tmpStr2);             Console.ReadLine();         }     } } 结果: String: 375 MSEL StringBuilder: 16 MSEL A A A 23.explicit 和 implicit 的含义? 答: explicit 和 implicit 属于转换运算符,如用这两者可以让我们自定义的类型支持相互交换 explicti 表示显式转换,如从 A -> B 必须进行强制类型转换(B = (B)A) implicit 表示隐式转换,如从 B -> A 只需直接赋值(A = B) 隐式转换可以让我们的代码看上去更漂亮、更简洁易懂,所以最好多使用 implicit 运算符。不过!如果对象本身在转换时会损失一些信息(如精度),那么我们只能使用 explicit 运算符,以便在编译期就能警告客户调用端 示例:  using System; using System.Collections.Generic; using System.Text; namespace Example23 {     class Program     {         //本例灵感来源于大话西游经典台词“神仙?妖怪?”--主要是我实在想不出什么好例子了         class Immortal         {             public string name;             public Immortal(string Name)             {                 name = Name;             }             public static implicit operator Monster(Immortal value)             {                 return new Monster(value.name + ":神仙变妖怪?偷偷下凡即可。。。");             }         }         class Monster         {             public string name;             public Monster(string Name)             {                 name = Name;             }             public static explicit operator Immortal(Monster value)             {                 return new Immortal(value.name + ":妖怪想当神仙?再去修炼五百年!");             }         }         static void Main(string[] args)         {             Immortal tmpImmortal = new Immortal("紫霞仙子");             //隐式转换             Monster tmpObj1 = tmpImmortal;             Console.WriteLine(tmpObj1.name);             Monster tmpMonster = new Monster("孙悟空");             //显式转换             Immortal tmpObj2 = (Immortal)tmpMonster;             Console.WriteLine(tmpObj2.name);             Console.ReadLine();         }     } } 结果: 紫霞仙子:神仙变妖怪?偷偷下凡即可。。。 孙悟空:妖怪想当神仙?再去修炼五百年!  24.params 有什么用? 答: params 关键字在方法成员的参数列表中使用,为该方法提供了参数个数可变的能力 它在只能出现一次并且不能在其后再有参数定义,之前可以 示例: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 {     class App     {         //第一个参数必须是整型,但后面的参数个数是可变的。         //而且由于定的是object数组,所有的数据类型都可以做为参数传入         public static void UseParams(int id, params object[] list)         {             Console.WriteLine(id);             for (int i = 0; i < list.Length; i++)             {                 Console.WriteLine(list[i]);             }         }         static void Main()         {             //可变参数部分传入了三个参数,都是字符串类型             UseParams(1, "a", "b", "c");             //可变参数部分传入了四个参数,分别为字符串、整数、浮点数和双精度浮点数数组             UseParams(2, "d", 100, 33.33, new double[] { 1.1, 2.2 });             Console.ReadLine();         }     } } 结果: 1 a c 2 d 100 33.33 System.Double[]  25.什么是反射? 答: 反射,Reflection,通过它我们可以在运行时获得各种信息,如程序集、模块、类型、字段、属性、方法和事件 通过对类型动态实例化后,还可以对其执行操作 简单来说就是用string可以在runtime为所欲为的东西,实际上就是一个.net framework内建的万能工厂 一般用于插件式框架程序和设计模式的实现,当然反射是一种手段可以充分发挥其能量来完成你想做的任何事情(前面好象见过一位高人用反射调用一个官方类库中未说明的函数。。。) 示例: using System; using System.Collections.Generic; using System.Text; namespace Example25Lib {     public class Class1     {         private string name;         private int age;         //如果显式的声明了无参数构造函数,客户端只需要用程序集的CreateInstance即可实例化该类         //在此特意不实现,以便在客户调用端体现构造函数的反射实现         //public Class1()         //{         //}         public Class1(string Name, int Age)         {             name = Name;             age = Age;         }         public void ChangeName(string NewName)         {             name = NewName;         }         public void ChangeAge(int NewAge)         {             age = NewAge;         }         public override string ToString()         {             return string.Format("Name: {0}, Age: {1}", name, age);         }     } } 反射实例化对象并调用其方法,属性和事件的反射调用略去 using System; using System.Collections.Generic; using System.Text; //注意添加该反射的命名空间 using System.Reflection; namespace Example25 {     class Program     {         static void Main(string[] args)         {             //加载程序集             Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "Example25Lib.dll");             //遍历程序集内所有的类型,并实例化             Type[] tmpTypes = tmpAss.GetTypes();             foreach (Type tmpType in tmpTypes)             {                 //获取第一个类型的构造函数信息                 ConstructorInfo[] tmpConsInfos = tmpType.GetConstructors();                 foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)                 {                     //为构造函数生成调用的参数集合                     ParameterInfo[] tmpParamInfos = tmpConsInfo.GetParameters();                      object[] tmpParams = new object[tmpParamInfos.Length];                     for (int i = 0; i < tmpParamInfos.Length; i++)                     {                         tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);                         if (tmpParamInfos[i].ParameterType.FullName == "System.String")                         {                             tmpParams[i] = "Clark";                         }                     }                     //实例化对象                     object tmpObj = tmpConsInfo.Invoke(tmpParams);                     Console.WriteLine(tmpObj);                     //获取所有方法并执行                     foreach (MethodInfo tmpMethod in tmpType.GetMethods())                     {                         //为方法的调用创建参数集合                         tmpParamInfos = tmpMethod.GetParameters();                         tmpParams = new object[tmpParamInfos.Length];                         for (int i = 0; i < tmpParamInfos.Length; i++)                         {                             tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);                             if (tmpParamInfos[i].ParameterType.FullName == "System.String")                             {                                 tmpParams[i] = "Clark Zheng";                             }                             if (tmpParamInfos[i].ParameterType.FullName == "System.Int32")                             {                                 tmpParams[i] = 27;                             }                         }                         tmpMethod.Invoke(tmpObj, tmpParams);                     }                     //调用完方法后再次打印对象,比较结果                     Console.WriteLine(tmpObj);                 }             }             Console.ReadLine();         }     } } 结果: Name: Clark, Age: 0 Name: Clark Zheng, Age: 27 来自: [url=http://www.cnblogs.com/reonlyrun/archive/2007/04/05/csharp_25_question.html]http://www.cnblogs.com/reonlyrun/archive/2007/04/05/csharp_25_question.html[/url]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部