草庐IT

file_contents_as_string

全部标签

c# - 为什么在测试受约束的泛型类型时直接转换失败但 "as"运算符成功?

``在编译一些使用具有类型约束的泛型的C#代码时,我遇到了一个有趣的好奇心。我写了一个快速测试用例来说明。我在VisualStudio2010中使用.NET4.0。namespaceTestCast{publicclassFruit{}publicclassApple:Fruit{}publicstaticclassTest{publicstaticvoidTestFruit(FruitTypefruit)whereFruitType:Fruit{if(fruitisApple){Appleapple=(Apple)fruit;}}}}转换为Apple失败并出现错误:“无法将类型‘Fr

c# - 如何通过整数索引引用 Dictionary<string, string> 中的项目?

我做了一个字典集合,以便我可以通过它们的字符串标识符快速引用这些项目。但我现在还需要通过索引计数器访问这个集合(foreach在我的真实示例中不起作用)。我必须对下面的集合做什么才能通过整数索引访问它的项目?usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceTestDict92929{classProgram{staticvoidMain(string[]args){Dictionaryevents=newDictionary();events.Add("firs

c# - : this() As a constructor

我正在尝试更好地理解一般实践...特别是在构造函数中派生this()。我知道它的代码较少,但我认为它的可读性较差。这样做是常见的/好的做法吗?还是编写第二个专门处理它的构造函数更好?publicSomeOtherStuff(stringrabble):this(rabble,"bloop"){}或PublicSomeOtherStuff(stringrabble){//setbloop}任何输入将不胜感激 最佳答案 最好尽可能使用this()。否则你将复制一些代码,这违反了DRY(不要重复自己)原则。重复自己的问题在于,每次您需要进

c# - 测试使用 HttpContext.Current.Request.Files 的 Web API 方法?

我正在尝试为使用HttpContext.Current.Request.Files的WebAPI方法编写测试,经过详尽的搜索和实验后,我无法弄清楚如何模拟它。正在测试的方法如下所示:[HttpPost]publicHttpResponseMessagePost(){varrequestFiles=HttpContext.Current.Request.Files;varfile=requestFiles.Get(0);//dosomeotherstuff...}我意识到有otherquestionssimilartothis,但他们没有解决这个具体情况。如果我尝试模拟上下文,我会遇到H

c# - .net 中的转换 : Native Utf-8 <-> Managed String

我创建了这两种方法来将nativeutf-8字符串(char*)转换为托管字符串,反之亦然。以下代码完成这项工作:publicIntPtrNativeUtf8FromString(stringmanagedString){byte[]buffer=Encoding.UTF8.GetBytes(managedString);//notnullterminatedArray.Resize(refbuffer,buffer.Length+1);buffer[buffer.Length-1]=0;//terminating0IntPtrnativeUtf8=Marshal.AllocHGlob

c# - PInvoke C# : Function takes pointer to function as argument

我想在我的C#代码中访问这个函数,这可能吗?所以最后C++代码会调用我的函数并应用名为“sFrameofData”的结构。C++代码://Theusersuppliedfunctionwillbecalledwheneveraframeofdataarrives.DLLintCortex_SetDataHandlerFunc(void(*MyFunction)(sFrameOfData*pFrameOfData));这也许行得通吗?C#代码:[DllImport("Cortex_SDK.dll")]publicexternstaticintCortex_SetDataHandlerFu

c# - 将由返回字符分隔的字符串转换为 List<string> 的最佳方法是什么?

我需要经常将“字符串block”(包含返回字符的字符串,例如来自文件或文本框)转换为List.有什么方法比下面的ConvertBlockToLines方法更优雅?usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;namespaceTestConvert9922{classProgram{staticvoidMain(string[]args){stringtestBlock="lineone"+Environment.NewLine+"linetwo"+Environment.NewLine+"linethree"

c# - 如何将 string[] 组合成字符串,中间有空格

如何将string[]解析为中间有空格的字符串您将如何重构这段代码?internalstringConvertStringArrayToString(string[]array){StringBuilderbuilder=newStringBuilder();builder.Append(array[0]);for(inti=1;i 最佳答案 已经有一个方法了:String.Join("",array)这将在每个元素之间放置一个空格。请注意,如果您的任何元素都是空字符串,您最终会得到彼此相邻的空格,因此您可能需要提前过滤这些元素,如

List<String[]> 的 C# "funny"问题

我的C#应用程序中的List有一些奇怪的问题。这一定是分配错误或者我做错了什么(我是普通的C#开发人员)。让我举一个接近我的台词的例子:ListMyPrimaryList=newList();ListMySecondaryList=newList();String[]array;StringarrayList="one,two,three,four,five";array=arrayList.Split(',');MyPrimaryList.Add(array);MySecondaryList.Add(array);MyPrimaryList[0][0]+="half";所以现在我希望

c# join string 逗号分隔,但双引号里面的所有值

我有一个字符串列表newList{"One","Two","Three","Four","Five","Six"}我想要一个包含这个内容的字符串(包括双引号)"One","Two","Three","Four","Five","Six"因为将写入一个文本文件,该文件将是一个数组[]={my_string}我试过了,没有成功varjoinedNames=fields.Aggregate((a,b)=>"\""+a+","+b+"\"");LittleLINQ帮助将不胜感激:) 最佳答案 varjoinedNames="\""+stri