小编Joh*_*Tan的帖子

在参数 c# 中使用 this 关键字

我有一堂课:

public static class PictureBoxExtensions
{
    public static Point ToCartesian(this PictureBox box, Point p)
    {
        return new Point(p.X, p.Y - box.Height);
    }

    public static Point FromCartesian(this PictureBox box, Point p)
    {
        return new Point(p.X, box.Height - p.Y);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是this前面的关键字有什么用PictureBox,而不是省略关键字?

c# this

5
推荐指数
2
解决办法
6168
查看次数

返回满足特定条件的列表元素

我有一节课:

class Point
{
    double X, Y;
}
Run Code Online (Sandbox Code Playgroud)

List<Point>,说我想要的Point地方Point.X + Point.Y是在列表中最高.我如何在LINQ中执行此操作?

c# linq

5
推荐指数
1
解决办法
80
查看次数

将函数作为函数参数传递

我正在尝试将c#中的trapezodial规则作为函数实现:

Int_a^b f(x) = (b-a) * [f(a) + f(b)] / 2 
Run Code Online (Sandbox Code Playgroud)

c#中是否有一个功能允许我编写这样的功能?

double integrate(double b, double a, function f)
{
    return (b-a) * (f(a) + f(b)) / 2;
}
Run Code Online (Sandbox Code Playgroud)

哪里f可以是在另一个函数内定义的任何多项式表达式,例如:

double f (double x)
{
    return x*x + 2*x;
}
Run Code Online (Sandbox Code Playgroud)

c#

4
推荐指数
1
解决办法
1230
查看次数

动态引用对象

说我有两个对象ObjAObjB.假设它们来自同一个类,并且调用了一个方法CommonMethod(),我正在寻找一种方法来做这样的事情:

void CallObjectMethod(string name)
{
    // where name could be 'A' or 'B'
    (Obj + name).CommonMethod();
}
Run Code Online (Sandbox Code Playgroud)

而不是做很长的路:

void CallObjectMethod(string name)
{
    if(name == 'A')
        objA.CommonMethod();
    else if(name == 'B')
        objB.CommonMethod();
}
Run Code Online (Sandbox Code Playgroud)

我明白这可能是通过反思来完成的,但不太确定如何实现这一点.

c# reflection

4
推荐指数
1
解决办法
54
查看次数

使用cmake在mac上生成visual studio解决方案

使用CMake GUI,对于相同的CMakeList,我能够在Windows上生成VS解决方案文件,在Mac上生成XCode解决方案文件.但是我无法在Mac上生成VS解决方案,因为我没有使用任何VS选项Specify the generator for this project.

我在Macbook上安装了VS 2017 Community for Mac.有什么我想念的吗?

macos cmake visual-studio

4
推荐指数
1
解决办法
1450
查看次数

根据类中的值从最小到最大排序

我有一节课:

Class A
{
    List<double> count;
    List<Parts> parts;
}
Run Code Online (Sandbox Code Playgroud)

其中countparts重合(意味着count [i]对应于parts [i],等等).

当我count使用以下方法从最小到最大排序:

count.Sort();
Run Code Online (Sandbox Code Playgroud)

我如何重新安排我的parts列表count[i]仍然对应parts[i]

c# sorting

2
推荐指数
1
解决办法
1780
查看次数

覆盖具有相同名称但属性不同的属性

我想知道是否有一种方法来覆盖派生类中具有不同类型的同名变量.有点像这样的东西(虽然这段代码不会编译):

public class A
{
    public virtual Point2D Point; // Represent a 2D point
}

public class B : A
{
    public override Point3D Point; // Represent a 3D point
}
Run Code Online (Sandbox Code Playgroud)

这样做的原因是,A并且B可能共享相似的属性,只是它的维度Point不同.

c# inheritance

1
推荐指数
2
解决办法
929
查看次数

使用派生的属性实现接口

如果我有话

interface ISomething
{
    Shape Entity { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

public class Shape
{ }

public class Circle : Shape
{ }

public class Square : Shape
{ }
Run Code Online (Sandbox Code Playgroud)

我怎样才能达到以下效果:

public class CircleEntity : ISomething
{
    Circle Entity { get; set; }
}

public class SquareEntity: ISomething
{
    Square Entity { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

由于CircleEntity并且SquareEntity没有完全实现Entity类型Shape.

c# interface

1
推荐指数
1
解决办法
43
查看次数

排序,然后在 C++ 中使用 lambda 函数

在 C# 中给出一个结构:

struct Point {int x, int y}
Run Code Online (Sandbox Code Playgroud)

我们可以这样写:

List<Point> list;
list.OrderBy(p => p.x).ThenBy(q => q.y);
Run Code Online (Sandbox Code Playgroud)

如何使用 lambda 函数在 C++ 中表达此逻辑?

c++ lambda

1
推荐指数
1
解决办法
3175
查看次数

获取满足条件的List中元素的索引

说我有一个Point:

public class Point
{
    double X, Y;
}
Run Code Online (Sandbox Code Playgroud)

我想得到List<Point>满足条件的元素索引,例如,Point.X里面有最大值List<Point>.

我如何使用LINQ表达式执行此操作?

c# linq

0
推荐指数
1
解决办法
515
查看次数

标签 统计

c# ×8

linq ×2

c++ ×1

cmake ×1

inheritance ×1

interface ×1

lambda ×1

macos ×1

reflection ×1

sorting ×1

this ×1

visual-studio ×1