有谁知道解决这个聪明的方法?
public static void Invert(this ref bool value)
{
value = !value;
}
Run Code Online (Sandbox Code Playgroud)
c#说我不能在扩展名中使用"ref"或"out".但是存在像List.Clear()这样的扩展.
我有一个产生返回值的方法.例如:
public static IEnumerable<int> GetValues()
{
for (int i = 0; i < 10; i++)
{
yield return i;
}
}
Run Code Online (Sandbox Code Playgroud)
当我用foreach调用这个方法时,yield return i;被调用了10次
foreach (int i in GetValues())
{
Console.WriteLine(i);
}
Run Code Online (Sandbox Code Playgroud)
当我用for循环调用它时,yield return i;被称为阶乘10次
for (int i = 0;i< 10;i++)
{
Console.WriteLine(GetValues().ElementAt(i));
}
Run Code Online (Sandbox Code Playgroud)
问题:有没有办法保持for-loop并避免那些多次调用ElementAt(i)?或者......我可以通过其索引从IEnumerable中调用一个元素,而不会导致其先前元素的迭代吗?我发现的唯一的事情就是这个,但是
for (int i = 0; i < 10; i++)
{
Console.WriteLine(GetValues().Skip(i).First());
}
Run Code Online (Sandbox Code Playgroud)
也不起作用.
运行完美,但没有结果只有光标.预期的结果是-15
int[] dizi = { 4, 5, -15, 22, -34, 3, 0, 7, 43, 100 };
int maxNumber = 0;
for (int i = 0; i < dizi.Length; i++)
{
if (dizi[i] < 0)
{
if (maxNumber < dizi[i])
{
maxNumber = dizi[i];
Console.WriteLine(maxNumber);
}
}
}
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud) 有没有办法实现这个目标?
我试过了:
string str = "{34.10,0,0.00}"; //the string as I get it from Postgres DB
decimal[] won;
won = (decimal[])(str); //Cannot convert type 'string' to 'decimal[]'
Run Code Online (Sandbox Code Playgroud)
我理想的是要进入won:
won[0] = 34.10
won[1] = 0
won[2] = 0.00
Run Code Online (Sandbox Code Playgroud)
当然,我可以用逗号分开,把它放在数组中,但我想知道是否有更好的方法.
我有一个文字,
'Me and you against the world' // false
'Can i have an email address' // true
'This is an' // true
'an' //true
Run Code Online (Sandbox Code Playgroud)
我想检查一下这个词an是否在我的String中.
如何检查文本中是否包含SQL中的特定单词?我无法添加全文目录.我能做的其他事
SELECT * FROM TABLE WHERE CONTAINS(Text, 'an')
Run Code Online (Sandbox Code Playgroud) 我想在给定角色第一次出现之后修剪一个字符串的结尾,在这种情况下是'.'
此字符在字符串中多次出现.
我可以找到与他们都使用的类似的多个问题LastIndexOf(); 这需要第一次出现并删除其余的字符串.
如何在代码中找到总和整数?
List<object> lst = new List<object> { "c#", 12, "Simi", 236.5, 17, "Asp.net", 98 };
lst.Where(c => c is int).ToList().Sum();
lst.Where(c => c is string).ToList().Max(d => d.ToString().Length);
Run Code Online (Sandbox Code Playgroud)