int.Parse(...) Question

M

Matt

I want to convert a string to an integer, I tried the following 2 methods
and they both worked. But I dont understand in method 1: int.Parse(...),
"int" should be the primitive type, but it can operate a method Parse?

int t1 = int.Parse(TextBox2.Text); //method 1
int t2 = System.Int32.Parse(TextBox2.Text); //method 2

Another question is which approach is correct? When to use which?

Please help! Thanks!
 
F

Frank Oquendo

Matt said:
I want to convert a string to an integer, I tried the following 2 methods
and they both worked. But I dont understand in method 1: int.Parse(...),
"int" should be the primitive type, but it can operate a method Parse?

int t1 = int.Parse(TextBox2.Text); //method 1
int t2 = System.Int32.Parse(TextBox2.Text); //method 2

Another question is which approach is correct? When to use which?

They're identical as int is a C# alias for System.Int32.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
J

José Joye

If I remember it correctly, int is a C# alias for the Int32 System type

So they are the same...

José
 
L

Lawrence Oluyede

Matt said:
I want to convert a string to an integer, I tried the following 2 methods
and they both worked. But I dont understand in method 1: int.Parse(...),
"int" should be the primitive type, but it can operate a method Parse?

int is not a "primitive" type (primitive-a-la-Java), it's the C#
"implementation" of the CLS System.Int32 type. So you can use it in place
of Int32, but I've read on MSDN that for interoperability using CLS types
is better.
int t1 = int.Parse(TextBox2.Text); //method 1
int t2 = System.Int32.Parse(TextBox2.Text); //method 2

Another question is which approach is correct? When to use which?

I use Int32.Parse() in my code but int.Parse() is correct too.

Bye!
 
J

Jon Jagger

They are the same. In C# int is an alias for System.Int32. And since you're
programming in C# and not CIL I'd recommend using int. The only time you
have to use System.Int32 is in reflection. Eg Type.GetType("System.Int32");
HTH
Cheers
Jon Jagger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top