Questions tagged «c#»

C#(发音为“ See Sharp”)是由Microsoft开发的一种高级,静态类型的多范例编程语言。C#代码通常针对Microsoft的.NET系列工具和运行时,其中包括.NET Framework,.NET Core和Xamarin。使用此标记可解决有关用C#或C#正式规范编写的代码的问题。


4
在字符串中使用变量
在PHP中,我可以执行以下操作: $name = 'John'; $var = "Hello {$name}"; // => Hello John C#中是否有类似的语言构造? 我知道有,String.Format();但是我想知道是否可以在不调用字符串的函数/方法的情况下完成。



4
例外:“不支持URI格式”
我有一个指向目录的绝对本地路径: "file:\\C:\\Users\\john\\documents\\visual studio 2010\\Projects\\proj" 但是,当我尝试将其放入DirectoryInfo的ctor中时,出现“不支持URI格式”异常。 我用谷歌搜索并查看了SO,但是我只看到带有远程路径的解决方案,而不是本地路径。我希望某种转换方法...
89 c#  .net  .net-4.0 

4
为什么等待后HttpContext.Current为null?
我有以下测试WebAPI代码,我不在生产中使用WebAPI,但由于进行了有关此问题的讨论,所以进行了此操作:WebAPI异步问题 无论如何,这是令人讨厌的WebAPI方法: public async Task<string> Get(int id) { var x = HttpContext.Current; if (x == null) { // not thrown throw new ArgumentException("HttpContext.Current is null"); } await Task.Run(() => { Task.Delay(500); id = 3; }); x = HttpContext.Current; if (x == null) { // thrown throw new ArgumentException("HttpContext.Current is null"); …



4
AddWithValue参数为NULL时发生异常
我有以下代码用于指定SQL查询的参数。我在使用时遇到了异常Code 1;但是我使用时工作正常Code 2。在这里,Code 2我们检查是否为null,因此是否为if..else块。 例外: 参数化查询'{@application_ex_id nvarchar(4000))SELECT E.application_ex_id A'期望未提供参数'@application_ex_id'。 代码1: command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID); 代码2: if (logSearch.LogID != null) { command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID); } else { command.Parameters.AddWithValue("@application_ex_id", DBNull.Value ); } 题 您能否解释一下为什么它无法从代码1中的logSearch.LogID值中获取NULL(但能够接受DBNull)? 有更好的代码来处理吗? 参考: 将空值分配给SqlParameter 返回的数据类型因表中的数据而异 从数据库smallint到C#可为空的int的转换错误 DBNull的意义是什么? 码 public Collection<Log> GetLogs(LogSearch logSearch) { Collection<Log> logs = new Collection<Log>(); using (SqlConnection connection = …
89 c#  .net  ado.net 

6
如何用匿名方法返回值?
这失败了 string temp = () => {return "test";}; 与错误 无法将lambda表达式转换为“字符串”类型,因为它不是委托类型 错误是什么意思,我该如何解决?
89 c#  .net  lambda 

3
在运行时动态添加C#属性
我知道有一些问题可以解决这个问题,但是答案通常遵循推荐字典或参数集合的方式,在我的情况下不起作用。 我正在使用通过反射工作的库,以对具有属性的对象进行许多巧妙的操作。这适用于定义的类以及动态类。我需要进一步迈出一步,并按照以下方针做点事情: public static object GetDynamicObject(Dictionary<string,object> properties) { var myObject = new object(); foreach (var property in properties) { //This next line obviously doesn't work... myObject.AddProperty(property.Key,property.Value); } return myObject; } public void Main() { var properties = new Dictionary<string,object>(); properties.Add("Property1",aCustomClassInstance); properties.Add("Property2","TestString2"); var myObject = GetDynamicObject(properties); //Then use them like this …


8
如何将DateTime格式化为Web UTC格式?
我有一个DateTime,我想将其格式化为“ 2009-09-01T00:00:00.000Z”,但是以下代码为我提供了“ 2009-09-01T00:00:00.000+01:00”(两行): new DateTime(2009, 9, 1, 0, 0, 0, 0, DateTimeKind.Utc).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffzzz") new DateTime(2009, 9, 1, 0, 0, 0, 0, DateTimeKind.Utc).ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffzzz") 有什么想法如何使其工作吗?
89 c#  .net  datetime  utc 



By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.