Questions tagged [casting]

Casting is a process where an object type is explicitly converted into another type if the conversion is allowed. This process might lead to a change in value.

Some algorithms have significant performance differences for different data-types, for example, calculating the N-dimensional median is much is much faster for integers than float type data. Therefore, the use of casting can be important for code performance.

19752 questions
130
votes
13 answers

Difference between is and as keyword

Please tell what is the difference between is and as keyword in C#
Aman
  • 1,445
  • 2
  • 13
  • 11
127
votes
4 answers

Int to Char in C#

What is the best way to convert an Int value to the corresponding Char in Utf16, given that the Int is in the range of valid values?
Boaz
  • 25,331
  • 21
  • 69
  • 77
124
votes
4 answers

Why can I type alias functions and use them without casting?

In Go, if you define a new type e.g.: type MyInt int You can't then pass a MyInt to a function expecting an int, or vice versa: func test(i MyInt) { //do something with i } func main() { anInt := 0 test(anInt) //doesn't work, int is…
jsdw
  • 5,424
  • 4
  • 25
  • 29
124
votes
10 answers

Difference between casting and using the Convert.To() method

I have a function that casts a double on string values. string variable = "5.00"; double varDouble = (double)variable; A code change was checked in and the project builds with the error: System.InvalidCastException: Specified cast is not valid.…
user5398447
124
votes
7 answers

'is' versus try cast with null check

I noticed that Resharper suggests that I turn this: if (myObj.myProp is MyType) { ... } into this: var myObjRef = myObj.myProp as MyType; if (myObjRef != null) { ... } Why would it suggest this change? I'm used to Resharper suggesting…
HotN
  • 4,216
  • 3
  • 40
  • 51
122
votes
8 answers

Java Class.cast() vs. cast operator

Having being taught during my C++ days about evils of the C-style cast operator I was pleased at first to find that in Java 5 java.lang.Class had acquired a cast method. I thought that finally we have an OO way of dealing with casting. Turns out…
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
122
votes
9 answers

Cast Int to Generic Enum in C#

Similar to Cast int to enum in C# but my enum is a Generic Type parameter. What is the best way to handle this? Example: private T ConvertEnum(int i) where T : struct, IConvertible { return (T)i; } Generates compiler error Cannot convert…
csauve
  • 5,904
  • 9
  • 40
  • 50
121
votes
8 answers

Cast object to T

I'm parsing an XML file with the XmlReader class in .NET and I thought it would be smart to write a generic parse function to read different attributes generically. I came up with the following function: private static T ReadData(XmlReader…
Kasper Holdum
  • 12,993
  • 6
  • 45
  • 74
121
votes
8 answers

Any idea why I need to cast an integer literal to (int) here?

In the following example int i = -128; Integer i2 = (Integer) i; // compiles Integer i3 = (Integer) -128; /*** Doesn't compile ***/ Integer i4 = (Integer) (int) -128; // compiles Integer i4 = -128; // compiles Integer i5 = (int) -128; //…
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
121
votes
5 answers

Does Java casting introduce overhead? Why?

Is there any overhead when we cast objects of one type to another? Or the compiler just resolves everything and there is no cost at run time? Is this a general things, or there are different cases? For example, suppose we have an array of Object[],…
Phil
  • 5,595
  • 5
  • 35
  • 55
119
votes
4 answers

How do I provide custom cast support for my class?

How do I provide support for casting my class to other types? For example, if I have my own implementation of managing a byte[], and I want to let people cast my class to a byte[], which will just return the private member, how would I do this? Is…
esac
  • 24,099
  • 38
  • 122
  • 179
114
votes
1 answer

PostgreSQL: ERROR: operator does not exist: integer = character varying

Here i am trying to create view as shown below in example: Example: create view view1 as select table1.col1,table2.col1,table3.col3 from table1 inner join table2 inner join table3 on table1.col4 = table2.col5 /* Here col4 of…
Sarfaraz Makandar
  • 5,933
  • 16
  • 59
  • 84
114
votes
12 answers

Why do we assign a parent reference to the child object in Java?

I am asking a quite simple question, but I am bit confused in this. Suppose I have a class Parent: public class Parent { int name; } And have another class Child: public class Child extends Parent{ int salary; } And finally my Main.java…
Narendra Pal
  • 6,474
  • 13
  • 49
  • 85
112
votes
33 answers

Is it possible to assign a base class object to a derived class reference with an explicit typecast?

Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?. I have tried it and it creates a run-time error.
Maddy.Shik
  • 6,609
  • 18
  • 69
  • 98
112
votes
11 answers

Converting a pointer into an integer

I am trying to adapt an existing code to a 64 bit machine. The main problem is that in one function, the previous coder uses a void* argument that is converted into suitable type in the function itself. A short example: void function(MESSAGE_ID id,…
PierreBdR
  • 42,120
  • 10
  • 46
  • 62