int.Parse(" 123")和Convert.ToInt32(" 456")之间的区别是什么? [英] what is difference between int.Parse("123") and Convert.ToInt32("456")?
问题描述
我知道int.Parse(123)将导致int为123,但是会发生什么情况
为空?我相信它会给出一个null异常(好像我得到了
NullArgumentException或ArgumentNullException如果我在一个
控制台应用程序或web应用程序中运行它, 那就是这个?。
我正试图从Application对象中获取一个counter(int)值。当
我第一次执行Convert.ToInt32(Application [" PageCounter"])时,它会将它转换为零(它是
null)太棒了但是当我使用
int.Parse(Application [" PageCounter"]并检查== null)时,它首次运行
,但不是第二次(当它有价值时)。嗯?
任何帮助都将不胜感激。谢谢。
I know the int.Parse("123") will result in an int of 123, but what happens
with a null? I believe it give a null exception (seems like I get either
NullArgumentException or ArgumentNullException if I''m running it in a
console app or in a web app, what''s up with that?).
I''m trying to get a counter (int) value out of the Application object. When
I do the Convert.ToInt32(Application["PageCounter"]), the first time (it''s
null), the method converts it to zero, great. But when I use the
int.Parse(Application["PageCounter"] and check for == null, it works the
first time, but not the second time (when it has a value). Huh?
Any help would be greatly appreciated. Thank you.
推荐答案
在Application.Start中将变量初始化为0.从那时起,它将是b / b
总是有一个整数值。你可以随时安全地检索它,并且知道
你将得到一个有效的整数。
"翻转< [remove_me] ph ****** @ hotmail.com>写在消息中
新闻:uU ************* @ TK2MSFTNGP14.phx.gbl ...
In Application.Start initialize the variable to 0. From then on, it will
always have an integer value. You can always safely retrieve it, and know
you will have a valid integer.
"Flip" <[remove_me]ph******@hotmail.com> wrote in message
news:uU*************@TK2MSFTNGP14.phx.gbl...
我知道int.Parse(" 123")会导致123的int,但是什么会发生什么呢?我相信它会给出一个null异常(如果我在一个
控制台应用程序中运行它,我似乎得到了NullArgumentException或ArgumentNullException)或者在网络应用程序中,该怎么办?)。
我正在尝试从Application对象中获取一个counter(int)值。
当我执行Convert.ToInt32(Application [" PageCounter"])时,第一次
(它为空),该方法将其转换为零,非常好。但是当我使用
int.Parse(Application [" PageCounter"]并检查== null)时,它首次运行
,但不是第二次运行(当它有值时)。嗯?
非常感谢任何帮助。谢谢。
I know the int.Parse("123") will result in an int of 123, but what happens
with a null? I believe it give a null exception (seems like I get either
NullArgumentException or ArgumentNullException if I''m running it in a
console app or in a web app, what''s up with that?).
I''m trying to get a counter (int) value out of the Application object.
When I do the Convert.ToInt32(Application["PageCounter"]), the first time
(it''s null), the method converts it to zero, great. But when I use the
int.Parse(Application["PageCounter"] and check for == null, it works the
first time, but not the second time (when it has a value). Huh?
Any help would be greatly appreciated. Thank you.
插入(如果你没有)一个global.asax文件,在
Application_Start方法中将它设置为0,这样你就可以确定它了
永远存在。
奇怪的是Int32.Parse无论如何都应该给出相同的,我打赌你
在某处有另一个错误。
不知道你在做什么但是应用程序将从任何线程访问
(请求)所以你应该特别注意并发问题。
欢呼,
-
Ignacio Machin,
ignacio.machin at dot.state.fl.us
佛罗里达州交通局
翻转< [remove_me] pH值****** @ hotmail.com>在消息中写道
news:uU ************* @ TK2MSFTNGP14.phx.gbl ...
Hi,
Insert ( if you do not have it ) a global.asax file, the in the
Application_Start method set it to 0, in this way you will be sure that it
always exist.
Weird that of Int32.Parse it should give the same no matter what, I bet you
have another error somewhere.
No idea what you are doing but Application will be accessed from any thread
( request ) so you should keep especial attention to concurrency issues.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Flip" <[remove_me]ph******@hotmail.com> wrote in message
news:uU*************@TK2MSFTNGP14.phx.gbl...
我知道int.Parse (123)将导致123的int,但是什么会发生?我相信它会给出一个null异常(好像我得到了NullArgumentException或ArgumentNullException,如果我在一个
控制台应用程序或Web应用程序中运行它,那有什么用?) 。
我正在尝试从Application对象中获取一个counter(int)值。
当我执行Convert.ToInt32(Application [" PageCounter"])时,第一次
(它为空),该方法将其转换为零,很棒。但是当我使用
int.Parse(Application [" PageCounter"]并检查== null)时,它首次运行
,但不是第二次运行(当它有值时)。嗯?
非常感谢任何帮助。谢谢。
I know the int.Parse("123") will result in an int of 123, but what happens
with a null? I believe it give a null exception (seems like I get either
NullArgumentException or ArgumentNullException if I''m running it in a
console app or in a web app, what''s up with that?).
I''m trying to get a counter (int) value out of the Application object.
When I do the Convert.ToInt32(Application["PageCounter"]), the first time
(it''s null), the method converts it to zero, great. But when I use the
int.Parse(Application["PageCounter"] and check for == null, it works the
first time, but not the second time (when it has a value). Huh?
Any help would be greatly appreciated. Thank you.
>我知道int.Parse(" 123")会产生123的int,但是会发生什么?
> I know the int.Parse("123") will result in an int of 123, but what happens
带有null?
为什么你不测试它? 。
//在void Main()...
尝试{
int i = Int32。 Parse(-1);
Console.WriteLine(Parse没有抛出; i = {0},i);
}
catch(exception ex)
{
Console.WriteLine(" Parse threw,message = {0}",ex.Message);
}
" Flip"写道:
我知道int.Parse(" 123")将导致int为123但是,使用null会发生什么?我相信它会给出一个空的例外如果我在一个
控制台应用程序或网络应用程序中运行它,那么看起来我得到了NullArgumentException或ArgumentNullException,那是什么原因?)
<我正在尝试从Application对象中获取一个counter(int)值。当我执行Convert.ToInt32(Application [" PageCounter"])时,第一次(它是
null),该方法将其转换为零,很棒。但是当我使用
int.Parse(Application [" PageCounter"]并检查== null)时,它首次运行
,但不是第二次运行(当它有值时)。嗯?
非常感谢任何帮助。谢谢。
with a null?
Why don''t you just test it? ...
// In void Main() ...
try {
int i = Int32.Parse("-1");
Console.WriteLine("Parse did not throw; i = {0}", i);
}
catch(Exception ex)
{
Console.WriteLine("Parse threw, message = {0}", ex.Message);
}
"Flip" wrote:
I know the int.Parse("123") will result in an int of 123, but what happens
with a null? I believe it give a null exception (seems like I get either
NullArgumentException or ArgumentNullException if I''m running it in a
console app or in a web app, what''s up with that?).
I''m trying to get a counter (int) value out of the Application object. When
I do the Convert.ToInt32(Application["PageCounter"]), the first time (it''s
null), the method converts it to zero, great. But when I use the
int.Parse(Application["PageCounter"] and check for == null, it works the
first time, but not the second time (when it has a value). Huh?
Any help would be greatly appreciated. Thank you.
这篇关于int.Parse(" 123")和Convert.ToInt32(" 456")之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!