Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have came across the following code:
EnsureGraphForAppOnlyAuth();
_ = _appClient ??
throw new System.NullReferenceException("Graph has not been initialized for app-only auth");
I understand if _appClient is null, it will throw an exception. (1)My question is what is _ =? (2)What's it called so I can look it up in a reference?
Next, in continuation of the code, I have this
return _appClient.Groups
.Request()
.Select(u => new
{
u.DisplayName,
u.Id
})
// Get at most 25 results
.Top(25)
// Sort by display name
.OrderBy("DisplayName")
.GetAsync();
(3)Is this chaining?
.Groups
.Request()
.Select()
.OrderBy()
.GetAsync()
Doesn't each function depend on the return value of the prior function? If so, there's no error checking or checking the return value prior to calling the next function? What happen if these where all async?
Also, if I want to write a class and its function so a client can do the same thing, (4) do I have to do anything special when designing the class except of each function return a value?
_ is a valid variable name in C#. Usually, developers use it as a placeholder to denote that 'this is required for the syntax to be valid, but I don't intend to use it'
Apparently it is called a discard. I didn't know that it had a real name.
Yes, that's using a fluent API style to chain methods together. Each method uses the return value of the previous method as its parameter.
Take a look at how the LINQ library is set up. You'll want to use extension methods similarly
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
So I was experimenting with Blazor and I'd like to understand the difference between these two.
<p #onclick="OnClickCallback">Click me normal</p>
<p #onclick="async () => await OnClickCallback()">Click me lambda</p>
#code {
private async Task OnClickCallback()
{
await Task.Delay(500);
}
}
What is the difference between these two approaches. What happens behind the scenes. I've found some issues with EF Core (which is a whole other topic) where the lambda approach did not throw an exception, and the normal did.
What is the difference between these two approaches
It adds another function, which means it introduces another call on the stack.
Also, because this is an async lambda, it allocates another Task instance.
This being said, using the former will have a negligible performance benefit.
For completeness, there is a third alternative that would prevent another Task being used, but would still result in another function call:
() => OnClickCallback()
In Blazor, that's a very good question. I don't really know if they compile differently, but I wanted to mention something that's quite important: a lambda method can pass a variable other than the normal event variables that go with the event. It's very useful to do something like this:
#foreach (var item in SomeCustomList)
{
<p #onclick="async (e) => await OnClickCallback(e, item )">Click me lambda</p>
}
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 have just learned get set principles in C# and I wonder whether there is any interest of using the same principle for methods.
If I understand well, get and set are used for variables. But it could be possible to apply the same principle for methods. For instance:
private int _GiveMultiply()
{
int a = ...
int b = ...
return c = a*b;
}
public int GiveMultiply
{
get { return _GiveMultiply(); }
}
But is there any kind of interest to do such a thing ?
For example is there a risk to use a public function that can be prevented using such a process ?
The answer is: it depends. I'll try to help you to reformulate your question: does it make any sense to return a function rather than computed result from another fuction? Then I would say: definitely yes (let me know if you'd like to know ehy, I'll update this post). But the example you showed does not return a fuction, it just wraps it into yet another fuction, which is useless. The only exception is various kinds of abstract method patterns, where you might have public function with some predefined logic and call to the abstract/virtual fuctions; rarely they do have same name, then indeed wrapping sort of works.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
void Update ()
{
if (transform.Translate((Input.acceleration.x), 0, 0))
{
GetComponent<Rigidbody>().AddForce(Vector2.right);
}
}
I am using the accelerator for a Android App and doesn't seem to like it. I can change it to work for KeyDown but it wont work with the accelerator.
Error:
"Cannot implicitly convert type "void" to "bool"
According to the documentation http://docs.unity3d.com/ScriptReference/Transform.Translate.html:
transform.Translate does not return a boolean; its return type is void. Therefore you cannot use an if statement to evaluate whether or not it was successful.
If you want to check to see if the translate happened correctly you would need to check the side effects from calling transform.Translate. In other words see what has changed on the transform and see if it matches your expectations.
Don't use the if statement, as Translate returns nothing. Just leave the AddForce line
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 8 years ago.
Improve this question
(questionn is misspelled on purpose: stackoverflow does not allow the word "question" in the title)
Consider a property SomethingHasChanged
This can be read as a statement, SomethingHasChanged!, or a question, SomethingHasChanged?
What are the conventions (C#) for naming booleans? As statements or as questions?
Background
All code of a client I work for, is written in Dutch. In Dutch there is a slight difference between these two forms that in English does not exist. Therefore we need to make a decision between the two forms. Example: ErIsIetsGewijzigd! vs IsErIetsGewijzigd?
Many boolean properties start with Is or Has , e.g.:
this.IsRed = this (object) is red
this.HasChildren = this (object) has children
Your name doesn't really fit this convention well:
this.SomethingHasChanged = this (object) has something that has changed.
To match this convention, I'd rename your property IsDirty or similar.
A Boolean variable or property is an outcome of an expression, therefore, it is not a question, but a statement.
If, instead, you are asking a question, which requires an operation to answer it, than it should be a method and may be named as such.
It depends on whether you want to communicate you read state or determine it on request. A property bool HasChanged {get; set; } can be called twice and it should result in the same stored value. However a method bool HasChanged() communicates it is going to determine it for you on demand.
Finally, as I am Dutch as well, ErIsIetsGewijzigd sounds like a command, therefore as I read it, it should set the property, i.e. ErIsIetsGewijzigd(true);.
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 8 years ago.
Improve this question
It may be a silly question, but since I can't give it an answer by my own, I will ask it here.
Let we have a module that we want to use in an http handler (the web app is written in C# using ASP.NET) and let that this module implements the IDisposable interface. Then a common approach is to
use the module as below:
using(var module = new ModuleName(param1, param2, param3))
{
}
Is it better to place any code to variables that we are going to use only inside the body of this using statement or before this. In terms of code:
Is it better (and why) the first approach or the second approach:
first approach
using(var module = new ModuleName(param1, param2, param3))
{
int a = Request.GetParam<int>("aNumber");
string b = Request.GetParam<string>("bString");
// and other parameters contained to the http request
}
second approach
int a = Request.GetParam<int>("aNumber");
string b = Request.GetParam<string>("bString");
// and other parameters contained to the http request
using(var module = new ModuleName(param1, param2, param3))
{
}
If there isn't any technical reason -and it is an opinion based decision- that we should prefer the first approach to second approach or vice versa, please let me know, in order to delete my post.
It depends on if you need those variables outside of the scope of the using-statement. If so, you need to declare them outside anyway. If not, declare them in the using.
Why? It's all about readability, fail-safety and refactoring.
This is true not only for the using but scopes and variable declaration in general. Read:
https://codereview.stackexchange.com/questions/6283/variable-declaration-closer-to-usage-vs-declaring-at-the-top-of-method
https://softwareengineering.stackexchange.com/questions/56585/where-do-you-declare-variables-the-top-of-a-method-or-when-you-need-them