473,508 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about type-conversion in property

Hi,

Using VS.NET 2003, the following code fails compilation (as expected):

-------------------------------------------
using System;
class App
{
static public int I { get { return 1.0f; } } // Cannot implicitly
convert type 'float' to 'int'
static void Main() { Console.WriteLine(App.I); }
}
-------------------------------------------

But the following code compiles without any error:

-------------------------------------------
using System;
class App
{
static public float F { get { return 1; } } // <----- Shouldn't this be
caught as an error instead?
static void Main() { Console.WriteLine(App.F); }
}
-------------------------------------------

What am I missing?

Thanks,
Simon
Nov 15 '05 #1
4 1184
Simon Cheng wrote:
What am I missing?


There's no threat of data loss when casting an int to a float. The reverse
is not true. Surely you're not actually using the posted example in
production code.

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

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
Frank Oquendo <fr****@acadxpin.com> wrote:
What am I missing?


There's no threat of data loss when casting an int to a float.


Actually, that's not strictly true. Not every int is exactly
representable as a float. (They can't be, as both types are 32 bit
types and there are floats which aren't representable as ints.)

For instance:

using System;

public class Test
{
static void Main()
{
int i = int.MaxValue/2;
float f = i;

Console.WriteLine (i);
Console.WriteLine ("{0:f}", f);
}
}

However, there is still an implicit conversion from int to float,
regardless of this potential loss of data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Jon Skeet [C# MVP] wrote:
Actually, that's not strictly true. Not every int is exactly
representable as a float. (They can't be, as both types are 32 bit
types and there are floats which aren't representable as ints.)
You lost me with that last line. Of course there are floats which are not
representable as ints. What were you trying to say?
int i = int.MaxValue/2;
float f = i;


The data loss here ocurred in the first line, not the second.

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

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #4
Frank Oquendo <fr****@acadxpin.com> wrote:
Jon Skeet [C# MVP] wrote:
Actually, that's not strictly true. Not every int is exactly
representable as a float. (They can't be, as both types are 32 bit
types and there are floats which aren't representable as ints.)


You lost me with that last line. Of course there are floats which are not
representable as ints. What were you trying to say?


Both int and float are 32 bit values, and all possible bit patterns
represent valid ints. There are bit patterns for float which are not
equivalent to ints, therefore there *must* be ints which cannot be
exactly represented as floats. There just aren't enough possible bit
patterns in float.
int i = int.MaxValue/2;
float f = i;


The data loss here ocurred in the first line, not the second.


Nope - I wasn't trying to get exact (mathematical) value of
int.MaxValue/2. I was merely taking the integer value of int.MaxValue/2
as an example of an int which isn't exactly representable as a float.
The value of f is not the same as the value of i, so data has been
lost. (You cannot get the original value of i from the value of f -
indeed, several nearby integers would all have given the same value for
f.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4335
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match...
7
2628
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
4
547
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question";...
9
340
by: LNM | last post by:
I am trying to create a survey type form where multiple choice answers are selected (radios) and I am using an onclick event to update a text box with 'correct', etc. I am using the...
1
2594
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
18
1634
by: Kamen Yotov | last post by:
hi all, i first posted this on http://msdn.microsoft.com/vcsharp/team/language/ask/default.aspx (ask a c# language designer) a couple of days ago, but no response so far... therefore, i am...
10
3682
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
0
2065
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
29
1972
by: Amar Kumar Dubedy | last post by:
implement a c++ class such that it allows us to add data members at runtime.
8
1654
by: fabian.lim | last post by:
Hi, I have a question on constant variables. In the following code snippet, I have a function assign() that takes in an iterator to the private variable v, the number of stuff to assign (int n),...
0
7227
marktang
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,...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
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...
0
5633
agi2029
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,...
1
5056
isladogs
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...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
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 ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.