我不理解以下代码中的执行顺序。在这里,满足第一个Where子句的数字是(4、10、3、7),满足第二个Where子句的数字是2和1,之后我们具有Aggregate将它们相减并从两个元素中构成一个元素的功能。
我的问题是,这里的执行流程是什么-(1)Where对所有元素使用c / 3> 0执行,然后(2)Where或(1)对一个元素执行第一个子句并将其传递给(2)并从那里汇总-当我打印值时,使用两种方法我都无法在纸上获得x的值为28,也无法调试linq语句。感谢您的任何帮助。
var ints = new int[] { 2, 4, 1, 10, 3, 7 };
var x = ints
.Where(c => c / 3 > 0) <-- (1)
.Select(s2 => s2 + ints
.Where(c => c / 3 == 0) <-- (2)
.Aggregate((f, s) => f - s))
.Sum();
Run Code Online (Sandbox Code Playgroud) 在上面的代码段我有一个基类形状,并从它两个派生的类Rectangle和Triangle。我实例化了它们,但是对于一个Triangle对象,我使用其基类的引用类型。
因此,现在当我调用一个方法时calculate(),它将更喜欢调用采用基类参数的方法。
这样做的目的是什么?
我创建的Triangle对象不是Shape对象,我只是使用基类的引用。我的另一个问题是,与使用基类的引用和从派生实例化对象而不是使用派生引用相比,它们还有其他区别吗?
public static void Main(string[] args)
{
Point tc = new Point(3, 4);
Point rc = new Point(7, 5);
Shape shape = new Triangle(tc, 4, 4, 4);
calculate(shape);
}
public static void calculate(Shape shape) <<-- new Triangle() with base class ref will came here.
{
Console.WriteLine("shape");
}
public static void calculate(Rectangle rectangle)
{
Console.WriteLine("rectangle");
}
public static void calculate(Triangle triangle) <<-- new Triangle() using triangle …Run Code Online (Sandbox Code Playgroud)