找到 34423 篇文章,关于编程

获取 C# 中此实例的哈希码

AmitDiwan
更新于 2019年12月10日 09:54:27

175 次浏览

要获取此实例的哈希码,代码如下所示:示例 在线演示using System; public class Demo { public static void Main() { string[] arr = {"tom", "amit", "kevin", "katie"}; Type t1 = arr.GetType(); Type t2 = t1.GetElementType(); Console.WriteLine("类型 = "+t2.ToString()); Console.WriteLine("哈希码 = "+t1.GetHashCode()); } }输出这将产生以下输出:类型 = System.String 哈希码 = 9144789示例让我们看另一个示例: 在线演示using System; public class Demo { enum Vehicle {Car, Bus, Bike, Airplane} public static void ... 阅读更多

在 C# 中向哈希表添加元素

AmitDiwan
更新于 2019年12月10日 09:53:02

254 次浏览

要向哈希表添加元素,代码如下所示:示例 在线演示using System; using System.Collections; public class Demo { public static void Main(){ Hashtable hash = new Hashtable(10); hash.Add("1", "A"); hash.Add("2", "B"); hash.Add("3", "C"); hash.Add("4", "D"); hash.Add("5", "E"); hash.Add("6", "F"); hash.Add("7", "G"); hash.Add("8", "H"); hash.Add("9", "I"); hash.Add("10", "J"); Console.WriteLine("哈希表键值对..."); foreach(DictionaryEntry entry in hash){ ... 阅读更多

获取当前类型 C# 中的特定字段

AmitDiwan
更新于 2019年12月10日 09:51:35

368 次浏览

要获取 C# 中当前类型的特定字段,代码如下所示:示例 在线演示using System; using System.Reflection; public class Demo { public static void Main() { Type type = typeof(Subject); try { FieldInfo fieldInfo = type.GetField("SubName"); Console.WriteLine("FieldInfo = {0}", fieldInfo); } catch (ArgumentNullException e) { Console.Write("{0}", e.GetType(), e.Message); } } } public class Subject { public string SubName = "Science"; }输出这将产生以下输出:FieldInfo = ... 阅读更多

在 C# 中向 ListDictionary 添加指定的键值对

AmitDiwan
更新于 2019年12月10日 09:49:45

96 次浏览

要向 ListDictionary 添加指定的键值对,代码如下所示:示例 在线演示using System; using System.Collections; using System.Collections.Specialized; public class Demo { public static void Main(){ ListDictionary dict = new ListDictionary(); dict.Add("1", "One"); dict.Add("2", "Two"); dict.Add("3", "Three"); dict.Add("4", "Four"); dict.Add("5", "Five"); Console.WriteLine("ListDictionary 键值对..."); IDictionaryEnumerator demoEnum = dict.GetEnumerator(); while (demoEnum.MoveNext()) Console.WriteLine("键 = " + demoEnum.Key + ", 值 = " + demoEnum.Value); } ... 阅读更多

获取 C# 中当前枚举类型中常量的值数组

AmitDiwan
更新于 2019年12月10日 09:48:24

77 次浏览

要获取当前枚举类型中常量的值数组,代码如下所示:示例 在线演示using System; public class Demo { enum Vehicle {Car, Bus, Bike, Airplane} public static void Main() { try { Type type = typeof(int); string[] str = type.GetEnumNames(); Console.WriteLine("GetEnumNames() 返回常量名称 = " + str); Type type2 = type.GetEnumUnderlyingType(); Console.Write("枚举基础类型 = "+type2); Array arrObj = type.GetEnumValues(); ... 阅读更多

在 C# 中向 HashSet 添加元素

AmitDiwan
更新于 2019年12月10日 09:47:07

251 次浏览

要向 HashSet 添加元素,代码如下所示:示例 在线演示using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args){ HashSet set1 = new HashSet(); set1.Add("A"); set1.Add("B"); set1.Add("C"); set1.Add("D"); set1.Add("E"); set1.Add("F"); set1.Add("G"); set1.Add("H"); Console.WriteLine("HashSet1 中的元素..."); foreach (string res in set1){ Console.WriteLine(res); } HashSet set2 = new HashSet(); ... 阅读更多

获取 C# 中当前类型的字段

AmitDiwan
更新于 2019年12月10日 09:45:21

203 次浏览

要获取当前类型的字段,代码如下所示:示例 在线演示using System; using System.Reflection; public class Demo { public static void Main() { Type type = typeof(System.String); FieldInfo [] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic); Console.WriteLine ("以下是私有字段 ="); foreach (FieldInfo myField in fields) { Console.WriteLine(myField.ToString()); } } }输出这将产生以下输出:以下是私有字段 = Int32 TrimHead Int32 TrimTail Int32 TrimBoth Int32 charPtrAlignConst Int32 alignConst示例让我们看另一个示例: 在线演示using System; using System.Reflection; public class Demo { ... 阅读更多

获取 C# 中指定对象的类型的句柄

AmitDiwan
更新于 2019年12月10日 09:41:48

183 次浏览

要获取指定对象的类型的句柄,代码如下所示:示例 在线演示using System; public class Demo { public static void Main() { Type type1 = typeof(System.Type); RuntimeTypeHandle typeHandle = Type.GetTypeHandle(type1); Type type = Type.GetTypeFromHandle(typeHandle); Console.WriteLine("属性 = " + type.Attributes); Console.WriteLine("类型引用 = "+ type); } }输出这将产生以下输出:属性 = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit 类型引用 = System.RuntimeType示例让我们看另一个示例: 在线演示using System; public class Demo { public static void ... 阅读更多

Java 中 Lambda 表达式的优点是什么?

raja
更新于 2020年7月10日 11:46:57

3K+ 次浏览

Lambda 表达式是实现函数式接口的内联代码,无需创建具体的或匿名类。Lambda 表达式基本上是一个匿名方法。Lambda 表达式的优点代码更少 - Lambda 表达式最大的好处之一是减少了代码量。我们知道 Lambda 表达式只能与函数式接口一起使用。例如,Runnable 是一个函数式接口,因此我们可以轻松地应用 Lambda 表达式。通过在方法中传递行为来支持顺序和并行执行 - 通过在 Java 8 中使用 Stream API,函数被传递给集合方法。现在 ... 阅读更多

检查字典在 C# 中是否包含指定的键

AmitDiwan
更新于 2019年12月10日 09:39:46

96 次浏览

要检查字典是否包含指定的键,代码如下:示例 在线演示使用 System; using System.Collections.Generic; public class Demo {    public static void Main() {       Dictionary dict =       new Dictionary();       dict.Add("One", "John");       dict.Add("Two", "Tom");       dict.Add("Three", "Jacob");       dict.Add("Four", "Kevin");       dict.Add("Five", "Nathan");       Console.WriteLine("元素数量 = "+dict.Count);       Console.WriteLine("键值对...");       foreach(KeyValuePair res in dict) {          Console.WriteLine("键 = {0}, 值 = {1}", res.Key, res.Value); ... 阅读更多

广告
© . All rights reserved.