草庐IT

string.format

全部标签

c# - 无法将类型 'X' 隐式转换为 'string' - 它何时以及如何决定它 "cannot"?

现在我正在使用Guids。我当然记得在整个代码中,这种隐式转换在某些地方有效,而在其他地方则无效。直到现在我都看不到模式。编译器如何决定何时不能?我的意思是,类型方法Guid.ToString()存在,是否在需要此转换时调用它?谁能告诉我这种转换在什么情况下会自动完成,以及我什么时候必须显式调用myInstance.ToString()? 最佳答案 简而言之,当定义了隐式或显式转换运算符时:classWithImplicit{publicstaticimplicitoperatorstring(WithImplicitx){retu

c# - LINQ to Entities 无法识别方法 'Int32 Int32(System.String)' 方法,并且无法将此方法翻译成存储表达式

我正在尝试使用LinqtoEntities查询数据库上下文,但出现此错误:LINQtoEntitiesdoesnotrecognizethemethod'Int32Int32(System.String)'method,andthismethodcannotbetranslatedintoastoreexpression.`代码:publicIEnumerableGetCourseName(){varcourse=fromoinentities.UniversityCoursesselectnewCourseNames{CourseID=Convert.ToInt32(o.Course

C# HashSet<string> 到单个字符串

我有一个HashSet定期添加。我想要做的是在不执行foreach循环的情况下将整个HashSet转换为字符串。有人有例子吗? 最佳答案 无论您是否显式编写内容,您都将遍历内容。然而,如果没有明确的写作,如果你说“cast”是指“concatenate”,你会这样写stringoutput=string.Join("",yourSet);//.NET4.0stringoutput=string.Join("",yourSet.ToArray());//.NET3.5 关于C#HashSe

c# - ICollection<string> 到 string[]

我有一个ICollection类型的对象.转换为string[]的最佳方式是什么?.如何在.NET2中完成此操作?如何在更高版本的C#中更清晰地完成此操作,或许在C#3中使用LINQ? 最佳答案 您可以使用以下代码片段将其转换为普通数组:string[]array=newstring[collection.Count];collection.CopyTo(array,0);这应该可以完成工作:) 关于c#-ICollection到string[],我们在StackOverflow上找到一

C# Linq - 无法将 IEnumerable<string> 隐式转换为 List<string>

我有一个这样定义的列表:publicListAttachmentURLS;我正在像这样向列表中添加项目:instruction.AttachmentURLS=curItem.Attributes["ows_Attachments"].Value.Split(';').ToList().Where(Attachment=>!String.IsNullOrEmpty(Attachment));但我收到此错误:无法将IEnumerable隐式转换为列表我做错了什么? 最佳答案 Where方法返回IEnumerable.尝试添加.ToLis

c# - 如何将 XML 转换为 List<string> 或 String[]?

如何将以下XML转换为List或String[]:12 最佳答案 听起来您更多的是在解析之后而不是完整的XML序列化/反序列化。如果您可以使用LINQtoXML,这将非常简单:usingSystem;usingSystem.Linq;usingSystem.Xml.Linq;publicclassTest{staticvoidMain(){stringxml="12";XDocumentdoc=XDocument.Parse(xml);varlist=doc.Root.Elements("id").Select(element=>e

c# - 如何将 string[] 转换为 ArrayList?

我有一个字符串数组。如何将其转换为System.Collections.ArrayList? 最佳答案 string[]myStringArray=newstring[2];myStringArray[0]="G";myStringArray[1]="L";ArrayListmyArrayList=newArrayList();myArrayList.AddRange(myStringArray); 关于c#-如何将string[]转换为ArrayList?,我们在StackOverfl

c# - 如何在一行 C# 3.0 中将 object[] 转换为 List<string>?

好吧,我放弃了,你如何在一行中做到这一点?publicobjectConvert(object[]values,TypetargetType,objectparameter,System.Globalization.CultureInfoculture){//Listfields=values.ToList();//Listfields=valuesasList;//Listfields=(List)values;Listfields=newList();foreach(objectvalueinvalues){fields.Add(value.ToString());}//proce

c# - 这段代码怎么可能: "ArgumentOutOfRangeException: startIndex cannot be larger than length of string"?

我的C#代码中有以下方法://////Removesthefirst(leftmost)occurenceofafroma.//////Thestringtoremovethefrom.Cannotbenull.///Thesubstringtolookforandremovefromthe.Cannotbenull.//////Therestofthe,afterthefirst(leftmost)occurenceoftheinit(ifany)hasbeenremoved.////////////Ifthedoesnotoccurwithinthe,theisreturnedin

c# - String.GetHashCode() 返回不同的值

为什么GetHashCode()为同一个字符串返回不同的值?我无法描述如何复制它,但请相信这不是一个恶作剧,并且以下两行来self的watch窗口在两个不同的时间:"DDD.Events.Application.ApplicationReferenceCreated".GetHashCode()-1386151123int"DDD.Events.Application.ApplicationReferenceCreated".GetHashCode()1858139950int怎么会这样?我不知道这是否有帮助,但我在VS2010中运行.NET4.0,并且正在调试NServiceBus应