Is there any significant difference between
int variable1 = (int) variable2;
and
int variable1 = Convert.ToInt32 (variable2);
It seems like the extra typing and granularity of .ToInt32 vs. .ToInt64 etc.
is a bit of overkill for most situations compared to the very simple and
straightforward cast. Is the IL the same anyway, but the compiler has to do
a little more work in the first case to figure out the conversion to use? 5 6895
Daniel Billingsley <db**********@N O.durcon.SPAAMM .com> wrote: Is there any significant difference between
int variable1 = (int) variable2;
and
int variable1 = Convert.ToInt32 (variable2);
Absolutely. However, the exact difference will depend on the type of
variable2.
For instance, if variable2 has a value which is a string reference to
"23" then Convert.ToInt32 will parse the int. However, you can't *cast*
from a string to an int.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Ah, yes that would make sense.
In this particular case variable2 is an enum, which according to the
documentation is stored by default as an integer. It would seem the
casting/conversion is rather cosmetic (to satisfy the compiler strong
typing).
So, what about this specific case? Any difference between the two and why?
But more generally speaking, I was really thinking only about between number
types - conversions from strings are a whole nutter ball game in my opinion.
Does your answer still depend on the two numeric types (assuming for the
sake of this discussion we're talking only about widening conversions)?
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Daniel Billingsley <db**********@N O.durcon.SPAAMM .com> wrote: Is there any significant difference between
int variable1 = (int) variable2;
and
int variable1 = Convert.ToInt32 (variable2);
Absolutely. However, the exact difference will depend on the type of variable2.
For instance, if variable2 has a value which is a string reference to "23" then Convert.ToInt32 will parse the int. However, you can't *cast* from a string to an int.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
I would use (int) MyEnum. Most likely once compiled there will be no conversion required (assuming your enum is an Int32 already),
but the Convert.ToInt32 () will probably still call the ToInt32 function.
--
Michael Culley
"Daniel Billingsley" <db**********@N O.durcon.SPAAMM .com> wrote in message news:up******** ******@TK2MSFTN GP12.phx.gbl... Ah, yes that would make sense.
In this particular case variable2 is an enum, which according to the documentation is stored by default as an integer. It would seem the casting/conversion is rather cosmetic (to satisfy the compiler strong typing).
So, what about this specific case? Any difference between the two and why?
But more generally speaking, I was really thinking only about between number types - conversions from strings are a whole nutter ball game in my opinion. Does your answer still depend on the two numeric types (assuming for the sake of this discussion we're talking only about widening conversions)?
"Jon Skeet" <sk***@pobox.co m> wrote in message news:MP******** *************** *@msnews.micros oft.com... Daniel Billingsley <db**********@N O.durcon.SPAAMM .com> wrote: Is there any significant difference between
int variable1 = (int) variable2;
and
int variable1 = Convert.ToInt32 (variable2);
Absolutely. However, the exact difference will depend on the type of variable2.
For instance, if variable2 has a value which is a string reference to "23" then Convert.ToInt32 will parse the int. However, you can't *cast* from a string to an int.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
casting also seems fast. Have not tested Convert yet. But on my machine
doing 300 million casts took about 14 seconds in a loop.
--
William Stacey, DNS MVP
"Daniel Billingsley" <db**********@N O.durcon.SPAAMM .com> wrote in message
news:uV******** ******@TK2MSFTN GP11.phx.gbl... Is there any significant difference between
int variable1 = (int) variable2;
and
int variable1 = Convert.ToInt32 (variable2);
It seems like the extra typing and granularity of .ToInt32 vs. .ToInt64
etc. is a bit of overkill for most situations compared to the very simple and straightforward cast. Is the IL the same anyway, but the compiler has to
do a little more work in the first case to figure out the conversion to use?
Daniel Billingsley <db**********@N O.durcon.SPAAMM .com> wrote: Ah, yes that would make sense.
In this particular case variable2 is an enum, which according to the documentation is stored by default as an integer. It would seem the casting/conversion is rather cosmetic (to satisfy the compiler strong typing).
So, what about this specific case? Any difference between the two and why?
But more generally speaking, I was really thinking only about between number types - conversions from strings are a whole nutter ball game in my opinion. Does your answer still depend on the two numeric types (assuming for the sake of this discussion we're talking only about widening conversions)?
Between two numeric types, I'd always cast. There's still a difference
when it comes to overflow cases - unless you've got checking on, the
cast won't throw an exception, whereas the Convert call would.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Logan X via .NET 247 |
last post by:
It's official....Convert blows.
I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance would be identical.
I was 100% incorrect.
The code below produces the results:
CType Took: 0.2187528 seconds.
Convert Took: 12.187656 seconds.
|
by: yufufi |
last post by:
lets say we have a 'shape' class which doesn't implement IComparable
interface..
compiler doesn't give you error for the lines below..
shape b= new shape();
IComparable h;
h=(IComparable)b;
but it complains for the following lines
|
by: fabrice |
last post by:
Hello
I have a little problem with casting in a Datagrid Control. I'm using Option
Strict in my web application.
In a Template Column, I put in an ImageButton Control with a Command
Argument using Container.DataItem.
Différent expressions as
System.Convert.ToString(Container.DataItem("Column"))) bring an Error :
|
by: Ken Allen |
last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years.
One of the drawbacks with VB6 and earlier was the difficulty in casting a
'record' to a different 'shape' so one could perform different manipulations
on it. For example, I have a complex data structure, which I can represent
in a VB6 TYPE declaration, but I cannot easily convert that to a fixed
length array of unsigned bytes so that I could perform a checksum...
|
by: Greg |
last post by:
Not sure if this is best place for this problem, but here it is. I have a
project that is simply a C# class that interfaces with an IFilter. This is
so I can retreive the text from Word docs. I'm able to use this DLL without
any problems within my test windows app, but not within my windows service
(that's when I receive the casting exception).
Here's the code (sorry it's long):
VB Function within windows app and windows service:
| |
by: MikeY |
last post by:
Hi Everyone,
Is there a way (syntax) for converting/casting color so I can find the
System.Byte of my chosen color with in my button?
I have buttons colored ie "Color.Olive" and up to now I change the color
into another color. But what I want to do at run time is to find the
existing color say in FromArgb and just darken the existing color at run
time.
|
by: stunt016 |
last post by:
I have a program written in C# that handles communication between two
pieces of software. My problem only deals with getting a text array
from one program to this C# "Bridge". I can get the text array to the
bridge, where it is received as a system.object. My problem is casting
this system.object to a string array. The code I'm using is below.
System.Array advArgs = args as System.Array; //creates array for
parameters being...
|
by: Frederic Rentsch |
last post by:
Hi all,
If I derive a class from another one because I need a few extra
features, is there a way to promote the base class to the derived one
without having to make copies of all attributes?
class Derived (Base):
def __init__ (self, base_object):
# ( copy all attributes )
...
|
by: kanepart2 |
last post by:
Hi guys, I am having a problem with the following code snippet:-
double x = (myReader);
double y = (myReader);
Resulting in the follwing compilation error:
Cannot implicitly convert type 'object' to 'double'. An explicit
conversion exists (are you missing a cast?)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |