我为什么要使用代码合同
Contract.Requires<ArgumentNullException>( x != null, "x" );
Run Code Online (Sandbox Code Playgroud)
而不是好老
if (x!=null){}
else throw...
Run Code Online (Sandbox Code Playgroud)
除了简洁之外还有其他任何好处吗?
C#中的类怎么可能没有定义构造函数?比如我有一个班级
internal class TextStyle
{
internal string text = "";
internal Font font = new Font("Arial", 8);
internal Color color = Color.Black;
}
Run Code Online (Sandbox Code Playgroud)
在代码中,这个类被实例化为
TextStyle textParameters = new TextStyle();
Run Code Online (Sandbox Code Playgroud) 我试图替换代码
foreach (var discovery in mpwrapper.parser.Discoveries)
{
solution.AddFile("Discoveries", discovery.DisplayStringName + ".mpx", discovery);
}
Run Code Online (Sandbox Code Playgroud)
使用以下linq表达式
mpwrapper.parser.Discoveries.Select(
s => solution.AddFile("Discoveries", s.DisplayStringName + ".mpx", s));
Run Code Online (Sandbox Code Playgroud)
但是得到了一个错误
无法从用法推断出方法'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable,System.Func)'的类型参数.尝试显式指定类型参数.
如何将这个foreach循环转换为linq查询,我在IEnumerable集合中的每个对象上执行一个方法?
我知道压缩版本reflog存储在 .git/packed-refs
但实际的reflog位于何处以及git如何在一个类型中恢复历史记录git reflog?
有人可以解释的意义操纵模式,例如TranslateX,TranslateRailsX,TranslateInertia?什么是rail mode?他们在说什么惯性?
我有一个名为的NSString变量myText.如何[myText length]在xcode调试器中观看?
我正在使用linq查询集合,并datetime使用以下代码按属性对数据进行分组:
var querty = from post in ds.Appointments
group post by new
{
Year = post.DateOfVisit.Year,
Month = post.DateOfVisit.Month
};
Run Code Online (Sandbox Code Playgroud)
当使用匿名类型时,一切都很好.但是,如果我定义自己的类
class YearMonth
{
public int Year;
public string Month;
public YearMonth(int year, int month)
{
Year = year;
Month = month;
}
public override string ToString()
{
return string.Format("{0}-{1}",Year,Month);
}
}
Run Code Online (Sandbox Code Playgroud)
并相应地修改我的查询
var querty = from post in ds.Appointments
group post by new YearMonth(post.DateOfVisit.Year,
post.DateOfVisit.Month);
Run Code Online (Sandbox Code Playgroud)
然后分组不起作用,我得到一个简单的对象列表.为什么?
例如,我有一个方法
SomeMethod(Graphics g)
{
...
}
Run Code Online (Sandbox Code Playgroud)
如果我以某种方式调用此方法
SomeMethod(new Graphics())
Run Code Online (Sandbox Code Playgroud)
我的图形对象是自动曝光还是应该g.Dispose() 在方法结束时手动调用?
SomeMethod(Graphics g)
{
...
g.Dispose();
}
Run Code Online (Sandbox Code Playgroud) 我有两个IEnumerable变量,两者都可以为null.我需要将它们合并到一个列表中.这是直接的方法.
var ienumerable1 = GetEnumerable1();
var ienumerable2 = GetEnumerable2();
if(ienumerable1 != null){
if(ienumerable2 != null){
return ienumerable1.Union(ienumerable2);
}
return ienumerable1;
}
else{
return ienumerable2;
}
Run Code Online (Sandbox Code Playgroud)
在更少的代码行中是否有更优雅的方式来做到这一点?
Roslyn是否有可能弄清楚一个类方法是否实现了一些用属性标记的接口方法?
特别是在wcf中,我们使用接口来描述服务合同.它的每种方法都应该OperationContractAttribute在下面的样本中标记
[ServiceContract]
public interface ISimleService
{
[OperationContract]
string GetData(int value);
}
public class SimleService : ISimleService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
Run Code Online (Sandbox Code Playgroud)
在Roslyn ISymbol接口中提供了GetAttributes()方法,但是在SimleService.GetData()它返回0 的方法上调用.在ISimleService.GetData()声明上调用它OperationContractAttribute按预期返回.
所以一般来说我必须检查类层次结构以找到所有实现的接口,然后遍历层次结构以找到合适的方法.这是一个很难的方法,我想应该有一个更简单的方法.
c# ×7
.net ×3
linq ×2
class ×1
constructor ×1
debugging ×1
dispose ×1
git ×1
git-reflog ×1
objective-c ×1
optimization ×1
roslyn ×1
uwp ×1
uwp-xaml ×1
wcf ×1
windows-8 ×1
winrt-xaml ×1
xcode ×1