如果我使用:
var strings = new List<string> { "sample" };
foreach (string s in strings)
{
Console.WriteLine(s);
strings.Add(s + "!");
}
Run Code Online (Sandbox Code Playgroud)
在Add
在foreach
引发InvalidOperationException(集合被修改;枚举操作可能不会执行),我认为合理的,因为我们从我们脚下拉地毯.
但是,如果我使用:
var strings = new List<string> { "sample" };
strings.ForEach(s =>
{
Console.WriteLine(s);
strings.Add(s + "!");
});
Run Code Online (Sandbox Code Playgroud)
它通过循环迅速射入脚中直到它抛出OutOfMemoryException.
这对我来说是一个惊喜,因为我一直认为List.ForEach只是一个包装foreach
或for for
.
有没有人对这种行为的方式和原因有解释?
(通过FOREach 循环获取无限重复的通用列表)
我想知道Visual Studio代码中console.log的快捷方式是什么?
如果我有一个带有协变类型参数的泛型接口,如下所示:
interface IGeneric<out T>
{
string GetName();
}
Run Code Online (Sandbox Code Playgroud)
如果我定义这个类层次结构:
class Base {}
class Derived1 : Base{}
class Derived2 : Base{}
Run Code Online (Sandbox Code Playgroud)
然后我可以使用显式接口实现在单个类上实现两次接口,如下所示:
class DoubleDown: IGeneric<Derived1>, IGeneric<Derived2>
{
string IGeneric<Derived1>.GetName()
{
return "Derived1";
}
string IGeneric<Derived2>.GetName()
{
return "Derived2";
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用(非泛型)DoubleDown
类并将其强制转换为IGeneric<Derived1>
或IGeneric<Derived2>
按预期运行:
var x = new DoubleDown();
IGeneric<Derived1> id1 = x; //cast to IGeneric<Derived1>
Console.WriteLine(id1.GetName()); //Derived1
IGeneric<Derived2> id2 = x; //cast to IGeneric<Derived2>
Console.WriteLine(id2.GetName()); //Derived2
Run Code Online (Sandbox Code Playgroud)
但是,转换x
为IGeneric<Base>
,给出以下结果:
IGeneric<Base> b = x;
Console.WriteLine(b.GetName()); //Derived1 …
Run Code Online (Sandbox Code Playgroud) 似乎我得到一个错误,指出"产品不包含带0参数的构造函数
public class Products
{
string id;
string name;
double price;
int soldCount;
int stockCount;
public Products(string id, string name, double price,
int soldCount, int stockCount, double tax)
{
this.id = id;
this.name = name;
this.price = price;
this.soldCount = soldCount;
this.stockCount = stockCount;
}
}
//I have got some get and set values for the code above
//but it would have been too long to put in here
public class FoodProducts : Products
{
public FoodProduct()
{
Console.WriteLine("This is …
Run Code Online (Sandbox Code Playgroud) 我有一个计算两个位置的函数,我想得到它们两个,有没有办法从同一个函数返回两个值,而不是把它们变成一个数组.我认为有一些争论或类似的东西... tnx.我的代码:
public static int Location(int p_1, int p_2, int p_3, int p_4)
{
int XLocation = p_2 - p_1;
int YLocation = p_4-p_3;
return XLocation,YLocation;
}
public void Print()
{
}
Run Code Online (Sandbox Code Playgroud) 我有这个简单的类与2个枚举字段,我试图在集合(List<T>
)中找到此对象的一个项目,但Contains方法无法正常工作
public class Calculator : IEqualityComparer<Calculator>
{
public DashboardsComputationMode ComputationMode { get; set; }
public Modes Mode { get; set; }
public Calculator(DashboardsComputationMode dashboardsComputationMode, Modes mode)
{
ComputationMode = dashboardsComputationMode;
Mode = mode;
}
public bool Equals(Calculator x, Calculator y)
{
return (x.ComputationMode.Equals(y.ComputationMode) && x.Mode.Equals(y.Mode));
}
public int GetHashCode(Calculator obj)
{
return obj.ComputationMode.GetHashCode() ^ obj.Mode.GetHashCode();
}
}
public enum DashboardsComputationMode
{
Weighted = 0,
Aggregated = 1,
PR = 2,
CurrentValue = 3,
EquivalentHours = 4,
AggregatedCorrected = …
Run Code Online (Sandbox Code Playgroud) 我是C#和ASP.NET MVC Razor的新手.如果字段不为空,我想在视图中显示一个字段.
<tr class="hide" id="trPhone2">
<td class="editor-label">
@Html.LabelFor(model => model.phone2)
</td>
<td>
@Html.EditorFor(model => model.phone2)
</td>
<td>
@Html.ValidationMessageFor(model => model.phone2)
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
现在,我想输出第一<tr>
行,如果model.phone2 is ""
和输出:
<tr id="trPhone2">
Run Code Online (Sandbox Code Playgroud)
我如何使用ASP.NET MVC Razor做到这一点?
我设置了一个svn存储库,想知道我应该把dll文件放在哪里.
我目前所做的是将它们放在/ bin/debug文件夹中,然后将它们链接到visual studio中的项目文件中.
这是这样做的方法吗?
这不是我的代码.我把这个代码从这个网站上删除了:
http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/linklist.html
我正在使用有关如何构建链表的参考资料.我对发生的事情感到有些困惑.有人可以向我解释发生了什么事.我会用1-5来标记令我困惑的事情.
#include<stdlib.h>
#include<stdio.h>
struct list_el {
int val;
struct list_el * next;
};
typedef struct list_el item;
void main() {
item * curr, * head;
int i;
head = NULL; //1
for(i=1;i<=10;i++) {
curr = (item *)malloc(sizeof(item));
curr->val = i;
curr->next = head; //2
head = curr; //3
}
curr = head; // 4
while(curr) { //5
printf("%d\n", curr->val);
curr = curr->next ;
}
Run Code Online (Sandbox Code Playgroud)
head = NULL→为什么head被设置为NULL?我知道你应该(我是出于习惯),但我不知道为什么.
curr-> next = head→我也从未真正理解过这一点.也许我对"head"的定义错了,但是在常规链表中,它是列表中的起始节点还是最后一个节点?我一直认为它是起始节点,但在这一行中它看起来像是最后一个节点.
head = curr→为什么我们将它设置为curr?
curr = head→然后在循环完成后设置curr = …
我使用Entity Framework 4.2并希望调用具有输入参数的存储过程.我正在使用Database.ExecuteSqlCommand
调用存储过程.
但是,为了正确映射参数,文档缺少正确的调用语法.我的google-foo让我失望,任何帮助都将受到赞赏.
即我有一个程序
procedure SetElementFrequency
@ElementTypeID integer,
@Frequency float
as ...
Run Code Online (Sandbox Code Playgroud)
我试过用它来试试
Database.ExecuteSqlCommand("exec SetElementFrequency @p0 @p1",
elementType, frequency);
Run Code Online (Sandbox Code Playgroud)
和
Database.ExecuteSqlCommand("exec SetElementFrequency {0} {1}",
elementType, frequency);
Run Code Online (Sandbox Code Playgroud)
但他们都失败了错误语法'@ p1'附近的错误语法.
c# ×5
.net ×1
asp.net-mvc ×1
c ×1
c#-4.0 ×1
covariance ×1
foreach ×1
generics ×1
head ×1
ienumerable ×1
inheritance ×1
interface ×1
linked-list ×1
list ×1
razor ×1
sql ×1
sql-server ×1
types ×1
typescript ×1