我有一些List<Building> playersBuildings包含所有来自Bulilding类的对象的列表,我遇到了一些对我来说非常不直观的东西.
以下代码:
foreach (Factory f in playersBuildings) {
f.Update (deltaTime);
}
Run Code Online (Sandbox Code Playgroud)
试图施放的每个元素playersBuildings,以Factory与产生InvalidCastException的错误.虽然它有一定道理,但在编写代码时我希望foreach只会省略与指定类型不匹配的元素.
我首先想到的解决方案是:
foreach (Building f in playersBuildings) {
if ((f is Factory) == false)
continue;
(f as Factory).Update (deltaTime);
}
Run Code Online (Sandbox Code Playgroud)
但我不喜欢使用as关键字.每次我想在我的f下面做一些迭代时需要输入额外的+10个字符是令人沮丧的.
有没有更优雅的方法来跳过不匹配正确类型的元素?
我有两个具有相同名称的函数,主要区别在于不同的返回类型.我怎么能重载函数才能使用相同的名称,因为有时我需要point3d或point3f数组,以下函数名称给出相同的错误:
public static Point3d[] GetNGonCenters(this Mesh mesh)
{
Point3d[] centers = new Point3d[mesh.Ngons.Count];
for (int i = 0; i < mesh.Ngons.Count; i++)
centers[i] = mesh.Ngons.GetNgonCenter(i);
return centers;
}
public static Point3f[] GetNGonCenters(this Mesh mesh)
{
Point3f[] centers = new Point3f[mesh.Ngons.Count];
for (int i = 0; i < mesh.Ngons.Count; i++)
centers[i] = (Point3f)mesh.Ngons.GetNgonCenter(i);
return centers;
}
Run Code Online (Sandbox Code Playgroud) 当我运行以下代码时
public class Program
{
public static void Main()
{
string s = "480";
Console.WriteLine(1 == -1 ? 0 : s[1]);
Console.WriteLine(s[1]);
}
}
Run Code Online (Sandbox Code Playgroud)
我明白了
56
8
Run Code Online (Sandbox Code Playgroud)
我不明白我是怎么得到56的.
我正在尝试编写的代码是替换文本文件中的一串字.虽然我能够将文件的内容读取到控制台,但我无法替换字符串并将新字符串写入文件.
这是我的代码:
private static void filesys_created (object sender, FileSystemEventArgs e)
{
using (StreamReader sr = new StreamReader(e.FullPath))
{
Console.WriteLine(sr.ReadToEnd());
File.ReadAllText(e.FullPath);
sr.Close();
}
using (StreamWriter sw = new StreamWriter(e.FullPath))
{
string text = e.FullPath.Replace("The words I want to replace");
string newtext = "text I want it to be replaced with";
sw.Write(e.FullPath, text);
sw.Write(newtext);
sw.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是.Replace正在删除文本文件中的所有内容,只插入目录的路径.
我正在尝试找到正确的正则表达式来提取.下面字符串中的数字。例如,第一行应该返回和数组1 1 1 1 1,第二行应该返回2 1 0 1 2。我似乎无法找到正确的正则表达式来实现这一点。任何帮助,将不胜感激。
line = 0.1, 1.1, 2.1, 3.1, 4.1 // payline 0
line = 0.2, 1.1, 2.0, 3.1, 4.2 // payline 1
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有下面的代码,但它只是返回字符串中的所有数字。例如,第一行返回0 1 1 1 2 1 3 1 4 1 0,第二行返回0 2 1 1 2 0 3 1 4 2 1
foreach (var line in Paylines)
{
int[] lines = (from Match m in Regex.Matches(line.ToString(), @"\d+")
select int.Parse(m.Value)).ToArray();
foreach (var x in …Run Code Online (Sandbox Code Playgroud) 列表枚举器在放入列表时不起作用.例如,在两个列表的情况下(两个枚举器)
public void test()
{
var firstList = new List<int>() { 1, 2, 3 };
var secondList = new List<int>() { 4, 5, 6 };
var lists = new List<List<int>>();
lists.Add(firstList);
lists.Add(secondList);
// Not working
var iterators = lists.Select(x => x.GetEnumerator()).ToList();
iterators.ForEach(x => x.MoveNext());
iterators.ForEach(x => Console.WriteLine(x.Current));
// Working
var firstIterator = iterators[0];
var secondIterator = iterators[1];
firstIterator.MoveNext(); secondIterator.MoveNext();
Console.WriteLine(firstIterator.Current);
Console.WriteLine(secondIterator.Current);
}
Run Code Online (Sandbox Code Playgroud)
第一部分不工作,它打印0 0,而第二部分工作,它打印1 4.
我不明白第一部分的错误是什么,以及如何解决.
我有两个字符串列表,我需要进行比较并验证至少一个位置是否相同.有人能帮我吗?
List<string> listA = new List<string>();
listA.Add("b");
listA.Add("c");
listA.Add("a");
List<string> listB = new List<string>();
listB.Add("h");
listB.Add("b");
listB.Add("d");
Run Code Online (Sandbox Code Playgroud)
预期产量:
b = h false
b = b true (break)
b = d false
Run Code Online (Sandbox Code Playgroud) 我有一个JSON数组字符串。我想反序列化它并想获取 Asp.Net Core 视图页面中的每个元素。我已经用该array split方法尝试过,但它在开始和结束时都以 [] 返回。请帮我解决这个问题。
我的数据是:
[
"a0xctflnmzfkxqibwgli.png",
"hjivmkuk2jafe2cfllpv.jpg",
"i2zcbw0se2btbiq7l4u6.gif",
"a0rzmnbjnbtvmdv1osxo.jpg",
"p5ok5a0ozni7lqlvoov2.gif"
]
Run Code Online (Sandbox Code Playgroud)
所需输出:
a0xctflnmzfkxqibwgli.png
hjivmkuk2jafe2cfllpv.jpg
i2zcbw0se2btbiq7l4u6.gif
p5ok5a0ozni7lqlvoov2.gif
Run Code Online (Sandbox Code Playgroud) MemoryStream我正在使用和将对象导出到 csv StreamWriter。但保存文件后,我发现文件末尾丢失了。它在一条线的中间被切断。
下面是我的代码:
public void Export(ProductExport productExport)
{
var country = (Country)(int.Parse(productExport.Firma.Code));
using(var stream = new MemoryStream())
{
var sw = new StreamWriter(stream);
// Write header row
sw.WriteLine(
"{0};{1};{2};{3};{4};{5};{6};{7};{8};{9}",
"Groep",
"TecDoc Leverancier",
"TecDoc Ref",
"SA",
"EAN",
"Omschrijving NL",
"V/E",
"VPE",
"Prijs per",
"Bruto prijs"
);
foreach(var product in productExport.Products)
{
// Write header row
sw.WriteLine(
"{0};{1};{2};{3};{4};{5};{6};{7};{8};{9}",
product.Artgrp,
product.TecDoc != null ? product.TecDoc.Supplier : "",
product.TecDoc != null ? product.TecDoc.Value : "",
product.Ids.Where<Id>(i => i.Type == "SA").Select(i => …Run Code Online (Sandbox Code Playgroud) I'm trying to create a list and insert values in to it. But when I add a new item it changes all existing items in the list to the same value I add and I don't understand why.
Here is my code:
public List<Times> CreateListOfTimes()
{
var listOfTimes = new List<Times>();
var times = new Times();
int startTime = 8;
int endTime = startTime + 1;
for (int i = 0; i < 8; i++)
{
times.StartTime = startTime;
times.EndTime …Run Code Online (Sandbox Code Playgroud) 我有以下循环:
string[] sfind = new string[Globais.dep.Length];
int udt = 0;
for (int getnum = 0; getnum <= Globais.dep.Length; getnum++)
{
if (Globais.dep[getnum].Contains(items[1]))
{
sfind[udt] = Globais.dep[getnum];
udt ++;
}
}
Run Code Online (Sandbox Code Playgroud)
我有更多的代码遵循这个循环,但我用调试器观察它并且它不运行下面的代码,在for循环结束后它会破坏整个代码.我在这做错了什么?我没有看到周期的问题.
无论如何,谢谢!
我正在学习C#并尝试过这个问题.请看下面问题下面的两次尝试,有人可以指出我在哪里/怎么出错了吗?谢谢.
尝试1:
如果第一个布尔值为真且第二个布尔值为假,则返回true.
public static bool ReturnTrueAndFalse(bool boolean1, bool boolean2)
{
if (boolean1 = true && (boolean2 = false))
{
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
尝试2:
public static bool ReturnTrueAndFalse(bool boolean1, bool boolean2)
{
boolean1 = true;
boolean2 = false;
return boolean1 && boolean2;
}
Run Code Online (Sandbox Code Playgroud) c# ×12
.net ×1
arrays ×1
asp.net-core ×1
boolean ×1
break ×1
casting ×1
for-loop ×1
foreach ×1
if-statement ×1
iterator ×1
json ×1
linq ×1
memorystream ×1
overloading ×1
regex ×1
streamwriter ×1
string ×1