FusedQyou
FusedQyou
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
All out parameters must be assigned a value before the method returns, but an async method effectively returns immediately by returning a Task, meaning the value wouldn't be available yet until the method reaches a point where the value would have been set.
47 replies
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
If this is a comment on why async code can't have an out parameters then yes, most likely
47 replies
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
So generally with async code you just return an object or struct as the return type. Iterators' purpose is to return a collection of items so out parameters don't really make sense anyway
47 replies
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
The thing with async code and iterators is that it can't properly ensure that the out parameter is set or set to its latest value, so it's just not supported because of it.
47 replies
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
Below the example on the site is also some rules regarding out keywords, which FestivalDelGelato already hinted on. The following limitations apply to using the out keyword: - out parameters are not allowed in asynchronous methods. - out parameters are not allowed in iterator methods. - Properties cannot be passed as out parameters.
47 replies
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
public void Main()
{
double radiusValue = 3.92781;
//Calculate the circumference and area of a circle, returning the results to Main().
CalculateCircumferenceAndArea(radiusValue, out double circumferenceResult, out var areaResult);
System.Console.WriteLine($"Circumference of a circle with a radius of {radiusValue} is {circumferenceResult}.");
System.Console.WriteLine($"Area of a circle with a radius of {radiusValue} is {areaResult}.");
Console.ReadLine();
}

//The calculation worker method.
public static void CalculateCircumferenceAndArea(double radius, out double circumference, out double area)
{
circumference = 2 * Math.PI * radius;
area = Math.PI * (radius * radius);
}
public void Main()
{
double radiusValue = 3.92781;
//Calculate the circumference and area of a circle, returning the results to Main().
CalculateCircumferenceAndArea(radiusValue, out double circumferenceResult, out var areaResult);
System.Console.WriteLine($"Circumference of a circle with a radius of {radiusValue} is {circumferenceResult}.");
System.Console.WriteLine($"Area of a circle with a radius of {radiusValue} is {areaResult}.");
Console.ReadLine();
}

//The calculation worker method.
public static void CalculateCircumferenceAndArea(double radius, out double circumference, out double area)
{
circumference = 2 * Math.PI * radius;
area = Math.PI * (radius * radius);
}
47 replies
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
47 replies
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
You could also just use a method return value and use a Tuple or an object, but multiple out parameters might be better suited for a method
47 replies
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
You can have multiple parameters with an out keyword
47 replies
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
Faker posts here really often and is very actively learning C#. I know that defensive copies and all that are definitely not what they are looking for on their skill level.
47 replies
CC#
Created by Faker on 2/12/2025 in #help
✅ out vs ref keyword usage
This goes way beyond the purpose of this thread tbh
47 replies
CC#
Created by YetAnohterOne on 2/6/2025 in #help
CA1860: Avoid using 'Enumerable.Any()' extension method
I still think it's silly though. I think most people just forget it exists
59 replies
CC#
Created by YetAnohterOne on 2/6/2025 in #help
CA1860: Avoid using 'Enumerable.Any()' extension method
Won't be a property then, though
59 replies
CC#
Created by YetAnohterOne on 2/6/2025 in #help
CA1860: Avoid using 'Enumerable.Any()' extension method
Otherwise I suppose maybe you can add an extension method on the interface that adds it (the IsEmpty)
59 replies
CC#
Created by YetAnohterOne on 2/6/2025 in #help
CA1860: Avoid using 'Enumerable.Any()' extension method
Pretty sure every collection type with an explicit length implements IList/ICollection, so they'd all have a Count
59 replies
CC#
Created by YetAnohterOne on 2/6/2025 in #help
CA1860: Avoid using 'Enumerable.Any()' extension method
Because length == 0 might not mean it's empty? 🤔
59 replies
CC#
Created by YetAnohterOne on 2/6/2025 in #help
CA1860: Avoid using 'Enumerable.Any()' extension method
But why
59 replies
CC#
Created by YetAnohterOne on 2/6/2025 in #help
CA1860: Avoid using 'Enumerable.Any()' extension method
You basically divide the term into two different things. People check if length is 0, people use this property
59 replies
CC#
Created by YetAnohterOne on 2/6/2025 in #help
CA1860: Avoid using 'Enumerable.Any()' extension method
Even if it was added to a collection it seems entirely pointless since it's just an alias to "length is 0"
59 replies
CC#
Created by YetAnohterOne on 2/6/2025 in #help
CA1860: Avoid using 'Enumerable.Any()' extension method
You know the whole issue is that an IEnumerable does not define the underlying collection, just how it's iterated. It can't be added there, so at that point you have to either find the underlying collection, or attempt to iterate. This is what Any does
59 replies