Tag: 数组

在C#中将2个字节转换为Short

我正在尝试将两个字节转换为无符号短路,以便我可以检索实际的服务器端口值。 我基于回复格式从协议规范开始 。 我尝试使用BitConverter.ToUint16() ,但问题是,它似乎没有抛出预期的值。 请参阅下面的示例实现: int bytesRead = 0; while (bytesRead < ms.Length) { int first = ms.ReadByte() & 0xFF; int second = ms.ReadByte() & 0xFF; int third = ms.ReadByte() & 0xFF; int fourth = ms.ReadByte() & 0xFF; int port1 = ms.ReadByte(); int port2 = ms.ReadByte(); int actualPort = BitConverter.ToUInt16(new byte[2] {(byte)port1 , (byte)port2 […]

在C#中按字母顺序排序数组

希望有人能提供帮助。 我创建了一个可变长度数组,它将接受多个名称输入。 我现在想按字母顺序对数组进行排序,并将其返回到控制台屏幕。 我以为是Array.Sort(名字); 会为我做这件事,但我得到一个exception抛出。 我一直在看笔记,例子和在线,但似乎没有什么能与我正在做的事情相匹配。 到目前为止我已经完成了以下工作。 我差点把头发撕成碎片! PS我一直试图弄清楚这几个小时,我已经30多岁了,试图学习自己,所以请不要只说“做你的功课”我试图解决这个问题而不能这样我需要有人来解释哪里出错了 这是一个星期天,我正在努力做额外的工作,没有任何笔记来完全覆盖这一点 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Student_Array { class Program { struct Student { public string Name; } static void Main(string[] args) { int numberOfStudents; Student[] names; string input; Console.WriteLine(“How many students are there?”); input = Console.ReadLine(); numberOfStudents = int.Parse(input); names = […]

避免随机重复

System.Random generator = new Random(DateTime.Now.Millisecond); int[] lotteryNumber = new int[7]; Console.WriteLine(“Your lottery numbers: “); for (int i = 0; i<7; i++) { lotteryNumber[i] = generator.Next(1, 37); Console.Write("{0} ",lotteryNumber[i]); } Console.ReadLine(); 我需要制作一个打印7个彩票号码的程序,但不需要重复。 上面的代码打印了(1-37)范围内的7个随机数,但重复了appaer。 我需要一种方法来防止出现重复的数字。

如何通过LINQ搜索2D数组?

我有一个像这样的2D数组: string[,] ClassNames = { {“A”,”Red”}, {“B”,”Blue”}, {“C”,”Pink”}, {“D”,”Green”}, {“X”,”Black”}, }; 我通过for语句在第 1列中搜索ClassName ,并在第2列中返回ColorName ,如下所示: string className = “A”; string color = “Black”; for (int i = 0; i <= ClassNames.GetUpperBound(0); i++) { if (ClassNames[i, 0] == className) { color = ClassNames[i, 1]; Response.Write(color); break; } } 我想使用LINQ而不是for语句来获取className的颜色 。 如何将上面的语句转换为LINQ。

arrays边界检查CLR中的消除?

我最近阅读了Dave Detlefs的这篇文章 ,其中他介绍了一些CLR执行数组边界检查消除的情况。 我决定自己测试一下,所以我做了以下几点: 打开Visual Studio 2010 Ultimate SP1 创建了一个类型为Console Application的新C#项目(默认情况下以.NET 4 Client Profile为目标) 添加了以下代码(所有子方法都直接来自文章): class Program { static void Main(string[] args) { int[] array = new int[30]; Test_SimpleAscend(array); Test_SimpleRedundant(array, 3); foreach (int i in array) { Console.WriteLine(i); } } static void Test_SimpleAscend(int[] a) { for (int i = 0; i < a.Length; i++) a[i] […]

如何在C#中初始化数组?

如何在C#中初始化数组?

C#拆分数组

我需要在中点将一个不确定大小的数组拆分成两个独立的数组。 使用ToArray()从字符串列表生成数组。 public void AddToList () { bool loop = true; string a = “”; Console.WriteLine(“Enter a string value and press enter to add it to the list”); while (loop == true) { a = Console.ReadLine(); if (a != “”) { mylist.Add(a); } else { loop = false; } } } public void ReturnList() { […]

C# – 字符串实际上是一个字符数组还是只有一个索引器?

由于在C#中可以使用以下代码,因此我认为string是否实际上是一个chars数组: string a=”TEST”; char C=a[0]; // will be T

如何将数组分成一组n个元素?

在c#4中将数组分组为n个元素数组的列表的最佳方法是什么? 例如 string[] testArray = { “s1”, “s2”, “s3”, “s4”, “s5”, “s6”, “s7”, “s8” }; 如果我们采取n = 3,应该分成。 string[] A1 = {“s1”, “s2”, “s3”}; string[] A2 = {“s4”, “s5”, “s6”}; string[] A3 = {“s7”, “s8”}; 使用LINQ可能是一种简单的方法吗?

ArraySegment – 返回实际的段C#

我一直在寻找返回段的方法,该段基本上由ArraySegment在偏移和计数方面保持。 尽管ArraySegment包含完整的原始数组,但它只是将其分隔,因为对该段的任何更改都会反映到原始数组中。 问题或说ArraySegment的限制是它不会返回整个段本身,我必须遍历值。 什么是返回整个细分市场的最佳方式? byte[] input = new byte[5]{1,2,3,4,5}; ArraySegment delimited = new ArraySegment(input,0,2); byte[] segment = HERE I NEED SOMETHING THAT WILL RETURN THE SEGMENT ie [0,1,2] 最重要的一点,段不能是副本,而应该引用原始数组。 如果对段进行了任何更改,则必须将它们反映在原始数组中。 任何提示都非常感谢,谢谢! 作业基准 :在Thomas和digEmAll的一些答案之后 好吧,我针对digEmAll和Thomas的代码运行了一些基准测试,令我惊讶的是代码速度非常快。 正是我拼命寻找的东西。 结果如下。 Construct Size Elements assigned Iterations Time _______________________________________________________________________________ ArraySegmentWrapper 1500 1500 1000000 396.3 ms Array.Copy 1500 1500 1000000 4389.04 ms […]