Tag: 变量赋值

如何将另一个结构隐式转换为我的Type?

因为它是MyClass x = 120; ,是否可以创建这样的自定义类? 如果是这样,我该怎么办?

如何在c#中赋值后保留有关对象的信息?

我一直在问我认为可能是什么解决方案的问题 ,但有人指出我陷入了XY问题,我应该问一下我的确切问题。 我有一个结构,我希望其他人能够在他们自己的程序中使用。 它需要可以从其他现有类型隐式转换为此类型,但同时在分配后需要保留一些信息。 这是一个简单的问题示例: using System; public struct MyStruct { public string SomethingImportant; public MyStruct(string s) { SomethingImportant = s; } //this function needs to have no knowledge of how/where the struct is being used public bool SomeFunction(string s) { return s == SomethingImportant; } public static implicit operator MyStruct(double x) { return new […]

foreach,表现明智。 我们应该在循环之前或之内声明变量吗?

对于性能明智而言,更好地在foreach语句之外声明变量,并且每次将其重新分配到foreach(foreach)或在foreach中创建新变量,例如 private List GetItems() { var items = new List(); var collection = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ListItem item; foreach (var i in collection) { item = new ListItem { Text = i.ToString() }; items.Add(item); } return items; } 还是这个? private List GetItems() { var […]

VB中的多分配,如C风格的语言

有没有办法在VB.NET中执行此操作,如在C风格的语言中: struct Thickness { double _Left; double _Right; double _Top; double _Bottom; public Thickness(double uniformLength) { this._Left = this._Right = this._Top = this._Bottom = uniformLength; } }

为什么赋值运算符(=)在foreach循环中无效?

为什么赋值运算符(=)在foreach循环中无效? 我正在使用C#,但我认为该参数对于支持foreach其他语言(例如PHP)是相同的。 例如,如果我这样做: string[] sArray = new string[5]; foreach (string item in sArray) { item = “Some assignment.\r\n”; } 我收到一个错误,“无法分配给’item’,因为它是’foreach迭代变量’。”

如何在foreach循环中修改foreach迭代变量?

当我尝试这样做时…… Item[,] array = new Item[w, h]; // Two dimensional array of class Item, // w, h are unknown at compile time. foreach(var item in array) { item = new Item(); } …我得到的Cannot assign to ‘item’ because it is a ‘foreach iteration variable’ 。 不过,我还想那样做。 我们的想法是将默认的Item类值分配给现有项目。

将IronPython方法分配给C#委托

我有一个C#类看起来有点像: public class MyClass { private Func processMethod = (ds) => { //default method for the class } public Func ProcessMethod { get{ return processMethod; } set{ processMethod = value; } } /* Other details elided */ } 我有一个IronPython脚本,可以在应用程序中运行 from MyApp import myObj #instance of MyClass def OtherMethod(ds): if ds.Data.Length > 0 : quot = […]

使用空赋值修复“使用未分配的局部变量”。 为什么?

使用这样的一段代码,编译器在c.MyProperty上c.MyProperty : MyClass c; try { throw new Exception(); } catch (Exception) { } c.MyProperty = 2; // “Use of unassigned local variable ‘c'”. 但是,如果在初始化中为c分配null ,则不会抱怨: MyClass c = null; try { throw new Exception(); } catch (Exception) { } c.MyProperty = 2; // no complains this time. 那么,为什么这有效呢? 如果c没有赋值为null并且编译器假设允许它,那么不会在c.MyProperty抛出相同的exception, 对象引用没有设置为对象的实例吗?

c#多任务

int a, b, n; … (a, b) = (2, 3); // ‘a’ is now 2 and ‘b’ is now 3 这种事情在C#中真的很有帮助。 在这个例子中,’a’和’b’没有被封装在一起,例如位置的X和Y可能是。 这是否以某种forms存在? 下面是一个不太重要的例子。 (a, b) = n == 4 ? (2, 3) : (3, n % 2 == 0 ? 1 : 2); Adam Maras在评论中表示: var result = n == 4 ? Tuple.Create(2, 3) […]

为此关键字指定struct值

我最近正在研究CancellationToken结构的内部结构,并发现了一些奇怪的结构(更确切地说,为this关键字赋值)。 其构造函数之一的代码如下: public CancellationToken( bool canceled ) { this = new CancellationToken(); if ( canceled ) { this.m_source = CancellationTokenSource.InternalGetStaticSource( canceled ); } } this关键字进行赋值的行的含义是什么? 请注意,类不能this关键字赋值 – 错误Cannot assign to ” because it is read-only 。