为何总说:使用了未赋值的局部变量“process”,兄弟刚学C#,请多指点!using System;namespace ConsoleApplication7
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
delegate double processDelegate(double param1,double param2);
static double Multiply(double param1,double param2)
{
return param1*param2;
}
static double Divide(double param1,double param2)
{
return param1/param2;
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
processDelegate process;
Console.WriteLine("Enter 2 numbers separated with a comma:");
string input = Console.ReadLine ();
int commaPos = input.IndexOf(',');
double param1=Convert.ToDouble(input.Substring(0,commaPos));
double param2=Convert.ToDouble(input.Substring(commaPos+1,input.Length-commaPos-1));
Console.WriteLine ("Enter M to multiply or D to divide:");
do
{
input=Console.ReadLine ();
if(input=="M")
process=new processDelegate(Multiply);
if (input=="D")
process=new processDelegate(Divide);
else
Console.WriteLine("Please reinput:");
}while((input!="M")||(input!="D"));
Console.WriteLine("Result:{0}",process(param1,param2));
}
}
}
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
delegate double processDelegate(double param1,double param2);
static double Multiply(double param1,double param2)
{
return param1*param2;
}
static double Divide(double param1,double param2)
{
return param1/param2;
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
processDelegate process;
Console.WriteLine("Enter 2 numbers separated with a comma:");
string input = Console.ReadLine ();
int commaPos = input.IndexOf(',');
double param1=Convert.ToDouble(input.Substring(0,commaPos));
double param2=Convert.ToDouble(input.Substring(commaPos+1,input.Length-commaPos-1));
Console.WriteLine ("Enter M to multiply or D to divide:");
do
{
input=Console.ReadLine ();
if(input=="M")
process=new processDelegate(Multiply);
if (input=="D")
process=new processDelegate(Divide);
else
Console.WriteLine("Please reinput:");
}while((input!="M")||(input!="D"));
Console.WriteLine("Result:{0}",process(param1,param2));
}
}
}
解决方案 »
- 用户权限问题!
- 一个循环求和的问题
- List <bullet> blist=new List <bullet>();与public ArrayList bList=new ArrayList();那个效率高,功能上等效吗?
- like '%[2008]%'报错,说不可识别字符串,怎么解决阿
- c#控制台程序无法使用using System.Web.Mail ;
- 高手请进,我想用C#调用delphi的dll文件可以吗
- 自己做的MediaPlayer全屏播放后后就监听不到键盘了
- 排分!不止100分……有多少送多少。
- 我发现了一个GNU的C#的编译器,SharpDevelop.exe,但不能用好像我缺一个名为mscoree.dll的东东
- 为什么读取dataset中值时会出错,提示indexOutOfRangeExpetion
- 请教各位大哥,帮我看看下面两个问题,谢谢!
- 请<中间件技术>课件
processDelegate process = null;
不过,你说到这里,我也想请问一下各位一个问题,也请楼主稍候在揭帖。do
{
input=Console.ReadLine ();
if(input=="M")
process=new processDelegate(Multiply);
if (input=="D")
process=new processDelegate(Divide);
else
Console.WriteLine("Please reinput:");
}while((input!="M")||(input!="D"));
他这里,使用了这么多的new,没有任何释放的代码,.net运行时真的会回收以上资源?(原谅我的无知吧,请回答一下。)
private int myMethod()
{
..... if (....)
return 2; if (....)
return 3;
}
在这种情况下,编译器会告诉你“并不是所有的路径都返回值”(大概是这样的意思,原句不记得了)
========================================================================他这里,使用了这么多的new,没有任何释放的代码,.net运行时真的会回收以上资源?(原谅我的无知吧,请回答一下。)
=================================================
这里只有一个new被执行了,所以并没有占用过多的资源。
.net 当然会自动回收不用的资源,难道这一点您还有质疑吗?
{
delegate double processDelegate(double param1,double param2);改为:
// delegate declarationdelegate double processDelegate(double param1,double param2);class Class1
{不晓得是不是这个问题
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/csref/html/vcrefTheDelegateType.htm
private int myMethod()
{
..... if (....)
return 2; if (....)
return 3;
}
在这种情况下,编译器会告诉你“并不是所有的路径都返回值”(大概是这样的意思,原句不记得了)
========================================================================他这里,使用了这么多的new,没有任何释放的代码,.net运行时真的会回收以上资源?(原谅我的无知吧,请回答一下。)
=================================================
这里只有一个new被执行了,所以并没有占用过多的资源。
.net 当然会自动回收不用的资源,难道这一点您还有质疑吗?
=================================================
你的new是在循环里啊,并非你所认为的只有一个new被执行
processDelegate process = null;