我们有一个以下示例:
public class X { }
public class Y { }
public class Z { }
public delegate IDictionary<Y, IList<Z>> Bar(IList<X> x, int i);
public interface IFoo
{
// ...
Bar Bar { get; }
}
public class Foo : IFoo
{
// ...
public Bar Bar
{
get
{
return null; //...
}
}
}
void Main()
{
IFoo foo; //= ...
IEnumerable<IList<X>> source; //= ...
var results = source.Select(foo.Bar); // <- compile error here
}
Run Code Online (Sandbox Code Playgroud)
编译器说:
无法从用法推断出方法'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable,System.Func)'的类型参数.尝试显式指定类型参数.
这是因为它无法转换 …
这是我的代码:
DateTime date1 = new DateTime(byear, bmonth, bday, 0, 0, 0);
DateTime datenow = DateTime.Now;
DateTime date2 = datenow - date1
Run Code Online (Sandbox Code Playgroud)
在最后一行,我收到此错误:
错误1无法将类型'System.TimeSpan'隐式转换为'System.DateTime'
如何减去两个日期?
我能看到的唯一优势:
var s = new ClassA();
Run Code Online (Sandbox Code Playgroud)
过度
ClassA s = new ClassA();
Run Code Online (Sandbox Code Playgroud)
如果你决定要ClassB,那么你只需要更改声明的RHS即可.
我想如果你通过集合枚举,你也可以只是'var',然后再计算出类型.
是吗?还有一些其他巨大的好处,我的虚弱的头脑没有看到?
在我的页面加载中,我打了ReturnStuff()一次或三次?如果我三次打电话,有没有更有效的方法呢?
protected void Page_Load(object sender, EventArgs e)
{
string thing1 = ReturnStuff(username,password)[0];
string thing2 = ReturnStuff(username, password)[1];
string thing3 = ReturnStuff(username, password)[2];
}
public static List<string> ReturnStuff(string foo, string bar)
{
// Create a list to contain the attributes
List<string> Stuff = new List<string>();
// Some process that determines strings values based on supplied parameters
Stuff.Add(fn);
Stuff.Add(ln);
Stuff.Add(em);
return Stuff;
}
Run Code Online (Sandbox Code Playgroud) 在VS或ReSharper中是否有提取局部变量的快捷方式?我知道有提取字段变量的快捷方式,但我找不到提取局部变量的快捷方式。提前致谢。
我刚开始使用Resharper.它的一个特点是,它建议基于我认为良好的编码实践来改变代码.
它建议的一个变化是在赋值期间将变量类型更改为var.我一直在改变,现在代码到处都是var.不知何故,我感觉"var"关键字使代码有点难以理解.
在可能的情况下使用"var"是否是一个很好的编码实践,或者更好地坚持实际类型.(除了需要使用"var"的匿名类型)
谢谢.
c#中var类型的一个用途似乎是快捷方式/简化/保存不必要的输入.我考虑过的一件事是:
MyApp.Properties.Settings.Default.Value = 1;
这是一堆不必要的代码.另一种方法是声明:
using MyApp.Properties;
Run Code Online (Sandbox Code Playgroud)
- 要么 -
using appsettings = MyAppp.Properties.Settings;
Run Code Online (Sandbox Code Playgroud)
导致:appsettings.Default.Value = 1或Settings.Default.Value = 1
升技更好,但我还是希望它更短:
var appsettings = MyFirstCSharpApp.Properties.Settings.Default;
appsettings.Value=1;
Run Code Online (Sandbox Code Playgroud)
最后,它足够短,它也可以用于其他恼人的长时间通话,但这是一种可接受的方式吗?我正在考虑"快捷变量"是否总是指向我正在制作快捷方式的现有实例?(显然不仅仅是这个例子中的设置)
我想知道你var什么时候应该使用?
C#中的几乎任何东西,除了可能是原语和一些更奇怪的情况,都来自Object.
那么使用那些实际类型不是更好的做法吗?或者至少object?
(我已经编程了一段时间,我的严格观点是那var是邪恶的)
我有一个复杂的吸气剂如下
public bool IsOk
{
get
{
return (IsFirstCondition && (IsSecondCondition.Items.First.Item == MyItems.PublicItems.BestItem
|| IsThirdCondition.Collection.EditedItem.IsTheMostUsedItem);
}
}
Run Code Online (Sandbox Code Playgroud)
为了简单和更好的可读性,我想把我的getter变成这样的东西:
public bool IsOk
{
get
{
var isBestItemm = IsSecondCondition.Items.First.Item == MyItems.PublicItems.BestItem;
var isMostUsedItem = IsThirdCondition.Collection.EditedItem.IsTheMostUsedItem;
return (IsFirstCondition && (isBestItemm || isMostUsedItem);
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,getter只是为了返回数据而不是为了设置/声明/初始化东西......我的简化getter是否对最佳实践和编码指南有效?