Thinker
Thinker
CC#
Created by gönnyd on 3/28/2025 in #help
Switch Expressions
-# granted if you didn't need that I assume you would be using INumber.Parse
17 replies
CC#
Created by gönnyd on 3/28/2025 in #help
Switch Expressions
Could also pass in the type as a generic, unless you really need to be able to pass arbitrary types
17 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ What is a primary constructor in C#
It's somewhat odd, but you don't need to worry about it too much unless you already know what records are.
25 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ What is a primary constructor in C#
in record L(string Z);, Z will become a property, but in class L(string z); you'll get an empty class with just a constructor
25 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ What is a primary constructor in C#
Mind you again that primary constructors in records actually declare properties, which primary constructors in normal classes do not
25 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ What is a primary constructor in C#
Reverse, the property Name is set to the parameter name
25 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ What is a primary constructor in C#
eg. you can do this
class Foo(int num)
{
public void PrintNum()
{
Console.WriteLine(num);
}
}
class Foo(int num)
{
public void PrintNum()
{
Console.WriteLine(num);
}
}
25 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ What is a primary constructor in C#
oh yeah, right, you can reference the parameters from the primary constructor elsewhere in the class
25 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ What is a primary constructor in C#
-# There are some slight nuances, but those are quite fringe.
25 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ What is a primary constructor in C#
If the constructor is public and you don't need any kind of functionality in the constructor besides initializing data, then a primary constructor is the same.
25 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ What is a primary constructor in C#
These two are literally equivalent
class Person(string name, int age)
{
public string Name { get; } = name;
public int Age { get; } = age;
}
class Person(string name, int age)
{
public string Name { get; } = name;
public int Age { get; } = age;
}
class Person
{
public string Name { get; }
public int Age { get; }

public Person(int name, int age)
{
Name = name;
Age = age;
}
}
class Person
{
public string Name { get; }
public int Age { get; }

public Person(int name, int age)
{
Name = name;
Age = age;
}
}
25 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ What is a primary constructor in C#
Yes, primary constructors are literally just a simpler way to write normal constructors.
25 replies
CC#
Created by noname7777 on 3/25/2025 in #help
Array as Public Property; Need to Set Value at Index
And in any case, accessing index 0 on an array or list without any elements will throw an exception.
34 replies
CC#
Created by noname7777 on 3/25/2025 in #help
Array as Public Property; Need to Set Value at Index
(I'm dumb and this wouldn't work because you'd first have to initialize the array)
34 replies
CC#
Created by noname7777 on 3/25/2025 in #help
Array as Public Property; Need to Set Value at Index
Actually you could also do this
GetCapabilitiesRequest caps = new()
{
Category = [CapabilityCategory.All]
};
GetCapabilitiesRequest caps = new()
{
Category = [CapabilityCategory.All]
};
34 replies
CC#
Created by noname7777 on 3/25/2025 in #help
Array as Public Property; Need to Set Value at Index
You can't initialize elements in an array field like that, you'll have to assign it after creation.
GetCapabilitiesRequest caps = new();
caps.Category[0] = CapabilityCategory.All;
GetCapabilitiesRequest caps = new();
caps.Category[0] = CapabilityCategory.All;
34 replies
CC#
Created by Hulkstance on 3/22/2025 in #help
Possible NullReferenceException
It would also require special versions for value types and reference types
28 replies
CC#
Created by Hulkstance on 3/22/2025 in #help
Possible NullReferenceException
There's also an issue to allow nullability to flow through Where iirc
28 replies
CC#
Created by Hulkstance on 3/22/2025 in #help
Possible NullReferenceException
You can very easily add that yourself
28 replies
CC#
Created by Hulkstance on 3/22/2025 in #help
Possible NullReferenceException
You can't, there's no attribute which specifies the kind of flow that the lambda will only execute if the condition is true/false
28 replies