我想使用Linq查询2D数组,但是我收到一个错误:
找不到源类型'SimpleGame.ILandscape [ , ]' 的查询模式的实现.找不到"选择".您是否缺少对'System.Core.dll'的引用或'System.Linq'的using指令?
代码如下:
var doors = from landscape in this.map select landscape;
Run Code Online (Sandbox Code Playgroud)
我已经检查过我包含的参考System.Core和使用System.Linq.
谁能给出一些可能的原因?
using System;
using System.Collections.Generic;
class Parent
{
public Child Child { get; set; }
}
class Child
{
public List<string> Strings { get; set; }
}
static class Program
{
static void Main() {
// bad object initialization
var parent = new Parent() {
Child = {
Strings = { "hello", "world" }
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
上面的程序编译很好,但在运行时崩溃,而Object引用没有设置为对象的实例.
如果您在上面的代码段中注意到,我在初始化子属性时省略了new.
显然,正确的初始化方法是:
var parent = new Parent() {
Child = new Child() {
Strings = new List<string> { …Run Code Online (Sandbox Code Playgroud) 我在c#中偶然发现了这个正则表达式我想移植到javascript,我不明白以下内容:
[-.\p{Lu}\p{Ll}0-9]+
Run Code Online (Sandbox Code Playgroud)
当然,我遇到困难的部分\p{Lu}.我访问过的所有正则表达式网站都没有提到过这个修饰符.
任何的想法?
有人可以解释同一类上的相同函数的行为有何不同吗?
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
MyUpperScript mAsdfScript = new MyUpperScript();
IInterface mAsdfInterface = mAsdfScript;
mAsdfScript.Foo();
mAsdfInterface.Foo();
}
}
public class MyUpperScript : MyScript
{
public void Foo()
{
Console.WriteLine("Upper Script");
}
}
public class MyScript : IInterface
{
}
public interface IInterface
{
public void Foo()
{
Console.WriteLine("Interface");
}
}
Run Code Online (Sandbox Code Playgroud)
我期望它在 Foo() 调用上都写“Upper Script”
请注意,如果我也从接口派生第二个类,它会按照我期望的方式工作 - 都打印“Upper Script”:
// now deriving for the interface too:
public class MyUpperScript : MyScript, IInterface
{ …Run Code Online (Sandbox Code Playgroud) c# inheritance overriding interface default-interface-member
如何将字符串值"0x310530"转换为C#中的整数值?
我已经尝试了int.TryParse(甚至int.TryParse与System.Globalization.NumberStyles.Any),但它不起作用.
更新: 似乎Convert.ToInt64或Convert.ToInt32工作而不必删除尾随的"0x":
long hwnd = Convert.ToInt64("0x310530", 16);
Run Code Online (Sandbox Code Playgroud)
文件Convert.ToInt64 Method (String, Int32)说:
"如果fromBase为16,则可以将value参数指定的数字加上"0x"或"0X"前缀."
但是,我更喜欢像TryParse这样不会引发异常的方法.
如何@"Sch\u00f6nen"在C#中解码此字符串'Sch\u00f6nen'(),我尝试了HttpUtility,但它没有给我我需要的结果,即"Schönen".
我实际上是在尝试将多个项目添加到列表中,但最后所有项目都具有与最后一项相同的值.
public class Tag
{
public string TagName { get; set; }
}
List<Tag> tags = new List<Tag>();
Tag _tag = new Tag();
string[] tagList = new[]{"Foo", "Bar"};
foreach (string t in tagList)
{
_tag.tagName = t; // set all properties
//Add class to collection, this is where all previously added rows are overwritten
tags.Add(_tag);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码生成两个项目的列表,TagName当我期望一个"Foo"和一个时,设置为"Bar" "Bar".为什么所有项目在结果列表中具有相同的属性?
解释为什么更改public class Tag以public struct Tag使此代码按预期工作(不同的项具有不同的值)的加分点.
如果重要的是我的实际目标是创建派生集合类,但由于问题只发生在列表中,它可能是可选的,仍然显示我的目标在下面.
按照一些教程,我可以成功创建一个集合类,它继承了创建DataTable所需的功能,可以将其作为表值参数传递给Sql Server的存储过程.一切似乎都运作良好; 我可以添加所有行,它看起来很漂亮.但是,经过仔细检查,我注意到当我添加一个新行时,所有前一行的数据都会被新行的值覆盖.因此,如果我有一个字符串值为"foo"的行,并且我添加了第二行,其值为"bar",则将插入第二行(使用两行的DataTable),但这两行的值都为"bar" ".任何人都可以看到为什么会这样?下面是一些代码,它们可以工作但是有点简化(Tag类已经减少了以便于解释).
以下是Collection类:
using …Run Code Online (Sandbox Code Playgroud) 我有2个班:
public class Articles
{
private string name;
public Articles(string name)
{
this.name = name;
}
public void Output()
{
Console.WriteLine("The class is: " + this.GetType());
Console.WriteLine("The name is: " + name);
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class Questionnaire
{
private string name;
public Questionnaire(string name)
{
this.name = name;
}
public void Output()
{
Console.WriteLine("The class is: " + this.GetType());
Console.WriteLine("The name is: " + name);
}
}
Run Code Online (Sandbox Code Playgroud)
我想写一个方法,它取一个整数(1表示Articles应该返回,2表示意思Questionnaire)和一个名字.
此方法必须返回这两个类之一的实例:
public [What type??] Choose(int x, string …Run Code Online (Sandbox Code Playgroud) 我有一个代码片段来修改.在那里我发现了这样的语法.
Session("LightBoxID")?.ToString()
Run Code Online (Sandbox Code Playgroud)
我不明白那个问号(?)是什么意思.没有谷歌搜索帮助我任何提示
我在变量字符串中定义了大写字母,我想输出字母表中的下一个和前一个字母.例如,如果变量等于'C',我想输出'B'和'D'.
c# ×9
.net ×1
alphabet ×1
asp.net ×1
duck-typing ×1
hex ×1
inheritance ×1
integer ×1
interface ×1
java ×1
javascript ×1
linq ×1
oop ×1
overriding ×1
regex ×1
string ×1
variables ×1
vb.net ×1