Tag: 数组

无法将’System.Object ‘类型的对象强制转换为’MyObject ‘,是什么给出的?

场景: 我正在编写一个层来将3个类似的Web服务抽象为一个可用的类。 每个Web服务都公开一组共享共性的对象。 我创建了一组利用共性的中间对象。 但是在我的图层中,我需要在Web服务对象和我的对象之间进行转换。 在调用Web服务之前,我已经使用reflection在运行时创建了相应的类型,如下所示: public static object[] CreateProperties(Type type, IProperty[] properties) { //Empty so return null if (properties==null || properties.Length == 0) return null; //Check the type is allowed CheckPropertyTypes(“CreateProperties(Type,IProperty[])”,type); //Convert the array of intermediary IProperty objects into // the passed service type eg Service1.Property object[] result = new object[properties.Length]; for (int i […]

sbyte 可以神奇地转换为byte

我不确定这是否是一个.NET错误,但我发现它真的很有趣。 正如所料,我不能这样做: sbyte[] sbytes = { 1, 2, 3 }; byte[] bytes = sbytes; // fails: cannot convert source type ‘sbyte[]’ to taget type ‘byte[]’ 但是,如果sbytes的类型是object ,则可以: object obj = new sbyte[]{ 1, 2, 3 }; byte[] bytes = obj as byte[]; Assert.IsNull(bytes, “WTF??”) 备注1 : int[] – uint[]和其他原始类型也会出现同样的问题。 备注2 :虽然代码将数组作为byte[] ,但调试器失去焦点并显示? -s在数组中。 备注3 :这仅适用于数组,不适用于基础类型本身: […]

创建一个常量但本地的数组

有时我需要一个单一方法的硬编码查找表。 我也可以创建这样的数组 本地方法本身 类中的静态 第一种情况的示例: public int Convert(int i) { int[] lookup = new[] {1, 2, 4, 8, 16, 32, 666, /*…*/ }; return lookup[i]; } 据我所知,每次执行此方法时,.net引擎都会创建一个新的查找数组。 这是正确的,还是JITer足够聪明,可以在调用之间缓存和重用数组? 我认为答案是否定的,所以如果我想确保在调用之间缓存数组,一种方法是使其static : 第二种情况的示例: private static readonly int[] lookup = new[] { 1, 2, 4, 8, 16, 32, 666, /*…*/ }; public int Convert(int i) { return lookup[i]; […]

C#:string 分隔字符串。 有一个单行class吗?

我更喜欢的是: string[] strArray = {“Hi”, “how”, “are”, “you”}; string strNew = strArray.Delimit(chDelimiter); 但是,没有这样的function。 我查看了MSDN,没有任何东西看起来像是一个执行相同操作的函数。 我看着StringBuilder,再一次,没有什么能让我感到高兴。 有没有人知道一个不是非常复杂的单线程,使数组成为一个分隔的字符串。 谢谢你们的帮助。 更新:哇,哈哈,我的坏。 我不停地看着arrays上的.Join,这让我感到害怕。 我甚至没看过String.Join。 多谢你们。 一旦它允许我接受我将。 提供帮助。

为什么((IList )数组).ReadOnly = True但是((IList)数组).ReadOnly = False?

我知道在.NET中,所有数组都派生自System.Array,System.Array类实现IList , ICollection和IEnumerable 。 实际数组类型还实现IList , ICollection和IEnumerable 。 这意味着如果你有一个String[] ,那么String[]对象也是一个System.Collections.IList和一个System.Collections.Generic.IList ;. 不难看出为什么那些IList会被认为是“ReadOnly”,但令人惊讶的是…… String[] array = new String[0]; Console.WriteLine(((IList)array).IsReadOnly); // True Console.WriteLine(((IList)array).IsReadOnly); // False! 在这两种情况下,尝试通过Remove()和RemoveAt()方法删除项会导致NotSupportedException。 这表明两个表达式都对应于ReadOnly列表,但IList的ReadOnly属性不会返回预期值。 怎么会?

如何删除数组中的第一个元素?

我有一个数组: arr[0]=”a” arr[1]=”b” arr[2]=”a” 我想只删除 arr[0] ,并保持 arr[1]和arr[2] 。 我用的是: arr= arr.Where(w => w != arr[0]).ToArray(); 由于arr[0]和arr[2]具有相同的值(“a”),因此我得到的结果只是arr[1] 。 如何返回arr[1]和arr[2] ,只删除arr[0] ?

String数组类型方法返回类型错误

public String[][] GetAllItems() { FoodCityData.ShoppingBuddyEntities fdContext = new FoodCityData.ShoppingBuddyEntities(); IQueryable Query = from c in fdContext.Item select c; List AllfNames = Query.ToList(); int arrayZise = AllfNames.Count; String[,] xx = new String[arrayZise,2]; int i = 0; int j = 0; foreach(Item x in AllfNames) { xx[i,0] = x.ItemName.ToString(); xx[i, 1] = x.ItemPrice.ToString(); i++; } return xx[2,2]; […]

使用Zip()合并不同长度的数组而不会丢失值

在下面的代码中,我正在合并两个类型为int和string数组。 第一个的长度大于第二个长度,因此,最后一个索引(即5 )不会合并: int[] numbers = new[] { 1, 2, 3, 4, 5 }; string[] words = new string[] { “one”, “two”, “three”, “four” }; var numbersAndWords = numbers.Zip(words, (n, w) => new { Number = n, Word = w }); foreach (var nw in numbersAndWords) { Console.WriteLine(nw.Number + nw.Word); } 我想知道一种让它合并的方法。 例如,在words中存在的最后一个字符串之后创建一个空字符串或空字符串,并使用它与最后一个numbers索引合并。 无法弄清楚。 编辑:我得到的结果 […]

数组的GetUpperBound()和GetLowerBound()函数

任何人都可以告诉这两个function有什么作用? 它们采用一个整数参数,它被称为维度。 但是这个整数的值如何改变输出? 下面是我跑的一个例子。 int[, ,] intMyArr = {{{ 7, 1, 3, 4 }, { 2, 9, 6, 5 } }, { { 7, 1, 3, 4 }, { 2, 9, 6, 5 }}}; Console.WriteLine(intMyArr.GetUpperBound(0)); // Output is 1 Console.WriteLine(intMyArr.GetUpperBound(1)); // Output is 1 Console.WriteLine(intMyArr.GetUpperBound(2)); // Output is 3 Console.WriteLine(intMyArr.GetLowerBound(0)); // Output is 0 Console.WriteLine(intMyArr.GetLowerBound(1)); […]

结构类型数组的性能

例: // Potentially large struct. struct Foo { public int A; public int B; // etc. } Foo[] arr = new Foo[100]; 如果Foo是100字节结构,则在执行以下语句期间将在内存中复制多少字节: int x = arr[0].A 也就是说,将arr [0]计算为某个临时变量(Foo实例的100字节副本),然后将.A复制到变量x(4字节副本)。 或者是编译器,JITer和CLR的某种组合能够优化此语句,以便将A的4个字节直接复制到x 。 如果执行优化,当项目保存在List或者数组作为IList或ArraySegment传递时,它是否仍然成立?