找到关于编程的34423篇文章

如何在C#中将字典初始化为空字典?

Samual Sam
更新于2020年6月22日 11:59:00

12K+ 浏览量

要将字典初始化为空字典,请使用Clear()方法。它会清除字典并将其设为空。dict.Clear();之后,使用Dictionary的Count属性检查列表是否为空 - if (dict.Count == 0) {    Console.WriteLine("字典为空!"); }让我们看看完整的代码 - 示例 在线演示using System; using System.Collections.Generic; using System.Linq; namespace Demo {    public class Program {       public static void Main(string[] args) {          var dict = new Dictionary < string, string > ();          dict.Clear();          if (dict.Count == 0) {             Console.WriteLine("字典为空!");          }       }    } }输出字典为空!

C#中的集合

mkotla
更新于2020年6月22日 11:57:09

7K+ 浏览量

C#中的集合是HashSet。C#中的HashSet可以消除数组中重复的字符串或元素。在C#中,它是一个优化的集合。声明HashSet - var h = new HashSet(arr1);上面,我们已经将已声明的数组arr1设置在HashSet中。现在将其设置在数组上以删除重复的单词 - string[] arr2 = h.ToArray();让我们看一个使用C# HashSet删除重复字符串的示例。这里,我们有重复的元素 - 示例using System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       string[] arr1 = {"Table", "Chair", "Pen", "Clip", "Table"};     ... 阅读更多

C#中的主线程与子线程

varma
更新于2020年6月22日 12:00:04

686 浏览量

主线程在进程中首先执行的线程称为主线程。当C#程序开始执行时,会自动创建主线程。子线程使用Thread类创建的线程称为主线程的子线程。以下是一个显示如何创建主线程和子线程的示例 - 示例 在线演示using System; using System.Threading; namespace Demo {    class Program {       static void Main(string[] args) {          Thread th = Thread.CurrentThread;          th.Name = "MainThread";          Console.WriteLine("这是 {0}", th.Name);          Console.ReadKey();       }    } }输出这是MainThread

C#中Main()的有效变体

varun
更新于2020年6月22日 12:01:04

578 浏览量

Main方法是所有C#程序的入口点。它说明了类在执行时所执行的操作。Main()的有效变体是 - static void Main(string[] args这里,static - 不需要对象来访问静态成员void - 方法的返回类型Main - 任何C#程序的入口点。程序执行从这里开始。string[] args - 用于C#中的命令行参数。示例这是一个示例 - using System; namespace Program {    public class Demo {       public static void Main(string[] args) {          Console.WriteLine("欢迎!");          Console.ReadKey();       }    }   }

如何在C#中将字符串转换为整数?

George John
更新于2020年6月22日 11:00:34

295 浏览量

假设我们的字符串是 - string str ="9999";现在,使用Int32.Parse()将字符串转换为整数 - int n = Int32.Parse(str);现在显示以下代码中所示的整数值 - 示例using System; class Demo {    static void Main() {       string str ="9999";       int n = Int32.Parse(str);       Console.WriteLine(n);    } }

如何在C#中将字符数组转换为字符串?

Arjun Thakur
更新于2020年6月22日 11:01:48

147 浏览量

让我们首先设置一个包含5个字符的数组 - char[] ch = new char[15]; ch[0] = 'T'; ch[1] = 'r'; ch[2] = 'i'; ch[3] = 'c'; ch[4] = 'k';现在将其转换为字符串 - string str = new string(ch);这是完整的代码 - 示例Using System; class Program {    static void Main() {       char[] ch = new char[15];       ch[0] = 'T';       ch[1] = 'r';       ch[2] = 'i';       ch[3] = 'c';       ch[4] = 'k';       // 转换为字符串       string str = new string(ch);       Console.WriteLine(str);    } }

如何在C#中克隆泛型列表?

Chandu yadav
更新于2020年6月22日 11:02:47

409 浏览量

列表是一个泛型集合,用于保存相同数据类型的元素。要克隆列表,可以使用CopyTo方法。声明一个列表并添加元素 - List < string > myList = new List < string > (); myList.Add("Programming"); myList.Add("Web Dev"); myList.Add("Database");现在创建一个新数组并将列表克隆到其中 - string[] arr = new string[10]; myList.CopyTo(arr);这是完整的代码 - 示例 在线演示using System; using System.Collections.Generic; public class Demo {    public static void Main() {       List < string > myList = new List < string > ();       myList.Add("Programming");       ... 阅读更多

C#中的信号量

Arjun Thakur
更新于2020年4月1日 08:47:56

3K+ 浏览量

信号量类允许您设置对临界区有访问权限的线程数的限制。该类用于控制对资源池的访问。System.Threading.Semaphore是Semaphore的命名空间,因为它包含实现Semaphore所需的所有方法和属性。要在C#中使用信号量,您只需要实例化Semaphore对象的一个实例即可。它至少有两个参数 - 参考-MSDN序号构造函数和说明1Semaphore(Int32, Int32)初始化Semaphore类的新实例,指定初始条目数和最大并发条目数。2Semaphore(Int32, Int32, String) - 初始化... 阅读更多

C#中ArrayList的同步

Ankith Reddy
更新于2020年6月22日 10:52:04

236 浏览量

在C#中,使用ArrayList.Synchronized方法来同步C#中的ArrayList。让我们看一个使用C#中的SyncRoot属性来锁定集合的示例 - ArrayList arr = new ArrayList(); lock(arr.SyncRoot) {    foreach (object ele in arr) {    } }以下是检查ArrayList同步状态的完整示例 - 示例 在线演示using System; using System.Collections; public class Demo {    public static void Main() {       ArrayList arr1 = new ArrayList();       arr1.Add("One");       arr1.Add("Two");       arr1.Add("Three");       arr1.Add("Four");       arr1.Add("Five");     ... 阅读更多

C#中的死锁和饥饿

George John
更新于2020年6月22日 10:52:20

1K+ 浏览量

当一个资源被一个线程锁定并且同时被另一个线程需要时,就会发生死锁。这个问题在多处理系统中经常发生。当两个或多个线程等待属于另一个线程的资源时,可能会发生这种情况。这是一个例子 - 线程一线程二获取锁P获取锁Q请求锁Q请求锁P线程一将无法获得锁Q,因为它属于线程二。同样,线程二将无法获得锁P,因为它最初的拥有者是线程一。死锁也可能是三方死锁,如果三个线程和... 阅读更多

广告
© . All rights reserved.