我只是写了一些快速代码并注意到这个编译器错误
在lambda表达式中使用迭代变量可能会产生意外结果.
相反,在循环中创建一个局部变量并为其分配迭代变量的值.
我知道这意味着什么,我可以很容易地解决它,而不是什么大不了的事.
但我想知道为什么在lambda中使用迭代变量是个坏主意?
我以后可能会遇到什么问题?
自从我在2005年从VB6迁移到VB.NET以来,我一直在使用CType从一种数据类型转换到另一种数据类型.我这样做是因为它只是更快地输入,以前存在于VB6中我不知道为什么我必须使用DirectCast,如果它们之间显然没有区别.
我偶尔会使用TryCast,因为我知道有时候施法会失败.但是我无法区分CType和DirectCast.
谁能告诉我简单的简单英语有什么区别两者(CType和DirectCast)有什么区别?添加使用内容的示例也会有所帮助.
我在大约2年前在.NET 2.0中编写了一个用于数据库访问的类库,并且一直在.NET 2.0,3.0和3.5上使用它.
在我正在进行的当前项目(这是一个.NET 4.0应用程序)中,尝试使用旧的忠实类库,我得到以下异常:
System.InvalidOperationException was unhandled Message=An error occurred creating the form. See Exception.InnerException for details. The error is: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information. Source=SchoolManager StackTrace: at SchoolManager.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190 at SchoolManager.My.MyProject.MyForms.get_frmGeneric() at SchoolManager.My.MyApplication.OnCreateMainForm() in D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\My Project\Application.Designer.vb:line 35 at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at SchoolManager.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] …
在我工作的许多项目中,每当我必须返回一个只读集合时,我使用该IEnumerable<T>
接口并使其类型特定如下:
Public ReadOnly Property GetValues() As IEnumerable(Of Integer)
Get
'code to return the values'
End Get
End Property
Run Code Online (Sandbox Code Playgroud)
大多数时候,我返回一个List但是在一些函数和只读属性中我返回一个数组,这个数组也可以通过扩展方法的礼貌来达到目的.
我的问题是我会通过返回违反任何设计原则IEnumerable<T>
s,而不是特定类型(例如:List<T>
,HashSet<T>
,Stack<T>
或Array
S) ?
是否可以将泛型类型参数[我不知道这是否是正确的名称]限制为Enum
?
例如,我该怎么做这样的事情?
//VB.NET
Function GetValues(Of T As System.Enum)(ByVal value As T) As IEnumerable(Of T)
Return [Enum].GetValues(value.GetType)
End Function
//C#
public IEnumerable<T> GetValues<T>(T value) where T : System.Enum
{
return Enum.GetValues(value.GetType());
}
Run Code Online (Sandbox Code Playgroud)
更新
为了这个目的,我最终使用了Jon Skeet的Unconstrained Melody.感谢大家的贡献.
我刚开始使用C#,我有几个问题.有没有办法像下面这样编写VB.NET Select语句的C#等价物?
Select Object.Name.ToString()
Case "Name1"
'Do something
Case "Name2"
'Do something else
Case Else
'Do the default action
End Select
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
感谢到目前为止的输入,如果我将几个控件挂钩到一个事件处理程序,如下所示,我想对每个控件执行稍微不同的操作:
Private Sub Button_Click(sender as Object, e as EventArgs) _
Handles button1.Click, Button2.Click
'do a general activity
Select CType(sender, Button).Name
Case button1.Name
'do something
Case button2.Name
'do something else
Case Else
'do the defalut action
End Select
End Sub
Run Code Online (Sandbox Code Playgroud)
有没有办法在C#中执行上面的select语句而不必使用嵌套的ifs?
我希望我的TabControl上的标签显示在左侧,或者有时右侧.
但是,与System.Windows.Forms.TabControl不同,我希望文本保持水平而不是向水平方向旋转90度或270度.
虽然我可以在大约一两个小时内自己编写代码来执行此操作,但我想我首先会询问是否存在实现此类功能的任何Winforms控件.
注意:任何现有的解决方案最好是非商业性的.
谢谢.
关于GAC
我有一个快速的问题我创建了一个程序集Awesome.DLL.如果它已签名,则安装到GAC中:
C:\MyApps\Awesome\Awesome\Awesome\bin\Release>sn -k Awesome.snk
Microsoft (R) .NET Framework Strong Name Utility Version 4.0.30319.1
Copyright (c) Microsoft Corporation. All rights reserved.
Key pair written to Awesome.snk
C:\MyApps\Awesome\Awesome\Awesome\bin\Release>gacutil /i Awesome.dll
Microsoft (R) .NET Global Assembly Cache Utility. Version 4.0.30319.1
Copyright (c) Microsoft Corporation. All rights reserved.
Assembly successfully added to the cache
C:\MyApps\Awesome\Awesome\Awesome\bin\Release>gacutil /l Awesome.dll
Microsoft (R) .NET Global Assembly Cache Utility. Version 4.0.30319.1
Copyright (c) Microsoft Corporation. All rights reserved.
The Global Assembly Cache contains the following assemblies:
Number of …
Run Code Online (Sandbox Code Playgroud) c# ×6
vb.net ×6
.net ×5
.net-4.0 ×2
tabcontrol ×2
winforms ×2
casting ×1
ctype ×1
directcast ×1
enums ×1
gac ×1
gacutil ×1
generics ×1
iteration ×1
lambda ×1
mixed-mode ×1
vb.net-to-c# ×1
warnings ×1