Questions tagged «c#»

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

5
有没有一种方法可以指定“空” C#lambda表达式?
我想声明一个“空” lambda表达式,它不会执行任何操作。有没有一种方法可以执行这种操作而不需要该DoNothing()方法? public MyViewModel() { SomeMenuCommand = new RelayCommand( x => DoNothing(), x => CanSomeMenuCommandExecute()); } private void DoNothing() { } private bool CanSomeMenuCommandExecute() { // this depends on my mood } 我这样做的意图只是控制WPF命令的启用/禁用状态,但这是一个问题。也许对于我来说还为时过早,但我想必须有一种方法可以x => DoNothing()像这样以某种方式声明lambda表达式来完成同一件事: SomeMenuCommand = new RelayCommand( x => (), x => CanSomeMenuCommandExecute()); 有什么办法可以做到这一点?似乎没有必要采取无所事事的方法。
118 c#  lambda 

3
加载自定义配置文件
我知道我可以使用静态ConfigurationManager.OpenExe(exePath)方法打开与程序集相关的配置文件,但我只想打开与程序集无关的配置。只是一个标准的.NET配置文件。
118 c#  configuration 


11
.NET唯一对象标识符
有没有办法获取实例的唯一标识符? GetHashCode()指向相同实例的两个引用是相同的。但是,两个不同的实例可以(很容易)获得相同的哈希码: Hashtable hashCodesSeen = new Hashtable(); LinkedList<object> l = new LinkedList<object>(); int n = 0; while (true) { object o = new object(); // Remember objects so that they don't get collected. // This does not make any difference though :( l.AddFirst(o); int hashCode = o.GetHashCode(); n++; if (hashCodesSeen.ContainsKey(hashCode)) …

4
我应该避免使用“异步无效”事件处理程序吗?
我知道使用即弃即用async void方法启动任务通常被认为是一个坏主意,因为没有跟踪待处理任务的信息,并且处理此类方法中可能引发的异常非常棘手。 我是否也应该一般避免使用async void事件处理程序?例如, private async void Form_Load(object sender, System.EventArgs e) { await Task.Delay(2000); // do async work // ... } 我可以这样重写它: Task onFormLoadTask = null; // track the task, can implement cancellation private void Form_Load(object sender, System.EventArgs e) { this.onFormLoadTask = OnFormLoadTaskAsync(sender, e); } private async Task OnFormLoadTaskAsync(object sender, …

9
将.net Func <T>转换为.net Expression <Func <T >>
使用方法调用从lambda到Expression很容易... public void GimmeExpression(Expression&lt;Func&lt;T&gt;&gt; expression) { ((MemberExpression)expression.Body).Member.Name; // "DoStuff" } public void SomewhereElse() { GimmeExpression(() =&gt; thing.DoStuff()); } 但是我只想在少数情况下将Func变成表达式... public void ContainTheDanger(Func&lt;T&gt; dangerousCall) { try { dangerousCall(); } catch (Exception e) { // This next line does not work... Expression&lt;Func&lt;T&gt;&gt; DangerousExpression = dangerousCall; var nameOfDanger = ((MemberExpression)dangerousCall.Body).Member.Name; throw new DangerContainer( …
118 c#  .net  lambda  expression  func 

4
复制或克隆DataRow的简单方法?
我正在寻找一种简单的方法来克隆DataRow。有点像为该行拍摄快照并将其保存。然后可以自由更改原始Row的值,但我们还有另一个已保存的副本,该副本不变。这是正确的方法吗? DataRow Source, Destination; // Assume we create some columns and fill them with values Destination.ItemArray = Source.ItemArray; 这是否只是将Snapshot的ItemArray引用设置为指向Source中的那个,还是实际上是单独制作一个副本?我应该这样做吗? Destination.ItemArray = Source.ItemArray.Clone(); 编辑:我不认为第二个代码片段实际上可以编译。
118 c#  datatable  datarow 

5
使用Json.net反序列化JSON对象数组
我尝试使用对以下返回的json使用以下示例结构的API [ { "customer":{ "first_name":"Test", "last_name":"Account", "email":"test1@example.com", "organization":"", "reference":null, "id":3545134, "created_at":"2013-08-06T15:51:15-04:00", "updated_at":"2013-08-06T15:51:15-04:00", "address":"", "address_2":"", "city":"", "state":"", "zip":"", "country":"", "phone":"" } }, { "customer":{ "first_name":"Test", "last_name":"Account2", "email":"test2@example.com", "organization":"", "reference":null, "id":3570462, "created_at":"2013-08-12T11:54:58-04:00", "updated_at":"2013-08-12T11:54:58-04:00", "address":"", "address_2":"", "city":"", "state":"", "zip":"", "country":"", "phone":"" } } ] JSON.net可以与以下结构一起很好地工作 { "customer": { ["field1" : "value", etc...], ["field1" …
118 c#  json.net 

2
确定.NET Core中的操作系统
如何确定我的.NET Core应用运行在哪个操作系统上?过去我可以使用Environment.OSVersion。 确定我的应用程序是在Mac还是Windows上运行的当前方法是什么?
118 c#  .net-core 

7
实体框架迁移重命名表和列
我重命名了几个实体及其导航属性,并在EF 5中生成了一个新的Migration。与EF迁移中的重命名一样,默认情况下,它将删除对象并重新创建它们。那不是我想要的,所以我几乎不得不从头开始构建迁移文件。 public override void Up() { DropForeignKey("dbo.ReportSectionGroups", "Report_Id", "dbo.Reports"); DropForeignKey("dbo.ReportSections", "Group_Id", "dbo.ReportSectionGroups"); DropForeignKey("dbo.Editables", "Section_Id", "dbo.ReportSections"); DropIndex("dbo.ReportSectionGroups", new[] { "Report_Id" }); DropIndex("dbo.ReportSections", new[] { "Group_Id" }); DropIndex("dbo.Editables", new[] { "Section_Id" }); RenameTable("dbo.ReportSections", "dbo.ReportPages"); RenameTable("dbo.ReportSectionGroups", "dbo.ReportSections"); RenameColumn("dbo.ReportPages", "Group_Id", "Section_Id"); AddForeignKey("dbo.ReportSections", "Report_Id", "dbo.Reports", "Id"); AddForeignKey("dbo.ReportPages", "Section_Id", "dbo.ReportSections", "Id"); AddForeignKey("dbo.Editables", "Page_Id", "dbo.ReportPages", "Id"); …

4
C#遍历类属性
我目前正在设置类对象的所有值Record。 这是我现在用来逐个属性填充记录的代码。 // Loop through each field in the result set for (int i = 0; i &lt;= resultItems.Length; i++) { Record newRecord = new Record() { itemtype = resultItems[i - (fieldCount - 0)], itemdesc = resultItems[i - (fieldCount - 1)], prodcode = resultItems[i - (fieldCount - 2)], proddesc = …
118 c#  properties  loops 

5
通过HttpClient将空主体发布到REST API
我尝试调用的API要求我执行POST,但正文为空。我是使用WCF Web API HttpClient的新手,我似乎找不到要写空正文的写代码。我找到了对某些HttpContent.CreateEmpty()方法的引用,但我认为这不是针对Web API HttpClient代码的,因为我似乎找不到该方法。

15
从技术上讲,这是“ Hello World”的O(1)算法吗?
将其归类为“ Hello,World!”的O(1)算法吗??? public class Hello1 { public static void Main() { DateTime TwentyYearsLater = new DateTime(2035,01,01); while ( DateTime.Now &lt; TwentyYearsLater ) { System.Console.WriteLine("It's still not time to print the hello ..."); } System.Console.WriteLine("Hello, World!"); } } 我正在考虑使用 DateTime TwentyYearsLater = new DateTime(2035,01,01); while ( DateTime.Now &lt; TwentyYearsLater ) { …
117 c#  .net  algorithm  big-o 


5
如何仅选择LINQ中具有最高日期的记录
我有一个带有以下字段的表“ lasttraces”。 Id, AccountId, Version, DownloadNo, Date 数据如下所示: 28092|15240000|1.0.7.1782|2009040004731|2009-01-20 13:10:22.000 28094|61615000|1.0.7.1782|2009040007696|2009-01-20 13:11:38.000 28095|95317000|1.0.7.1782|2009040007695|2009-01-20 13:10:18.000 28101|15240000|1.0.7.1782|2009040004740|2009-01-20 14:10:22.000 28103|61615000|1.0.7.1782|2009040007690|2009-01-20 14:11:38.000 28104|95317000|1.0.7.1782|2009040007710|2009-01-20 14:10:18.000 在LINQ to SQL中,如何才能仅获取每个AccountId的最后lasttrace(日期最高的那一个)?
117 c#  .net  linq  linq-to-sql 

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.