Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MightConst - Runtime Constwrapper Value (C#)
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 //orrz using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { /// Runtime 'pseudo-constant' value representation [Serializable()] public class MightConst<T> { private bool constant; private T v; /// Get the contained value public static implicit operator T(MightConst<T> v) { return v.v; } /// Try to assign a new value /// \param v the new value to assign /// \return true if value has been assigned public bool assign(T v) { if (!this.constant) { this.v = v; } return !this.constant; } /// Create new MightConst /// \param v the value /// \param initial constness public MightConst(T v, bool constant) { this.v = v; this.constant = constant; } /// Change the 'constness' of the value /// \param constness to use public void setConstant(bool constant) { this.constant = constant; } } public class Program { public static void Main(string[] args) { var val = new MightConst<double>(3.2, true); Console.WriteLine(val); val.assign(20.0); // won't change since 'const' flag is set Console.WriteLine(val); val.setConstant(false); val.assign(20.0); // will change since 'const' flag is no longer set Console.WriteLine(val); } } }
run
|
edit
|
history
|
help
0
world
asxasdxsd
Sri 19 Sept
Enumeration
莊王嫩
Root
c# hashset custom comparer
Basic classes
Des encryption
C# - out parameters and reference parameters