小错误- [英] Small error -
问题描述
错误1预期的类,委托,枚举,接口或结构Program.cs 20 12
错误无效"
|
\/
静态void ProcessAndDisplayNumber(DoubleOp操作,双精度值)
{
Console.WriteLine("\ nProcessAndDisplayNumber调用,值=" +值);
动作(值);
}
课程计划
{
静态void Main(string [] args)
{
DoubleOp操作=新的DoubleOp(MathOperations.MultiplyByTwo);
操作+ =新的DoubleOp(MathOperations.Squre);
ProcessAndDisplayNumber(operations,2.0);
ProcessAndDisplayNumber(operations,7.94);
ProcessAndDisplayNumber(operations,1.414);
Console.WriteLine();
}
}
Error 1 Expected class, delegate, enum, interface, or struct Program.cs 20 12
Error ''void''
|
\/
static void ProcessAndDisplayNumber(DoubleOp action, double value)
{
Console.WriteLine("\nProcessAndDisplayNumber called with value = " + value);
action(value);
}
class Program
{
static void Main(string[] args)
{
DoubleOp operations = new DoubleOp(MathOperations.MultiplyByTwo);
operations += new DoubleOp(MathOperations.Squre);
ProcessAndDisplayNumber(operations, 2.0);
ProcessAndDisplayNumber(operations, 7.94);
ProcessAndDisplayNumber(operations, 1.414);
Console.WriteLine();
}
}
推荐答案
将代码放入类struct ...,
例如
Put the code inside class, struct ...,
for example
class mySuperClassName
{
}
static void ProcessAndDisplayNumber(DoubleOp action, double value)
{
Console.WriteLine("\nProcessAndDisplayNumber called with value = " + value);
action(value);
}
必须移到您的
侧
Has to be moved in side your
class Program
{
...
}
可以将Program的类更改为静态的Program
Can change class Program to static class Program
您已经修改并破坏了Wrox出版的《 Professional C#2008》一书中的示例代码.为什么不回到原始位置查看做错了什么?
可在此处示例代码下载 [ ^ ]
为方便起见,这里是您破坏的代码.
You have modified and broken sample code from the book Professional C# 2008 published by Wrox. Why don''t you just go back to the original to see what you have done wrong?
It available here Sample code download[^]
For your convenience here is the code that you have broken.
using System;
using System.Collections.Generic;
using System.Text;
namespace Wrox.ProCSharp.Delegates
{
class MathOperations
{
public static double MultiplyByTwo(double value)
{
return value * 2;
}
public static double Square(double value)
{
return value * value;
}
}
delegate double DoubleOp(double x);
class MainEntryPoint
{
static void Main()
{
DoubleOp[] operations =
{
MathOperations.MultiplyByTwo,
MathOperations.Square
//new DoubleOp(MathOperations.MultiplyByTwo),
//new DoubleOp(MathOperations.Square)
};
for (int i = 0; i < operations.Length; i++)
{
Console.WriteLine("Using operations[{0}]:", i);
ProcessAndDisplayNumber(operations[i], 2.0);
ProcessAndDisplayNumber(operations[i], 7.94);
ProcessAndDisplayNumber(operations[i], 1.414);
Console.WriteLine();
}
}
static void ProcessAndDisplayNumber(DoubleOp action, double value)
{
double result = action(value);
Console.WriteLine(
"Value is {0}, result of operation is {1}", value, result);
}
}
}
艾伦.
Alan.
这篇关于小错误-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!