请考虑以下事项:
class A {}
class B : A {}
class C
{
C()
{
var b = new B();
Foo(b);
Foo2(ref b); // <= compile-time error:
// "The 'ref' argument doesn't match the parameter type"
}
void Foo(A a) {}
void Foo2(ref A a) {}
}
Run Code Online (Sandbox Code Playgroud)
为什么会发生上述编译时错误?两者ref和out参数都会发生这种情况.
我在Visual Studio 2017 RC中创建了一个项目,以检查我是否可以在.NET Framework 4.5项目中使用新的C#7.0语言功能.在我看来,在引用System.ValueTupleNuGet之后,新的元组工作得很好.还有什么我应该考虑的,或者这会起作用吗?
检查System.ValueTupleNuGet依赖项后,看起来不支持.NET Framework 4.0.是这种情况,还是有一些方法可以让新语言在这个运行时工作?
运行C#7的最低.NET框架和CLR版本要求是什么?另外,我需要VS 2017来编译C#7吗?
该陈述是什么意思?
在C#中引用和输出参数,不能标记为变体.
1)是否意味着不能做以下事情.
public class SomeClass<R, A>: IVariant<R, A>
{
public virtual R DoSomething( ref A args )
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
2)或者它是否意味着我不能拥有以下内容.
public delegate R Reader<out R, in A>(A arg, string s);
public static void AssignReadFromPeonMethodToDelegate(ref Reader<object, Peon> pReader)
{
pReader = ReadFromPeon;
}
static object ReadFromPeon(Peon p, string propertyName)
{
return p.GetType().GetField(propertyName).GetValue(p);
}
static Reader<object, Peon> pReader;
static void Main(string[] args)
{
AssignReadFromPeonMethodToDelegate(ref pReader);
bCanReadWrite = (bool)pReader(peon, "CanReadWrite");
Console.WriteLine("Press any key to quit...");
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
我试过(2)并且它有效.
这可能是一个基本问题,但我无法找到解决方法.我有一个字符串数组,我试图用整数解析它们.正如所料,我得到了格式异常.
我怎么能跳过"3a"并继续解析剩余的数组并使用Linq将整数存储到输出中?这是一种更好的方法还是不做练习?在这种情况下,请介绍一下如何使用TryParse
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] values = { "1", "2", "3a","4" };
List<int> output = new List<int>();
try{
output = values.Select(i => int.Parse(i)).ToList<int>();
}
catch(FormatException)
{
foreach (int i in output)
Console.WriteLine(i);
}
foreach (int i in output)
Console.WriteLine(i);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个文本框,应填充4号的int,类似于[0000,4444,5555,6666].我需要得到逗号的位置,然后将4个数字放在var中.你能帮助我吗?
I have:
IEnumerable<string> c = codigo_incidencia.Split('#');
Run Code Online (Sandbox Code Playgroud)
I need to cast "c" to be an IEnumerable<int>. I don´t know how to do this cast in C#.
Can someone help me?
c# ×7
.net ×3
c#-7.0 ×2
.net-4.0 ×1
.net-4.5 ×1
covariance ×1
linq ×1
polymorphism ×1
requirements ×1
variance ×1
wpf ×1
xaml ×1