Why IEnumerable<char>.ToString() doesn't work ? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I wonder why can't we just convert IEnumerable to string using ToString() ! I mean what is the underlying reason behind this.
Microsoft docs say " ToString() returns A string that represents the current object." What is this A string ? Is it a special property of the object ? Why int.ToString() works but IEnumerable.ToString() doesn't ?

An IEnumerable<char> is not neccessarily a string. Imagine you have some service that returns an infinite number of characters (e.g. a stream). As there´s no end of that stream and data flows endlessly you are not able to call ToString and materialize a string from it.
However ToString just returns a representation of the object, not its data. In case of an array for instance, the object is the collection of items, or more general just a container. What you expect is the data that is contained in that container.
So when calling myArray.Totring for example you don´t get { 1, 2, 3 }, but simply System.int[]. That´s what ToString returns if there is no override for the type: its type-name. The same happens in your case: there is no overrdie for ToString defined for char[] or List<char> or whatever, so the method falls back to use typeofObject.FullName.

Related

Where should I call a print function for Non-compatible type conversion in C#.NET? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
In what line of this C#.NET code below would be the best way to print (Console.WriteLine()) the output of non-compatible type conversion:
string Start = "2757457";
int Middle = Convert.ToInt32(Start); // is it: Console.WriteLine(Middle)
int End = int.Parse(Start); // or is it: Console.WriteLine(End)
I don't think your question has anything to do with printing to the console and everything to do with should you use Parse or Convert. Assuming that's correct then you may find the following breakdown of Convert, Parse, & TryParse applicable. If it's not correct, clarify and I'll either edit my answer or delete as applicable.
Parse Takes a string and (assuming it is a number) outputs the number equivalent of it. It will throw an exception if the value is null, not a number, or outside the min/max range of Int.
Convert.ToInt32 Takes a string and (assuming it is a number) checks if it's null. If null it returns 0 otherwise it calls Parse.
TryParse Takes a string and if it's not a number returns false. If it is a number it'll return true. If it's null, it will return 0 in the out parameter (but return false as it's primary return value). If it is a number, it'll return the number as an out parameter.

why is Next() method called next? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Why does Random.Next() have Next in it's name? I know what it does, but the name doesn't seem to correspond to it.
It returns the next number in the infinite sequence of numbers generated from your Random instance's seed.
In computer science jargon, a "generator" is a specific kind of function: one that returns a different result each time it is called. It is traditional to call this function something like next(), because they are often used to return the next piece of a sequence (perhaps infinite). RNGs are just a special case of generator function, returning the next value in a calculated sequence.

How to return value of List without []? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to return value from list or a array without brackets?
For example i have List of integers:
List<int> myIntegers = new List<int>();
myIntegers.Add(1);
myIntegers.Add(2);
myIntegers.Add(3);
int newValue = myIntegers; //normally i should use myIntegers[0]
i want myIntegers to return the first value from list but i want to use it without brackets. Is there a way?
Thanks. M.
As you wrote it, it will never compile. However, if you really have a thing against [], then yes. You can use First() (or its companion, FirstOrDefault()), or just ElementAt(0).
First and FirstOrDefault can take a predicate, which is often useful, FirstOrDefault returns default(T) if there is no first element that matches the predicate, and ElementAt does the exact same thing as the [].
If you subclass List<T>, you could potentially do this with an implicit conversion to T. Although, what you are describing is sort of a weird data structure, and not really idiomatic c#. Probably best not to call it a list.

Strange C# syntax [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I keep seeing the following type of syntax:(string[])myList.ToArray(typeof(string));
What does it mean when the object type is declared at the front of the object in brackets, before calling a method on it?
I am struggling to locate explanations becuase I don't know what this setup would be called.
Any help appreciated.
Thanks
it's Called a Casting, Casting is usually a matter of telling the compiler that although it only knows that a value is of some general type, you know it's actually of a more specific type. For example:
object x="any string";
string s=(string)x;
if we are using the upper one then it may possible that it will through the exception at runtime like if you are using
object x="string";
int s=(int)x;
it will through the Exception at runtime unable to cast
but if you use as oprator then it will return a null rather then throwing an exception.
object x = new object();
string y = x as string; // Now y is null because x isn't a string

Parse method or overload the constructor [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
When writing a method that takes a string and populates a poco based on it, is it better to have a static Parse(string s) method like Int32.Parse() or overload the constructor so it takes a string?
I prefer the constructor version, but including both is easy, since the constructor can just call Parse. This is the pattern followed by the Guid struct (and likely others as well.)
I should add that if you're not dealing with a struct, then the static method should probably be referring to the constructor (or even a separate method that both can call) since you can't assign to this in a class constructor.
EDIT: As TrueWill points out, if you do include Parse, you should include TryParse as well. Incidentally, Guid is once again instructive: the Parse method actually uses TryParse, and just throws an exception if TryParse returns false.
If the method might fail due to an invalid string, I'd lean towards Parse and include TryParse as per the TryParse pattern.
I would recommend using .Parse(string s) if its a simple object, if the object stores more then 1-2 values you should use the constructor, or in other words, dont parse if the return value will be an instance with memebers unaffected by the parse value.

Categories