Clint
Clint
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
public string AskUser(string message, ? validator, string failureMessage) {
var value = Console.ReadLine(message);
if (!validator(value)) {
Console.WriteLine(failureMessage);
}

// code to go around while we don't have a valid value not included
}
public string AskUser(string message, ? validator, string failureMessage) {
var value = Console.ReadLine(message);
if (!validator(value)) {
Console.WriteLine(failureMessage);
}

// code to go around while we don't have a valid value not included
}
That's more like what we're aiming for, yes? Where ? is the delegate to perform the validation check.
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
Oh okay, I see the confusion here, your ask methods are returning string but we're getting tripped up on the validation part
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
so close
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
public bool ValidateLastName(string value) {
// return some validation pass / fail result
}
public bool ValidateLastName(string value) {
// return some validation pass / fail result
}
Take the types of the return value and the arguments, what do you think the delegate signature looks like for that?
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
And if it helps, think of:
delegate ? ValidatorDelegate(? input);
delegate ? ValidatorDelegate(? input);
Like the way you'd write a method signature, just with an extra keyword at the start, and NO implementation body.
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
What if take a step back? Merineth what problem is you're trying to solve? Perhaps we can tackle from first principles and slot the delegates in gradually?
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
And they show up all over the place in the core library
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
Pobiega is right, multitude of uses
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
But my offer still stands, we will have you understanding how delegates work cos they're an extremely useful tool!
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
Ah I wasn't aware, apologies.
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
No worries if not though, no problem.
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
Just letting you know that I'm available if you want options and alternative ways of going through this, I think it's great you're looking to learn this, but appreciate that a busy chat window isn't always the best forum for it πŸ™‚
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
Gives some code to try and then watch what it gets up to, hard to explain the concept without involving the type signatures of what's going on.
322 replies
CC#
Created by Merineth πŸ‡ΈπŸ‡ͺ on 10/11/2024 in #help
βœ… Can someone explain Delegates to me like i'm 5?
Delegates are pointers to a function, it's just a way of passing the location of a function (and its signature) around. Let's say you have a list of integers and a function that squares integers and you want to end up with a list of squared integers.
int Square(int num) {
return num ^ 2;
}

List<int> SquareIntsTheOldWay(List<int> ints) {
var result = new List<int>();

foreach(var num in ints) {
result.Add(Square(num));
}

return result;
}

List<int> DoSomethingToInts(List<int> nums, Func<int, int> something) {
var result = new List<int>();
foreach(var num in nums) {
result.Add(something(num));
}
}
int Square(int num) {
return num ^ 2;
}

List<int> SquareIntsTheOldWay(List<int> ints) {
var result = new List<int>();

foreach(var num in ints) {
result.Add(Square(num));
}

return result;
}

List<int> DoSomethingToInts(List<int> nums, Func<int, int> something) {
var result = new List<int>();
foreach(var num in nums) {
result.Add(something(num));
}
}
Then going further you could expand out to a generic use case:
IEnumerable<T> DoSomethingToInts(IEnumerable<int> nums, Func<int, T> something) {
foreach(var num in nums) {
yield return something(num);
}
}
IEnumerable<T> DoSomethingToInts(IEnumerable<int> nums, Func<int, T> something) {
foreach(var num in nums) {
yield return something(num);
}
}
Which you could then call do to all sorts of things!
var Squared = DoSomethingToInts(myInts, (i) => i ^ 2);

var toString = DoSomethingToInts(myInts, (i) => i.ToString());
var Squared = DoSomethingToInts(myInts, (i) => i ^ 2);

var toString = DoSomethingToInts(myInts, (i) => i.ToString());
And so on so forth. This is (basically) what LINQ's Select method is doing:
var squared = myInts.Select((i) => i ^ 2);
var squared = myInts.Select((i) => i ^ 2);
322 replies
CC#
Created by Ownera on 10/10/2024 in #help
Just learned generics...
This way you could do something like:
public void SendEmailTo(IHasEmail person, string subject, string body)
{
var address = person.Email;
// do something else here
}
public void SendEmailTo(IHasEmail person, string subject, string body)
{
var address = person.Email;
// do something else here
}
Generics would be useful if your types were immutable e.g.:
public abstract record Person
{
public string Name { get; init; }
public string Pass { get; init; }
}

public interface IHasEmail
{
public string Email { get; init; }
}

public record User : Person, IHasEmail
{
public string Email { get; init; }
}

public T UpdateEmail<T>(T person, string newEmail) where T : IHasEmail
{
return person with {
Email = newEmail
}
}
public abstract record Person
{
public string Name { get; init; }
public string Pass { get; init; }
}

public interface IHasEmail
{
public string Email { get; init; }
}

public record User : Person, IHasEmail
{
public string Email { get; init; }
}

public T UpdateEmail<T>(T person, string newEmail) where T : IHasEmail
{
return person with {
Email = newEmail
}
}
This might not be 100% as just typing it out here, but you could invoke it like this:
var myUser = new User {
Name = "Name",
Email = "[email protected]",
Pass = "test"
};

var myModifiedUser = UpdateEmail(myUser, "[email protected]");
var myUser = new User {
Name = "Name",
Email = "[email protected]",
Pass = "test"
};

var myModifiedUser = UpdateEmail(myUser, "[email protected]");
27 replies
CC#
Created by Ownera on 10/10/2024 in #help
Just learned generics...
Yeah looks like you'd be better off avoiding generics for a scenario like this, you're probably better off looking at interfaces for something - similar -. E.g.
public abstract class Person {
public string Name { get; set; }
public string Pass { get; set; }
}

public interface IHasEmail {
public string Email { get; set; }
}

public class User : Person, IHasEmail {
public string Email { get; set; }
}
public abstract class Person {
public string Name { get; set; }
public string Pass { get; set; }
}

public interface IHasEmail {
public string Email { get; set; }
}

public class User : Person, IHasEmail {
public string Email { get; set; }
}
27 replies