Error 1 Cannot Modify The Result Of An Unboxing Conversion
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Casting, unboxing, conversion..? up vote 2 down vote favorite 2 Recently I am learning value types and I am bit confused. Also both casting and unboxing uses the same syntax - (expected type)(object), right? And what about simple conversion between types, that is casting or just conversion? int x = (int)2.5; //casting? object a=x; int Y=(int)a; //unboxing I think Random r=new Random(); object X=r; Random R=(Random)X; // casting c# boxing value-type share|improve this question edited Jan 19 '11 at 11:47 Steve 30.1k31931 asked Jan 19 '11 at 11:41 Lojol 2622412 Hi @Lojol, welcome to Stack Overflow! If you care, you might be able to help us solve a little mystery about the edits in your question. Like: did you change the tags yourself right after asking this? Please see the discussion at our "meta" site: Pending edit does not reveal any changes, maybe due to edits in grace period? Thanks! –Arjan Jan 19 '11 at 12:25 @Steve, same for you! (See my comment above.) Like: did you try to re-order the tags? –Arjan Jan 19 '11 at 12:40 add a comment| 5 Answers 5 active oldest votes up vote 6 down vote accepted There's a lot of things to consider here, but let's tackle the simplest first: What is the syntax (type)expression? Well, in its basic form, it is considered casting. You cast the expression from one type, to another. That's it. However, what exactly happens, that depends on the type, and a lot of other things. If casting a value type to something else, you depend on one of the two types involved to declare a casting operator that handles this. In other words, either the value type need to define a casting operator that can cast to that other type, or that other type need to define a casting operator that can cast from the original type. What that operator does, is up to the author of that operator. It's a method, so it can do anything. Casting a value type to some other type gives you a different value, a separate value t
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. C# C# Reference C# Compiler Errors C# Compiler Errors Compiler Error CS0445 Compiler Error CS0445 Compiler http://stackoverflow.com/questions/4734807/casting-unboxing-conversion Error CS0445 Compiler Error CS0001 Compiler Error CS0006 Compiler Error CS0007 Compiler Error CS0015 Compiler Error CS0016 Compiler Error CS0019 Compiler Error CS0029 Compiler Error CS0034 Compiler Error CS0038 Compiler Error CS0039 Compiler Error CS0050 Compiler Error CS0051 Compiler Error CS0052 Compiler Error CS0071 Compiler Error CS0103 Compiler Error CS0106 https://msdn.microsoft.com/en-us/library/1zd0a13x.aspx Compiler Error CS0115 Compiler Error CS0116 Compiler Error CS0120 Compiler Error CS0122 Compiler Error CS0134 Compiler Error CS0151 Compiler Error CS0163 Compiler Error CS0165 Compiler Error CS0173 Compiler Error CS0178 Compiler Error CS0188 Compiler Error CS0201 Compiler Error CS0229 Compiler Error CS0233 Compiler Error CS0234 Compiler Error CS0246 Compiler Error CS0260 Compiler Error CS0266 Compiler Error CS0269 Compiler Error CS0270 Compiler Error CS0304 Compiler Error CS0310 Compiler Error CS0311 Compiler Error CS0413 Compiler Error CS0417 Compiler Error CS0433 Compiler Error CS0445 Compiler Error CS0446 Compiler Error CS0504 Compiler Error CS0507 Compiler Error CS0518 Compiler Error CS0523 Compiler Error CS0545 Compiler Error CS0552 Compiler Error CS0563 Compiler Error CS0570 Compiler Error CS0571 Compiler Error CS0579 Compiler Error CS0592 Compiler Error CS0616 Compiler Error CS0650 Compiler Error CS0686 Compiler Error CS0702 Compiler Error CS0703 Compiler Error CS0731 Compiler Error CS0826 Compiler Error CS0834 Compiler Error CS0840 Compiler Error CS0843 Compile
help? Post your question and get tips & solutions from a community of 418,502 IT Pros & Developers. It's quick & easy. Cannot modify the result of an unboxing conversion? P: n/a Håkan Johansson error CS0445: Cannot modify https://bytes.com/topic/c-sharp/answers/665395-cannot-modify-result-unboxing-conversion the result of an unboxing conversion I'm quite new to C# and can't really see how to get rid of the above error. I have an ArrayList instance to which I add two types of elements. The first type of http://www.interact-sw.co.uk/iangblog/2005/01/27/modifyinsitu element is a class, the other type of element is a struct. Sometimes I need to change the field values of the elements held by the ArrayList. In the case of the class type element, this is no problem. However, in error 1 the case of the struct type element, C# won't allow it. First of all, I don't really understand why. Secondly, do I really have to create a new class with similar content of the struct to get rid of this error? Regards Carl Johansson Jun 20 '07 #1 Post Reply Share this Question 6 Replies P: n/a marss Håkan Johansson wrote: error CS0445: Cannot modify the result of an unboxing conversion I'm quite new to C# and can't really see how error 1 cannot to get rid of the above error. I have an ArrayList instance to which I add two types of elements. The first type of element is a class, the other type of element is a struct. Sometimes I need to change the field values of the elements held by the ArrayList. In the case of the class type element, this is no problem. However, in the case of the struct type element, C# won't allow it. First of all, I don't really understand why. Secondly, do I really have to create a new class with similar content of the struct to get rid of this error? Regards Carl Johansson Hi, I guess you wrote something like this: ((SomeStruct)ar[0]).Field1 = ...; Change it to: SomeStruct s1 = (SomeStruct)ar[0]; s1.Field1 = ...; Regards, Mykola http://marss.co.ua Jun 20 '07 #2 P: n/a Jon Skeet [C# MVP] On Jun 20, 10:06 am, "Håkan Johansson" (1 item) February (2013) (6 items) September (2011) (2 items) November (2010) (4 items) September (2010) (1 item) August (2010) (4 items) July (2010) (2 items) September (2009) (1 item) June (2009) (1 item) April (2009) (1 item) November (2008) (1 item) October (2008) (1 item) September (2008) (1 item) July (2008) (1 item) June (2008) (1 item) May (2008) (2 items) April (2008) (2 items) March (2008) (5 items) January (2008) (3 items) December (2007) (1 item) November (2007) (1 item) October (2007) (1 item) September (2007) (3 items) August (2007) (1 item) July (2007) (1 item) June (2007) (2 items) May (2007) (8 items) April (2007) (2 items) March (2007) (7 items) February (2007) (2 items) January (2007) (2 items) November (2006) (1 item) October (2006) (2 items) September (2006) (1 item) June (2006) (2 items) May (2006) (4 items) April (2006) (1 item) March (2006) (5 items) January (2006) (1 item) December (2005) (3 items) November (2005) (2 items) October (2005) (2 items) September (2005) (8 items) August (2005) (7 items) June (2005) (3 items) May (2005) (7 items) April (2005) (6 items) March (2005) (1 item) February (2005) (2 items) January (2005) (5 items) December (2004) (5 items) November (2004) (7 items) October (2004) (3 items) September (2004) (7 items) August (2004) (16 items) July (2004) (10 items) June (2004) (27 items) May (2004) (15 items) April (2004) (15 items) March (2004) (13 items) February (2004) (16 items) January (2004) (15 items) Blog Home RSS 2.0 Writing Programming C# 5.0 Programming WPF Other Sites Interact Software CLR Generics Limitation - Modifying Values In Situ In a Container Thursday 27 January, 2005, 11:40 AM I've recently been in a discussion with Ondra Spilka over on the DOTNET-CLR list. The discussion was about a subtle issue with ArrayList and value types - while you can put values into an ArrayList you cannot modify them in situ, because the indexer always returns a boxed copy of the value. If you want to change the value, you must replace it entirely. (This is only an issue for values with several fields. For singular values like integers or doubles, there is no dist