tostring

Most efficient Dictionary<K,V>.ToString() with formatting?

若如初见. 提交于 2019-11-27 23:53:38
问题 What's the most efficient way to convert a Dictionary to a formatted string. e.g.: My method: public string DictToString(Dictionary<string, string> items, string format){ format = String.IsNullOrEmpty(format) ? "{0}='{1}' " : format; string itemString = ""; foreach(var item in items){ itemString = itemString + String.Format(format,item.Key,item.Value); } return itemString; } Is there a better/more concise/more efficient way? Note: the Dictionary will have at most 10 items and I'm not

How does ToString on an anonymous type work?

末鹿安然 提交于 2019-11-27 23:30:22
I was messing with anonymous types, and I accidentally outputted it onto the console. It looked basically how I defined it. Here's a short program that reproduces it: using System; class Program { public static void Main(string[] args) { int Integer = 2; DateTime DateTime = DateTime.Now; Console.WriteLine(new { Test = 0, Integer, s = DateTime }); Console.ReadKey(true); } } Now, the output is: { Test = 0, Integer = 2, s = 28/05/2013 15:07:19 } I tried using dotPeek to get into the assembly to find out why, but it was no help. [1] Here's the dotPeek'd code: // Type: Program // Assembly:

What are the original reasons for ToString() in Java and .NET?

主宰稳场 提交于 2019-11-27 22:22:43
I've used ToString() modestly in the past and I've found it very useful in many circumstances. However, my usage of this method would hardly justify to put this method in none other than System.Object . My wild guess is that, at some point during the work carried out and meetings held to come up with the initial design of the .NET framework, it was decided that it was necessary - or at least extremely useful - to include a ToString() method that would be implemented by everything in the .NET framework. Does anyone know what the exact reasons were? Am I missing a ton of situations where

Entity Framework 4 / Linq: How to convert from DateTime to string in a query?

旧城冷巷雨未停 提交于 2019-11-27 21:57:48
I have the following query: from a in Products select new ProductVM { id = a.id, modified = a.modified.ToString() } Which gives me an error of: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. The modified in the Products table is DateTime. The modified in the ProductVM class is string. Any ideas? This has to be a trivial issue. ToString() is not supported in Linq to Entities - there is a list of function helpers as part of SqlFunctions but this doesn't support Date to string conversion. Easiest

Laravel Error: Method Illuminate\\View\\View::__toString() must not throw an exception

浪尽此生 提交于 2019-11-27 20:24:19
Have you seen this lovely error while working in Laravel? Method Illuminate\View\View::__toString() must not throw an exception I have seen it and it's incredibly annoying. I have found out two reasons why this error gets thrown. I just want to help people not take hours and hours of time. View answers & situations below. :) There is a very simple solution: don't cast View object to a string. Don't: echo View::make('..'); or echo view('..'); Do: echo View::make('..')->render(); or echo view('..')->render(); By casting view, it uses __toString() method automatically, which cannot throw an

How to create an extension method for ToString?

五迷三道 提交于 2019-11-27 20:21:53
I have tried this: public static class ListHelper { public static string ToString<T>(this IList<String> list) { return string.Join(", ", list.ToArray()); } public static string ToString<T>(this String[] array) { return string.Join(", ", array); } } But it does not work, both for string[] and List<string> . Maybe I need some special annotations? Eric Lippert Extension methods are only checked if there are no applicable candidate methods that match. In the case of a call to ToString() there will always be an applicable candidate method, namely, the ToString() on object . The purpose of extension

Difference between .ToString and “as string” in C#

给你一囗甜甜゛ 提交于 2019-11-27 20:01:09
问题 What is the difference between using the two following statements? It appears to me that the first "as string" is a type cast, while the second ToString is an actual call to a method that converts the input to a string? Just looking for some insight if any. Page.Theme = Session["SessionTheme"] as string; Page.Theme = Session["SessionTheme"].ToString(); 回答1: If Session["SessionTheme"] is not a string , as string will return null . .ToString() will try to convert any other type to string by

Java toString() using reflection?

雨燕双飞 提交于 2019-11-27 18:41:33
I was writing a toString() for a class in Java the other day by manually writing out each element of the class to a String and it occurred to me that using reflection it might be possible to create a generic toString() method that could work on ALL classes. I.E. it would figure out the field names and values and send them out to a String. Getting the field names is fairly simple, here is what a co-worker came up with: public static List initFieldArray(String className) throws ClassNotFoundException { Class c = Class.forName(className); Field field[] = c.getFields(); List<String> classFields =

Negative numbers to binary string in JavaScript

依然范特西╮ 提交于 2019-11-27 17:26:55
Anyone knows why javascript Number.toString function does not represents negative numbers correctly? //If you try (-3).toString(2); //shows "-11" // but if you fake a bit shift operation it works as expected (-3 >>> 0).toString(2); // print "11111111111111111111111111111101" I am really curious why it doesn't work properly or what is the reason it works this way? I've searched it but didn't find anything that helps. Short answer: The toString() function takes the decimal, converts it to binary and adds a "-" sign. A zero fill right shift converts it's operands to signed 32-bit integers in two

What is the Objective-C equivalent for “toString()”, for use with NSLog?

纵然是瞬间 提交于 2019-11-27 17:24:55
Is there a method that I can override in my custom classes so that when NSLog(@"%@", myObject) is called, it will print the fields (or whatever I deem important) of my object? I guess I'm looking for the Objective-C equivalent of Java's toString() . It is the description instance method, declared as: - (NSString *)description Here's an example implementation (thanks to grahamparks): - (NSString *)description { return [NSString stringWithFormat: @"Photo: Name=%@ Author=%@", name, author]; } Add this to the @implementation of your Photo class: - (NSString *)description { return [NSString