我正在尝试在插值字符串中使用变量,但运气不佳。我该怎么做?
var name = "mike";
var desc = "hello world {name}";
var t = $"{ desc }";
Console.WriteLine(t); // PRINTS: hello world {name}
Run Code Online (Sandbox Code Playgroud)
这是我要实现的目标:
Console.WriteLine(t); // PRINTS: hello world mike
Run Code Online (Sandbox Code Playgroud)
这可能吗?
例如,假设我有一个方法:
public string FormatString(string s) {
var Now = DateTime.Now;
return $s;
}
Run Code Online (Sandbox Code Playgroud)
用法:
Console.WriteLine(FormatString("The time is {Now}"));
Run Code Online (Sandbox Code Playgroud) 我有一个学生的XML文件和他们的分数.以下是一个学生节点的示例.我有其他学生关注.
<Students>
<Student>
<Name> Billy Blue </Name>
<Grade> 1 </Grade>
<Sex> Male </Sex>
<Age> 7 </Age>
<Picture> c:/School/Students/BillyBlue </Picture>
<Grades>
<Score>80.5</Score>
<Score>100.0</Score>
<Score>70.0</Score>
<Score>0.0</Score>
<Score>0.0</Score>
<Score>0.0</Score>
<Score>0.0</Score>
<Score>0.0</Score>
<Score>0.0</Score>
<Score>0.0</Score>
</Grades>
</Student>
Run Code Online (Sandbox Code Playgroud)
我想打印出每个学生的平均分数.我的代码目前正在打印文件中所有分数的平均值.
foreach (XElement student in listStudents)
{
IEnumerable<XElement> listScores =
from XElement in listStudents.Descendants("Grades").Elements("Score")
.Where(x => Convert.ToSingle(x.Value) != 0.0)
select XElement;
var fAverageScore = 0.0;
foreach (XElement score in listScores)
{
fAverageScore += Convert.ToSingle(score.Value);
}
Console.WriteLine("Average Score: " + (fAverageScore / listScores.Count()).ToString("0.00"));
Console.WriteLine("\n");
}
Run Code Online (Sandbox Code Playgroud) 我有一个自定义通用数组/容器,它有一个排序方法:
public void Sort()
{
for (int i = 0; i < Count; i++)
{
int min = i;
for (int j = i + 1; j < Count; j++)
if (Items[j].CompareTo(Items[min]) < 0)
min = j;
if (min == i) continue;
T temp = Items[i];
Items[i] = Items[min];
Items[min] = temp;
}
}
Run Code Online (Sandbox Code Playgroud)
Items是T对象的数组.T实现IComparable并具有CompareTo方法:
public int CompareTo(object obj)
{
if (!(obj is Player))
return -1;
Player player = (Player)obj;
if (Name.CompareTo(player.Name) < 0)
return -1;
else if (Name.CompareTo(player.Name) == …Run Code Online (Sandbox Code Playgroud) 我有这个字符串:
var a = "a new test string today";
Run Code Online (Sandbox Code Playgroud)
我如何解析一个只包含单词的另一个字符串
"a new test"
Run Code Online (Sandbox Code Playgroud) 如何在 Angular 中创建一个月份日期选择器,不包括隐藏日期和年份?
此以下链接将执行月份和年份选择器。我试图操纵它只做 Month Only。如何配置?
闪电战:
https://stackblitz.com/angular/gxymgjpprdy?file=src%2Fapp%2Fdatepicker-views-selection-example.ts
结果应该是这样的,发出的值可以是例如 May:a) 5 或 b) 2020 年 5 月 1 日。我可以删除 Day 和 year later 。
typescript angular-material angular angular-material-datetimepicker angular8
使用services.AddSingleton<SomeService, SomeServiceImplementation>()而不是有services.AddSingleton<SomeServiceImplementation>()什么好处?
例如我有一个示例界面
interface ISampleInterface
{
void DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
和一个样本类:
class SampleClass : ISampleInterface
{
public void DoSomething()
{
console.write("hi");
}
}
Run Code Online (Sandbox Code Playgroud)
不,我做 services.AddSingleton<SampleClass>()
为什么或何时使用services.AddSingleton<ISampleInterface, SampleClass>()?
谢谢你的帮助!:-)
我正在寻找满足以下要求的模式的简单 C# 示例:
这似乎是我可以轻松使用 async 和 await 关键字的场景,但是在查看了几个 Hello World 示例后,我很惊讶我无法轻松地同时满足上述所有条件。例如,虽然使用 await 关键字使我的 UI 不会阻塞并允许我的应用程序在我等待方法完成时继续响应事件,但这些方法仍在同步执行。或者,如果我能够异步执行,我在尝试将输入参数传递给调用方法并接收返回结果时遇到了麻烦。
以下内容似乎符合我的要求,并让我相信我可以充分利用它,但首先我想得到您关于如何改进它的反馈。
我之前玩过 Threads 并发现它使我的代码变得更加复杂,并期望“async 和 await”关键字会让我的生活更轻松,但以下似乎满足我要求的代码甚至没有使用这些关键字。我在这里使用“并行任务库”吗?如果是这样,您将如何将此库中的功能与 async 和 await 功能进行对比?
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace ASyncCourse
{
static class Program
{
static void Main()
{
int result1 = 0;
int result2 = 0;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Task task1 = Task.Run(() => result1 = DoSomeWork(1));
Debug.WriteLine($"After Task1: {stopWatch.Elapsed}");
Task task2 = Task.Run(() => result2 = DoSomeOtherWork(10)); …Run Code Online (Sandbox Code Playgroud) 我有一个字典如下,其中键是一个字符串,值是一个双精度列表:
Dictionary<string, List<double>> dataStore = new Dictionary<string, List<double>>();
List<string> channel_names = new List<string>(); // contains the keys
Run Code Online (Sandbox Code Playgroud)
现在,当我想向此字典添加数据时,我会这样做:
if (dataStore.ContainsKey(channel_names[j]))
{
dataStore[channel_names[j]].Add(measurement);
}
else
{
dataStore.Add(channel_names[j], new List<double>((int)measurement));
}
Run Code Online (Sandbox Code Playgroud)
第一个语句(添加到现有的键)工作正常,但第二个语句有问题,即当我试图用列表中的第一个双精度初始化键时。第一次测量被遗漏了。任何人都可以请建议为什么?
谢谢
假设我有这个方法:
public void UpdateEmployee ( String empFirst, String empLast, String empAddress,
String empType, String empPhoneNo, String empSalary, DateTime? dob, String empDepartment)
Run Code Online (Sandbox Code Playgroud)
有没有办法不在参数中一直重复 String 类型?
谢谢
所以我创建了一个任务处理程序。我想让它运行一段预定的保证时间,然后我想做一些我的事情,只有这样我才需要等待处理程序的结果。就像是:
var th = TaskCreator();
th.awaitFor(5000);
//do some work
var result = await th;
Run Code Online (Sandbox Code Playgroud)
那么异步任务如何运行给定的秒数呢?
c# ×9
async-await ×2
.net-core ×1
angular ×1
angular-material-datetimepicker ×1
angular8 ×1
asp.net-core ×1
asynchronous ×1
blazor ×1
dictionary ×1
generics ×1
list ×1
roslyn ×1
task ×1
typescript ×1