我正在尝试使用 C# 中的 LINQ 获取数组中最常见的值。
例如,
int[] input = {1, 1, 1, 3, 5, 5, 6, 6, 6, 7, 8, 8};
output = {1, 6}
Run Code Online (Sandbox Code Playgroud)
int[] input = {1, 2, 2, 3 ,3, 3, 5}
output = {3}
Run Code Online (Sandbox Code Playgroud)
请让我知道如何构建 LINQ。
请仔细阅读。这是使用 LINQ 选择最常见值的不同问题
我必须只选择最常见的值。下面的代码类似,但我不能使用 Take(5),因为我不知道结果的数量。
int[] nums = new[] { 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7 };
IEnumerable<int> top5 = nums
.GroupBy(i => i)
.OrderByDescending(g => g.Count())
.Take(5)
.Select(g …Run Code Online (Sandbox Code Playgroud) 我目前正在转换 Java 程序。我来自字节移位运算符。
我想知道>>>=运算符在 C# 中的含义。只是>>=吗?是否>>=在 C# 中移动符号?
我是单元测试的新手,也是 C# 的新手,非常感谢有关如何编写单元测试以确保 AddGrade 方法中的逻辑正常工作的帮助。所以基本上如果成绩 >=0 和 <=100 那么成绩是有效的,它将被添加,其他任何东西都不是,它应该在控制台中显示一个错误。
我看过另一篇关于在 c# 中对 if-else 语句进行单元测试的帖子,并尝试从中解决这个问题,但如果我说实话,它让我感到困惑。我环顾网上等,并尝试了很多不同的方法来尝试解决这个问题,但我发现有时很难将人们的示例应用到我的代码中,所以我认为最好只是发布我的代码并获得一些帮助,任何帮助都会不胜感激:)
我正在使用 Xunit 进行单元测试。该项目在控制台中运行。
这是带有main方法的程序类
using System;
using System.Collections.Generic;
namespace GradeBook
{
class Program
{
static void Main(string[] args)
{
var book = new Book("Dave's");
//add grade to the book
book.AddGrade(90.5);
book.AddGrade(80.5);
book.AddGrade(70.5);
book.AddGrade(60.5);
var stats = book.GetStatistics();
Console.WriteLine($"This Gradebooks name is {book.Name}");
Console.WriteLine($"The highest grade is {stats.High:N1}");
Console.WriteLine($"The lowest grade is {stats.Low:N1}");
Console.WriteLine($"The average grade is {stats.Average:N1}");//N1,N2 etc. is number of decimal places
}
}
} …Run Code Online (Sandbox Code Playgroud) 我偶然发现了一种我意想不到的行为,也找不到相关文档。我有一个带有 2 个参数的函数,其中一个具有默认值。当在 Select 内部调用该函数时,它出于某种原因获取调用它的值并将其应用于两个参数。例子:
具有以下功能:
private static Directions PrintDir(Directions dir, int distance = 1)
{
Console.WriteLine($"Direction {dir}, distance {distance}");
return dir;
}
Run Code Online (Sandbox Code Playgroud)
如果单独调用,它会按预期运行:
using System;
using System.Linq;
public class Program
{
enum Directions { Left, Up, Right, Down };
public static void Main()
{
PrintDir(Directions.Left);
PrintDir(Directions.Up);
PrintDir(Directions.Right);
PrintDir(Directions.Down);
}
}
Run Code Online (Sandbox Code Playgroud)
印刷:
Direction Left, distance 1
Direction Up, distance 1
Direction Right, distance 1
Direction Down, distance 1
Run Code Online (Sandbox Code Playgroud)
但是当在 LINQ 表达式中调用时:
using System;
using System.Linq;
public class Program
{
enum …Run Code Online (Sandbox Code Playgroud) 这段代码是我偶然看到的。代码大致如下。
public class Question2
{
public void Run()
{
List<Task> tasks = new List<Task>();
for (int i = 0; i < 10000; i++)
{
tasks.Add(Task.Factory.StartNew(() =>
{
GetNextId();
}));
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine("currentId=" + GetCurrentId());
Console.ReadKey();
}
private int currentId;
public int GetNextId()
{
currentId++;
return currentId;
}
public int GetCurrentId()
{
return currentId;
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么结果不是 10000?有人可以告诉我为什么以及如何纠正它。
我有一个嵌套字典,如下所示:
Dictionary<string, Dictionary<string, int>> users = new Dictionary<string, Dictionary<string, int>>();
Run Code Online (Sandbox Code Playgroud)
第一个字符串是用户的姓名,第二个字符串是他正在参加的比赛,int 是他的分数。一名用户可以参加多项竞赛。
我的任务是通过添加所有得分来找到得分最高的用户。现在我使用这段代码:
foreach (var user in users)
{
bestUsers.Add(user.Key, 0);
foreach (var contest in user.Value)
{
bestUsers[user.Key] += contest.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何使用匿名函数来做到这一点,如下所示:
KeyValuePair<string, int> bestUser = users.OrderBy(x => x.Value.Sum());
Run Code Online (Sandbox Code Playgroud) 我以这种方式定义了 PropertyChangedEventHandler:
public event PropertyChangedEventHandler PropertyChanged;
Run Code Online (Sandbox Code Playgroud)
自从我切换到 .NET 7 以来,我收到了 2 条错误消息。一旦CS8612(类型中引用类型的可为空性与隐式实现的成员不匹配。)和CS8618(不可为空变量在退出构造函数时必须包含非空值。考虑将其声明为可为空。)。我不知道如何修复该错误。
我有 2 个列表
List1List2我怎样才能继续循环List2直到到达结束List1?所以,如果我在结束List2之前到达结束List1,重新开始并List2再次循环。一旦我到达 List1 的末尾,我想跳出两个循环并继续执行程序的其余部分。我每次都需要每个列表中的不同项目。
W M S | S M W
01 02 03 | 19 20 21
04 05 06 | 22 23 24
07 08 09 | 25 26 27
10 11 12 | 28 29 30
13 14 15 | 31 32 33
16 17 18 | 34 35 36
Run Code Online (Sandbox Code Playgroud)
这些是巴士的座位安排。我必须编写一个 C# 算法 - 当用户给出一个数字时,算法应返回座位是 W、M 或 S。示例 - 如果用户输入数字 26,算法应返回 M。实现此目的最快的方法是什么?
在我的项目中,我有一个类(可能有多个派生类),其中一个字段是一个字符串。
我们也有实例化这个类的许多对象的代码(出于技术原因,每个实例都有单独的代码)。所以代码文件可能很长。
对于大多数情况,这是可以的。但在极少数情况下,我需要有一个字符串数组作为 TestValue。我知道我可以将其声明为string[]. 但是因为通常我们只分配单个字符串,所以当只需要一个字符串时,有可能不必总是在代码中显式创建一个数组,就像这样:
public class Datapoint
{
public uint Id;
public string[] TestValue;
}
public void CreateEntries()
{
Table.Add(new Datapoint { Id = 1, TestValue = "123" });
Table.Add(new Datapoint { Id = 2, TestValue = "12.9" });
Table.Add(new Datapoint { Id = 3, TestValue = "Enabled" });
Table.Add(new Datapoint { Id = 4, TestValue = "Temperature" });
Table.Add(new Datapoint { Id = 5, TestValue = { "12.3", "9.8", "7.3" } });
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢!
我有以下 C# 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
IEnumerable<Task> tasks = Enumerable.Range(0, 2)
.Select(_ => Task.Run(() =>
{
Task.Delay(10);
Console.Write("*");
}));
await Task.WhenAll(tasks);
Console.Write($"{tasks.Count()} stars!");
}
}
Run Code Online (Sandbox Code Playgroud)
它是不确定的,我让它打印两种输出:
**2 stars!******2 stars!我可以理解星星可能处于不同的位置,但我不明白为什么有 4 颗星星?在我看来,它应该打印 2 颗星。
c# ×11
.net ×2
arrays ×2
linq ×2
.net-7.0 ×1
dictionary ×1
if-statement ×1
java ×1
lambda ×1
list ×1
loops ×1
select ×1
string ×1
unit-testing ×1
xunit.net ×1