5
Answers

Question on datatypes

harish reddy

harish reddy

7y
211
1
First example:
long value = 234L;
Console.WriteLine(value);
Q: Why should we give 234L instead of 234? Anyway the datatype is long. then why is L needed at the end of 234?
second example:
double d = 0.1f; Console.WriteLine(d);
float c = 0.1f; Console.WriteLine(c);
Q: Why is f required after 0.1 in both cases ? Main doubt is "f" is called float. Since the data type is float,Why f needs to be put after 0.1 ?
Answers (5)
0
Amit Gupta
NA 16.5k 25.7k 7y
Hey
 

Precision is the main difference.

 
Int - 32 bit - No suffix
 

Float - 7 digits (32 bit) - 'f' suffix

Double-15-16 digits (64 bit) - 'd' suffix

 
As Nilesh said, if you don't specify any suffix to a number then it will be co sidered as integer value i.e int
 
Q.Why is f required after 0.1 in both cases ? Main doubt is "f" is called float. Since the data type is float,Why f needs to be put after 0.1 ? 
A.  As float has a suffix f and double as d, hence, double has more range than float, so in this case, type casting​ has been applied, as float has smaller value than double, it has adjusted in that datatype without any problem but if you have been typecasted from double to float, it will loose its value and retains to its max value.
 
Good Luck !! 
Accepted
1
Nilesh Shah
NA 22.3k 215k 7y
please mark my answer as accepted if it satisfied your question.
1
Nilesh Shah
NA 22.3k 215k 7y
If you don't specify any suffix, then the integer values are evaluated as per the following sequence:
int
uint
long
ulong

The suffix L denotes a long in your value 234L.

When you use the suffix L, the type of the literal integer is determined to be either long or ulong, depending on its size. In your case, it is long because it less than the range of ulong.

A common use of the suffix is to call overloaded methods by passing in values directly or when passig in "var" type variables.

similar thing applies in case of f suffix.

please see below code, or download attached project and see yourself.
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             SampleMethod(5);    // Calls the method with the int parameter    
  6.             SampleMethod(5L);   // Calls the method with the long parameter    
  7.   
  8.             Console.WriteLine("------------");  
  9.   
  10.             var myVal1 = 5;  
  11.             SampleMethod(myVal1);   // Calls the method with the int parameter    
  12.   
  13.             var myVal2 = 5L;  
  14.             SampleMethod(myVal2);   // Calls the method with the long parameter  
  15.   
  16.             Console.ReadKey();  
  17.         }  
  18.   
  19.         public static void SampleMethod(int i)  
  20.         {  
  21.             Console.WriteLine("Called integer version of SampleMethod");  
  22.         }  
  23.   
  24.         public static void SampleMethod(long l)  
  25.         {  
  26.             Console.WriteLine("Called long version of SampleMethod");  
  27.         }  
  28.     } 
 
0
Amit Gupta
NA 16.5k 25.7k 7y
Hey
 
Type casting means converting One data type to another data type .
 
You can read the below link for detail discussion
 
https://stackoverflow.com/questions/618535/difference-between-decimal-float-and-double-in-net 
0
harish reddy
NA 95 5.3k 7y
Hi,
Why is f required after 0.1 in both cases ? Main doubt is "f" is called float. Since the data type is float,Why f needs to be put after 0.1 ?
A. As float has a suffix f and double as d, hence, double has more range than float, so in this case, type casting​ has been applied, as float has smaller value than double, it has adjusted in that datatype without any problem but if you have been typecasted from double to float, it will loose its value and retains to its max value.
 
In the above question you said :type casting...what is it means?
Also are these below both same: still some confustion exisits. can you be more clear
 
float x = 0.5f
float x = 0.5 
 
double x = 0.5d
double x = 0.5