Strange C# syntax [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 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

Related

How to check array for a value in C#? [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 2 years ago.
Improve this question
I'm trying to help someone out and this isn't my area of expertise so thought maybe someone can help me help someone else.
I have a field called Master that contains an array. I also have a field called Original that contains a string. I want to check if the string in Original is in the array field called Field1 and then with an if statement do something if true / false
"Original":"1234",
"Master":[{"ID":1,"Field1":12345},
{"ID":2,"Field1":123456},
{"ID":3,"Field1":1234},
{"ID":4,"Field1":12344}]
The array can be different each time and have a different amount of records in it.
Can anyone help me?
if Original and Master would be properties of the same class and in a variable named instance then you could use Linq to do:
bool isPresent = instance.Master.Any(entry => entry.Field1 == instance.Original);
Obviously you would need to first serialize the json to an instance of this class.

C# Best practices for check objects and strings [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 2 years ago.
Improve this question
I work on Xamarin.Forms application and want to check all possible scenarios. For example i got this code:
User user= new User(); string token= string.Empty i got a instance of object user and string token in the ViewModel's Constructor. I call them like this:
user= await GetUser();token = await GetToken();
I want to check every possible return from this calls. For object check if is empty , is null or got data. for string is empty , is null or got data ? Also hint for array of object? How to organize this?
They return what you have defined in the method definition.
The following
Task<ReturnType> GetUser()
returns an object of type ReturnType.
To compare if an object is null:
user == null.
To compare if a string is null or empty:
string..IsNullOrEmpty(<yourstring>)
I suggest you to study OOP in C#

Why IEnumerable<char>.ToString() doesn't work ? [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
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.

What is the method variable? in C# [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
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.

anonymous type object or dynamic [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 7 years ago.
Improve this question
Assuming we have a function:
public IEnumerable<object> GetViewModel(int id) {
var records = LoadRecordById(id);
var result = (from record in records
select new {
field1 = record.data1,
field2 = record.data2,
....
}).ToArray();
return result;
}
We can return it like IEnumerable<object> and like IEnumerable<dynamic>. The question is: what to use and why? what advantages/disadvantages ?
You have an anonymous type right now.
If you cast it to object, you will have no way to get the fields out. You cannot cast it back to an anonymous object (since you don't know what it was) and you can't access the properties from object.
If you cast it to dynamic, you can access the fields as you normally would, but you will lose all type safety. It will determine the type at compile time, which means you have access to the types generated at compile time, but if you type something wrong you will get a compilation error at runtime.
Best thing to do is to create a class that has the properties you need and return a list of that class. Then you will get the properties, and will get the benefits of static typing.
You should use an interface or a base class. But to answer your question:
dynamic foo = "Some string";
foo.ToUpper(); // this works fine
foo.Something(); // compiles, but runtime error
object bar = "Some string";
bar.ToUpper(); // does not compile

Categories