tostring

Cannot implicitly convert type 'X' to 'string' - when and how it decides that it “cannot”?

喜你入骨 提交于 2019-11-29 16:55:12
问题 Right now I'm having it with Guid s. I certainly remember that throughout the code in some places this implicit conversion works, in others it does not. Until now I fail to see the pattern. How the compiler decides when it cannot? I mean, the type method Guid.ToString() is present, isn't it called whenever this transformation is needed? Can someone please tell me under what circumstances this transformation is done automatically and when I have to call myInstance.ToString() explicitly? 回答1:

When stepping through my program the ToString() method is being called for no reason

浪子不回头ぞ 提交于 2019-11-29 16:10:19
This is a bizarre problem I'm having even my senior programmer next to me is also confused. The issue in full is that somehow my ToString() method is being called and I don't know or understand how this is my code static void Main(string[] args) { Console.Out.WriteLine("Blank Constructor"); Form form = new Form(); <-- ToString() gets called on this line. form.ToString(); Console.Read(); } public Form() { FormName = ""; FormImageLocation = ""; FormDescription = ""; FormID = 0; CreatedDate = DateTime.Now; LastUpdate = DateTime.Now; Fields = new List<Field>(); Packets = new List<Packet>(); <--

Where and why do we use __toString() in PHP?

主宰稳场 提交于 2019-11-29 16:08:11
问题 I understand how it works but why would we practically use this? <?php class cat { public function __toString() { return "This is a cat\n"; } } $toby = new cat; print $toby; ?> Isn't this the same as this: <?php class cat { public function random_method() { echo "This is a cat\n"; } } $toby = new cat; $toby->random_method(); ?> can't we just use any other public method to output any text? Why do we need magic method like this one? 回答1: You don't "need" it. But defining it allows your object

Automatic .ToString()?

ε祈祈猫儿з 提交于 2019-11-29 15:27:34
I have a method like this: void m1(string str) and have a class like this: public class MyClass { public bool b1 { set; get; } //and other properties } Now why following code does not cause compile error? IClass2 _class2 = new Class2(); MyClass c1 = new MyClass(); _class2.m1("abcdef" + c1); When I debug it, I realized that c1.ToString() has been passed to m1 . Why this automatic .ToString() has been occurred? The only thing I could say is that m1 has been defined in IClass2 interface and has been implemented by Class2 . This follows the rules of the C# language specification around string

JS - 变量和类型

拜拜、爱过 提交于 2019-11-29 14:55:07
导读 变量和类型是学习 JavaScript 最先接触到的东西,先看下面几个问题: JavaScript 中的变量在内存中的具体存储形式是什么? 0.1+0.2 为什么不等于 0.3 ?发生小数计算错误的具体原因是什么? Symbol 的特点,以及实际应用场景是什么? [] == ![] 、 [undefined] == false 为什么等于 true ?代码中何时会发生隐式类型转换?转换的规则是什么? 如何精确的判断变量的类型? 如果你还不能很好的解答上面的问题,那说明你还没有完全掌握这部分的知识 一、Javascript数据类型 ECMAScript标准 规定了 7 种数据类型,其把这 7 种数据类型又分为两种:原始类型和对象类型。 原始类型 Null :只包含一个值: null Undefined :只包含一个值: undefined Boolean :包含两个值: true 和 false Number :整数或浮点数,还有一些特殊值( -Infinity 、 +Infinity 、 NaN ) String :一串表示文本值的字符序列 Symbol :一种实例是唯一且不可改变的数据类型 (在 es10 中加入了第七种原始类型 BigInt ,现已被最新 Chrome 支持) 对象类型 Object :除了常用的 Object , Array 、 Function

Fastest way to convert a BigInteger to a decimal (Base 10) string?

≯℡__Kan透↙ 提交于 2019-11-29 14:52:18
Answers So Far So here is the code breakdown. //Time: ~7s (linear loop algorithm) //100,000! (456,574 decimal digits) BigInteger bigIntVar = computeFactorial(100000); //The first three here are just for comparison and are not actually Base 10. bigIntVar.ToBase64String() //Time: 00.001s | Base 64 | Tetrasexagesimal bigIntVar.ToString("x") //Time: 00.016s | Base 16 | Hexadecimal bigIntVar.ToBinaryString() //Time: 00.026s | Base 02 | Binary bigIntVar.ToQuickString() //Time: 11.200s | Base 10 | String Version bigIntVar.ToQuickString() //Time: 12.500s | Base 10 | StringBuilder Version bigIntVar

How to create an array of non repeating random numbers

让人想犯罪 __ 提交于 2019-11-29 14:18:06
I have a lottery application in C# which takes in the number of numbers to draw and also the maximum number to draw.I have coded up to creating an array holding the required random numbers but I need them to be unique and am having trouble doing it.I would be very grateful if someone could give me some advice on this,Thanks Here is my code so far: class Lottery { static int[] numberHolder; //array to be filled with numbers up to an //amount entered by the user eg 42 Max static int[] drawHolder; //array to hold the each random number //drawn from the pool of numbers eg 7 numbers public Lottery(

C# Enum.ToString() with complete name

你说的曾经没有我的故事 提交于 2019-11-29 13:45:45
I am searching a solution to get the complete String of an enum. Example: Public Enum Color { Red = 1, Blue = 2 } Color color = Color.Red; // This will always get "Red" but I need "Color.Red" string colorString = color.ToString(); // I know that this is what I need: colorString = Color.Red.ToString(); So is there a solution? public static class Extensions { public static string GetFullName(this Enum myEnum) { return string.Format("{0}.{1}", myEnum.GetType().Name, myEnum.ToString()); } } usage: Color color = Color.Red; string fullName = color.GetFullName(); Note: I think GetType().Name is

Dynamically changing the text of items in a Winforms ComboBox

懵懂的女人 提交于 2019-11-29 07:27:17
I have a Winforms ComboBox that contains instances of a custom class. When the items are first added to the Items collection of the ComboBox , the ToString method is call on each of them. However when the user changes the language the application is running in, the result of the ToString method changes. Therefore how can I get the ComboBox to call the ToString method on all items again without having to remove all items from the ComboBox and adding them back in? Thanks svick, RefreshItems() work, however as it is protected (so can only be called by a subclass) I had to do public class

Python Enum class (with tostring fromstring)

流过昼夜 提交于 2019-11-29 06:47:47
I've found a simply way to implement(hack) an enum into Python: class MyEnum: VAL1, VAL2, VAL3 = range(3) I can then call this as such: bob = MyEnum.VAL1 Sexy! Alright, now I want to be able to get both the numerical value if given a string, or a string if given a numerical value. Let's say I want the strings to exactly match up to the Enum key's The best I could think of is something like this: class MyEnum: VAL1, VAL2, VAL3 = range(3) @classmethod def tostring(cls, val): if (val == cls.VAL1): return "VAL1" elif (val == cls.VAL2): return "VAL2" elif (val == cls.VAL3): return "VAL3" else: