我有一个.cs充满C#代码的文件,我一直在多个项目中重复使用.
现在我通过将代码复制并粘贴到每个项目目录中的新文件中来将它包含在这些项目中.这是错误的做法.
什么是正确的方法呢?
正确的方法应该:
随机猜测,我期待某种指令using mycode = C:\common\mycode.cs,但我确信这不是.NET的做事方式.
(我正在使用C#2010,.NET 4.0,并且只在一台计算机上本地编译此代码.)
我试图在C#中重载一个运算符(不要问为什么!)Lists.例如,我希望能够写:
List<string> x = // some list of things
List<string> y = // some list of things
List<string> z = x + y
Run Code Online (Sandbox Code Playgroud)
所以'z'包含'x'的所有内容,后跟'y'的内容.我知道已经有办法组合两个列表,我只是想了解运算符重载如何与泛型结构一起工作.
(顺便说一下,这是List班级Systems.Collections.Generic.)
我正在尝试编写一个函数,它将函数作为其参数之一 - 我以前做过很多次的任务.这很好用:
int RunFunction(Func<int,int> f, int input) {
return f(input);
}
int Double(int x) {
return x*2;
}
// somewhere else in code
RunFunction(Double,5);
Run Code Online (Sandbox Code Playgroud)
然而,这不起作用:
public static class FunctionyStuff {
public static int RunFunction(this Func<int,int> f, int input) {
return f(input);
}
}
// somewhere else in code
Double.RunFunction(5);
Run Code Online (Sandbox Code Playgroud)
知道为什么第一个有效,第二个没有?
当我创建一个附有宏的按钮,并将鼠标光标悬停在按钮上时,它会在默认指向手形光标和看起来像是一个全白指针的手之间闪烁,该手指还有1个像素.
任何想法如何解决这一问题?
我有一个MyCollection<T>我已经制作的通用集合,除了Apply我正在添加的这个新功能外,一切正常:
class MyCollection<T> {
T value;
public MyCollection(T starter) { value = starter; }
public MyCollection<S> Apply<T, S>(Func<T, S> function) {
return new MyCollection<S>(function(value)); // error in function(value)
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个我以前从未见过的错误:
Argument 1: cannot convert from 'T' to 'T [C:\folder\code.cs (line number)]'
Run Code Online (Sandbox Code Playgroud)
这两种T类型是什么?我正在尝试的转换有什么问题?
据我所知,C#有一种编写数组的语法:{ 1, 2, 3 }.为什么这个无效:
x = { 1, 2, 3 }.GetLength(0);
Run Code Online (Sandbox Code Playgroud)
虽然这是有效的?
int[] numbers = { 1, 2, 3 };
x = numbers.GetLength(0);
Run Code Online (Sandbox Code Playgroud)
表达式的数据类型不{ 1, 2, 3 }一样numbers吗?
如何.Properties.Resources在控制台应用程序中获得访问权限?这是使用附加到解决方案的资源文件.
这正是我能看到的:

第一个语法错误不是我关注的那个(只有赋值,调用,递增,递减和新对象表达式可以用作语句).
我想要修复的是第二个:当前上下文中不存在名称"属性"
假设我有一套车,每辆车都有一个方向盘.我想编写一行代码,在集合中查找汽车并返回其方向盘,如果汽车不在集合中,则返回null.像这样的东西:
Car found = // either a Car or null
SteeringWheel wheel = (found == null ? null : found.steeringwheel);
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点不使用found,并null在表达两次吗?我不喜欢这里重复的气味.
我正在尝试使用这样的代码,但它失败了:
data = Range("A1")
MsgBox data.Offset(1,1)
Run Code Online (Sandbox Code Playgroud)
这似乎应该打印单元格B2的值,但它给了我一个错误(Run-time error 424: Object required.).
那么表达式Range("A1")返回什么数据类型,以及如何声明data存储它的正确数据类型?
VBA有一个Select声明,它根据一个值选择要运行的代码(很像switchC风格的语言).此语句的语法如下所示:
Select Case x
Case 1
'do one thing
Case 2
'do a different thing
End Select
Run Code Online (Sandbox Code Playgroud)
Case之后的目的是什么Select?或者,是什么意思Select不Case?
我有一个叫做的课Animal.对于任何给定的动物a,a.number_of_legs应该是4.
我有一个叫做Human继承自的类Animal.对于任何给定的人h,h.number_of_legs应该是2.
我该如何设置number_of_legs房产?
这是我到目前为止所拥有的:
class Animal {
int number_of_legs = 4;
}
class Human : Animal {
int number_of_legs = 2;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我采取一些任意动物,并询问它有多少腿,答案总是2:
Animal x = new Animal();
Animal y = new Human();
x.number_of_legs // --> 4
y.number_of_legs // --> 4
Run Code Online (Sandbox Code Playgroud)
我知道这个新Human的被视为一个Animal因为变量y存储了一个Animal.
如何设置number_of_legs属性,使其x.number_of_legs为4且y.number_of_legs为2?
你怎么能从它自己调用一个Func函数?
例如:
Func<int, int> f = x => {
// do stuff
if (x > 5) { return f(x); }
// do other stuff
};
Run Code Online (Sandbox Code Playgroud) 在 C# 中定义构造函数时,可以使用冒号(如本例所示):
public class Custom : OtherClass {
public Custom(string s) : base(s) { }
}
Run Code Online (Sandbox Code Playgroud)
这个冒号叫什么? (在不知道它叫什么的情况下阅读它是非常困难的。)
请注意,我问的是构造函数定义中的冒号,而不是类定义中显示继承的冒号。
c# ×10
excel ×2
vba ×2
arrays ×1
casting ×1
code-reuse ×1
collections ×1
delegates ×1
idiomatic ×1
inheritance ×1
recursion ×1
syntax ×1
terminology ×1