immutability

Why tuple is not mutable in Python? [duplicate]

怎甘沉沦 提交于 2019-12-04 13:18:28
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why are python strings and tuples are made immutable? What lower-level design makes tuple not mutable in Python? Why this feature is useful? 回答1: A few reasons: Mutable objects like lists cannot be used as dictionary keys or set members in Python, since they are not hashable. If lists were given __hash__ methods based on their contents, the values returned could change as the contents change, which violates the

Instantiating Immutable Objects With Reflection

六眼飞鱼酱① 提交于 2019-12-04 12:37:15
问题 I created a base class to help me reduce boilerplate code of the initialization of the immutable Objects in C#, I'm using lazy initialization in order to try not to impact performance a lot , I was wondering how much am I affecting the performance by doing this? This is my base class: public class ImmutableObject<T> { private readonly Func<IEnumerable<KeyValuePair<string, object>>> initContainer; protected ImmutableObject() {} protected ImmutableObject(IEnumerable<KeyValuePair<string,object>>

Practical example for Immutable class

穿精又带淫゛_ 提交于 2019-12-04 12:11:07
It is obvious that immutability increases the re-usability since it creates new object in each state change.Can somebody tells me a practical scenario where we need a immutable class ? Consider java.lang.String . If it weren't immutable, every time you ever have a string you want to be confident wouldn't change underneath you, you'd have to create a copy. Another example is collections: it's nice to be able to accept or return a genuinely immutable collection (e.g. from Guava - not just an immutable view on a mutable collection) and have confidence that it won't be changed. Whether those count

Creating an immutable object without final fields?

送分小仙女□ 提交于 2019-12-04 12:06:16
问题 Can we create an immutable object without having all fields final? If possible a couple of examples would be helpful. 回答1: Declare all fields private and only define getters: public final class Private{ private int a; private int b; public int getA(){return this.a;} public int getB(){return this.b;} } citing @Jon Skeet's comment, final class modifier is useful for: While an instance of just Private is immutable, an instance of a subclass may well be mutable. So code receiving a reference of

Why String is immutable or final in Java [duplicate]

空扰寡人 提交于 2019-12-04 11:32:07
问题 This question already has answers here : Why is the String class declared final in Java? (16 answers) Closed 6 years ago . As i was told this is important String Interview question in Java, which starts with discussion of " What is String ", how String is different in java than in C or C++ and then you are asked about immutable objects and you're asked the main question: " Why String is immutable or final in Java ". Can you share your Ideas ? Thanks in advance. 回答1: It is mainly for security

Should every immutable class be final?

老子叫甜甜 提交于 2019-12-04 11:12:51
I was designing a Card class to be used in a Blackjack game. My design was to make a Card class with a getValue() that returns, for example, 11 for J, 12 for Q and 13 for K, and then extend it with a BlackjackCard class to override that method so that those cards return 10. Then something hit me: objects of the Card class should be immutable. So I re-read Effective Java 2nd Edition to see what to do and I there I found that immutable classes need to be final, to avoid a subclass to break the immutability. I also looked in Internet and everyone seems to agree in that point. So should the Card

Examples of Immutable Types in .Net

半世苍凉 提交于 2019-12-04 10:50:59
问题 We know the concept of immutability but need to know few immutable types other than String DateTime Are there more? 回答1: A list of immutable types in the framework class library follows below. (Feel free to expand it!) System.… All primitive value types: (Note: not all value types are immutable!) Byte and SByte Int16 and UInt16 Int32 and UInt32 Int64 and UInt64 IntPtr Single Double Decimal All anonymous types created by the compiler ( new { ... } in C#, New With { ... } in VB.NET) (Wrong for

C# immutable int

柔情痞子 提交于 2019-12-04 10:32:18
问题 In Java strings are immutable. If we have a string and make changes to it, we get new string referenced by the same variable: String str = "abc"; str += "def"; // now str refers to another piece in the heap containing "abcdef" // while "abc" is still somewhere in the heap until taken by GC It's been said that int and double are immutable in C#. Does it mean that when we have int and later change it, we would get new int "pointed" by the same variable? Same thing but with stack. int i = 1; i +

Design a mutable class that after it's consumed becomes immutable

独自空忆成欢 提交于 2019-12-04 08:56:24
Suppose that the scenario doesn't allow to implement an immutable type. Following that assumption, I'd like opinions / examples on how to properly design a type that after it's consumed, becomes immutable. public class ObjectAConfig { private int _valueB; private string _valueA; internal bool Consumed { get; set; } public int ValueB { get { return _valueB; } set { if (Consumed) throw new InvalidOperationException(); _valueB = value; } } public string ValueA { get { return _valueA; } set { if (Consumed) throw new InvalidOperationException(); _valueA = value; } } } When ObjectA consumes

Set System.Drawing.Color values

瘦欲@ 提交于 2019-12-04 08:46:45
问题 Hi how to set R G B values in System.Drawing.Color.G ? which is like System.Drawing.Color.G=255; is not allowed because its read only Property or indexer 'System.Drawing.Color.G' cannot be assigned toit is read only i just need to create a Color Object by assigning custom R G B values 回答1: You could create a color using the static FromArgb method: Color redColor = Color.FromArgb(255, 0, 0); You can also specify the alpha using the following overload. 回答2: The Color structure is immutable (as