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 3 years ago.
Improve this question
I would like to create a class or struct for this message, but I don't know what the following means:
body:,from:,to:,
I know what they are doing on a high-level, but don't know what these words are for or how they would fit into a class.
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
body: "Join Earth's mightiest heroes. Like Kevin Bacon.",
from: new Twillio.Types.PhoneNumber("+1501712261"),
to: new Twillio.Types.PhoneNumber("+1501712261")
)
This is a named arguments of methods. Basically, you can specify an argument for a parameter by associating the argument with the parameter's name without keeping in mind the order of arguments in parameters list. You can read more at MSDN
Named arguments free you from the need to remember or to look up the
order of parameters in the parameter lists of called methods. The
parameter for each argument can be specified by parameter name.
the body:, from: ? It's the name of the parameters, it makes the call more readable, it allows you provide the arguments in another order, and it has no influence on the performance, it's just syntax sugar.
Related
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 1 year ago.
Improve this question
Is it possible to make a couple of overrides for ToString() method in class instead of one?
I have a project, that would use ToString() method, depends on the result set of searching.
Is it possible to make a couple of overrides for ToString() method in class instead of one?
It depends
You either mean "overloads" in which case, sure - make as many variations of a method as you want but they have to have different signatures (some unique combination of number of/type of/order of types of arguments)
Or you really do mean "overrides" in which case you can only do it if there is a suitable method in a base class to override. If your class descends from object then no; object has only one overridable ToString. You could overload other variations of ToString but they wouldn't be overrides
No. Overloads have to differ in signature, and in this case there's nothing to vary because the function doesn't have any arguments.
If you had more than one identical toString function, how would the compiler know which one you intended to call?
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.
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
I use C#.
When I defined Hoge method below,
void Hoge(bool isBar){}
I get the Hoge method like below
var methodName = this.Hoge as Action<bool>).Method.Name;
However, I can't understand what does this.Hoge type.
Because, it can assign and casting.
but, it can't give me method name directly.
this.Hoge.Method.Name;
and, it also error. typeof(this.Hoge)
what is method variable exactly?
The code you provided isn't valid C# code, so it's very difficult to understand what you're asking. But I think you're trying to understand how the expression this.Hoge is translated into something that can eventually provide you with the name of the method.
If so, then your code example should look something like this:
var methodName = ((Action<bool>)this.Hoge).Method.Name;
And what that does is to implicitly create an instance of a delegate type (in this case, of the type Action<bool>), as if you'd written this:
var methodName = new Action<bool>(this.Hoge).Method.Name;
And of course, once you have a delegate type, that type has a Method property, which returns a MethodInfo object which in turn, of course, has a Name property.
If that is not what you're asking, please improve your question by providing a valid, compilable C# example of what you're asking about, along with a more precisely worded question about that code.
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 9 years ago.
Improve this question
I want to make a a function, and i dont want to write it many times with different types.
Can I use 'where' and "tell it" that I want one of the specific types that I write?
By the way, I need a function to handle integers and another to handle floating numbers.
No, you can't. There are specific constraints you can apply for type parameters, e.g. it must be a non-nullable value type, or it must be a class, or it must implement an interface or whatever... but you can't specify a set of types and say that it must be one of those.
Even if you could do so, I suspect it wouldn't do what you want - because I expect you want to perform arithmetic on these types.
Two options:
If you're using C# 4 you could use dynamic typing. It doesn't give you compile-time safety, but it'll work if you're careful.
You could use Marc Gravell's generic operators in MiscUtil
You can't statically restrict the function to only take floats or integers. To do so, you would have to check the argument types manually inside the function.
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.