草庐IT

int_void

全部标签

c# 对 List<KeyValuePair<int, string>> 进行排序

在C#中,我想对List>进行排序按列表中每个字符串的长度。在Psuedo-Java中,这将是匿名的,看起来像这样:Collections.Sort(someList,newComparator>({publicintcompare(KeyValuePairs1,KeyValuePairs2){return(s1.Value.Length>s2.Value.Length)?1:0;//specifymysortingcriteriahere}});如何获得上述功能? 最佳答案 C#中的等效项是使用lambda表达式和Sort方法:s

c# - 从 double 转换为 int 的最佳(最安全)方法

我很好奇将double转换为整数的最佳方法。运行时安全是我在这里的主要关注点(它不一定是最快的方法,但这是我的次要关注点)。我在下面留下了一些我可以想出的选项。任何人都可以权衡哪种是最佳实践吗?有没有我没有列出的更好的方法来完成这个?doublefoo=1;intbar;//Option1boolparsed=Int32.TryParse(foo.ToString(),outbar);if(parsed){//...}//Option2bar=Convert.ToInt32(foo);//Option3if(fooInt32.MinValue){bar=(Int32)foo;}

c# - 将 int 1、10、100 格式化为字符串 "001"、 "010"、 "100"的掩码

我如何将掩码应用于字符串以按以下方式格式化输出文本(最多2个前导零):inta=1,b=10,c=100;stringaF=LeadingZeroFormat(a),bF=LeadingZeroFormat(b),cF=LeadingZeroFormat(c);Console.Writeline("{0},{1},{2}",aF,bF,cF);//"001,010,100"什么是最优雅的解决方案?提前致谢。 最佳答案 您可以使用Int32.ToString("000")以这种方式格式化整数。有关详细信息,请参阅CustomNumer

c# - 背靠背 for 循环中的 int、short、byte 性能

(背景:WhyshouldIuseintinsteadofabyteorshortinC#)为了满足我自己对使用“适当大小”整数与“优化”整数的优缺点的好奇心,我编写了以下代码,这些代码强化了我之前对.Net中的int性能的看法(并对此进行了解释在上面的链接中)这是针对int性能而不是short或byte进行了优化。DateTimet;longa,b,c;t=DateTime.Now;for(intindex=0;index这在......方面给出了大致一致的结果~950000~2000000~1700000这符合我的预期。然而,当我尝试像这样为每种数据类型重复循环时......t=D

c# - 如何将 List<String> 转换为 Dictionary<int,String>

我有List,我需要将其转换为Dictionary自动生成key,有什么最短的方法可以实现吗?我试过:vardictionary=newDictionary();intindex=0;list.ForEach(x=>{definitions.Add(index,x);index++;});但我认为这是肮脏的方式。 最佳答案 vardict=list.Select((s,i)=>new{s,i}).ToDictionary(x=>x.i,x=>x.s); 关于c#-如何将List转换为Di

c# - 一种解析带有 'Flags' 的 .NET 枚举字符串或 int 值的方法

有一种使用以下方法计算枚举元素的好方法://memberTypeisenumtypeif(Enum.IsDefined(memberType,valueString)){returnEnum.Parse(memberType,valueString);}else{try{varunderlyingValue=Convert.ChangeType(valueString,Enum.GetUnderlyingType(memberType));if(Enum.IsDefined(memberType,underlyingValue)){returnunderlyingValue;}}cat

c# - 是否有用于 Where 和 Select 的 (T, int) 重载的 LINQ 语法?

查询varq=fromelemincollectionwheresomeCondition(elem)selectelem;翻译成varq=collection.Where(elem=>someCondition(elem));是否有可转换为以下内容的LINQ语法?varq=collection.Where((elem,index)=>someCondition(elem,index)); 最佳答案 不,没有LINQ语法。一个简单的解决方法是:varq=fromelemincollection.Select((x,i)=>new{x

c# - 将 null 分配给可为空的 int 时出错 - "The value ' null' 对属性无效”

我的View模型中有这个属性:[DisplayName("Region")]publicint?RegionId{get;set;}我将我的View模型传递给我的Controller,如果RegionId为null,它会在ModelState.IsValid处失败。如果我向它传递一个整数,它就可以正常工作。错误信息是:Thevalue'null'isnotvalidforRegion在检查ModelState.IsValid之前,我也试过调用它,但我得到了同样的错误:if(viewModel.RegionId==null)viewModel.RegionId=(int?)null;这里

c# - 为什么每个 Char static "Is..."都有一个字符串重载,例如IsWhiteSpace(字符串,Int32)?

http://msdn.microsoft.com/en-us/library/1x308yk8.aspx这允许我这样做:varstr="string";Char.IsWhiteSpace(str,6);而不是:Char.IsWhiteSpace(str[6]);似乎不寻常,所以我看了看倒影:[TargetedPatchingOptOut("PerformancecriticaltoinlineacrossNGenimageboundaries")]publicstaticboolIsWhiteSpace(charc){if(char.IsLatin1(c)){returnchar.I

c# - 为 tinyint 列生成的查询将 CAST 引入到 int

我正在查询一个tinyint列,并且EntityFramework生成一个SELECT查询,该查询为该列引入一个CAST到INT,即使我在WHERE子句中使用的值是byte类型也是如此。查看模型,为我的tinyint列生成的类型是byte。查看代码:bytebyteValue=6;varentityList=fromrinrep.DataContext.FooTablewherer.TinyintColumn==byteValueselectr;查看生成的查询:SELECT[Extent1].[TinyintColumn]AS[TinyintColumn]WHERE@p__linq__