none
Features... RRS feed

  • Question

  • Hello,
    general questions, the procedure.
    To understand the existing classes, objects to understand what is possible.
    Maybe someone can add helpful examples to extend and demonstrate the features.
    Perhaps someone can say something, give concise examples. Thanks in advance for your help.
    Best regards Markus
    -----------------------
    My problem to understand it, what is possible.
    public class Progress<T> : IProgress<T>
    {    
    	public Progress();       
    	public Progress(Action<T> handler);       
    	public event EventHandler<T> ProgressChanged;
    	protected virtual void OnReport(T value);
    }

    I look at the object, what do I have available?

    public Progress(Action<T> handler);       
    //I saw that.
    //Action is void, so I can write this.
      var progress = new Progress<string>(value => sb.Append($"{value}{Environment.NewLine}"));
    or
    	var progress = new Progress<string>(value =>
    	{
    		sb.Append($"{value}{Environment.NewLine}");
    		Console.WriteLine("Test progress!");
    	}
        );

    If I use this, I need not the Eventhandler ProgressChanged, right?

    //////////////////////////////////////////////////////
    Is that right for my understanding?
    Here I define it.

    // Action
    Action<int> printActionDel = i => Console.WriteLine(i);
    printActionDel(10);
    
    // Function   
    Func<int, int, int> add = Sum;
    int result = add(13, 10);
    Console.WriteLine(result);
    
    // or
    Func<int> getRandomNumber = delegate ()
    {
    	Random rnd = new Random();
    	return rnd.Next(1, 100);
    };
    
    // or 
    Func<int> getRandomNumber2 = () => new Random().Next(1, 100);
    
    //Or the best
    Func<int, int, int> Sum2 = (x, y) => x + y;
    
    Func<int, int, int> Sum3 = (x, y) =>
    {
    	Console.WriteLine($"### {x + y}");
    	return x + y;
    };
    
    // Can I assign a function like this?     ##########  
    //Func<int, int, int> dd = (x, y) => Sum();   #### Can I write similar like this…..??
    
    Console.WriteLine(getRandomNumber2());
    Console.WriteLine(Sum2(10, 20));
    int erg = Sum3(10, 20);
    
    Action<string> greet = name =>
    {
    	string greeting = $"Hello {name}!";
    	Console.WriteLine(greeting);
    };
    greet("World");



    Tuesday, February 11, 2020 4:59 PM

Answers

  • Hi,

    The answer is yes, you can call that method in lambda or trinocular operator as you want.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Markus Freitag Monday, February 17, 2020 11:54 AM
    Friday, February 14, 2020 5:55 AM

All replies

  • Hi Markus,

    Thank you for posting here.

    First, events are based on delegate, so we will use them like this.

        public delegate void BoilerLogHandler(string status);
        class Program
        {
            public event BoilerLogHandler BoilerEventLog;
            static void Main(string[] args)
            {
            }
        }

    In the first piece of code you gave, I didn't see the delegate, it should be because you didn't post it.

    Therefore, the following two lines of code are actually not relevant. When you execute the second piece of code, you do not need Eventhandler ProgressChanged.

    public Progress(Action<T> handler);       
    public event EventHandler<T> ProgressChanged;

    For the question in your last piece of code, we can call the method after "=>", but did you forget to pass the parameter?

    Func<int, int, int> dd = (x, y) => Sum();

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 12, 2020 7:26 AM
  • Hello Timon,

    class TimePeriod
    {
    	public const double Epsilon = 1.401298E-45;
    	string Test = "is a test";
    	public string ATest => Test;
    
    // Yes that is it!
    Func<int, int, int> dd = (x, y) => Sum(x,y);
    int resultDD = dd(14, 10);
    Console.WriteLine(resultDD);
    
    TimePeriod test1 = new TimePeriod();
    test1.Seconds = 33;
    string te1 = (test1.ATest.Length == 0) ? "" : test1.ATest;
    TimePeriod retTimePeriod = (test1 == null) ? null : test1; // A
    retTimePeriod = test1 ?? test1;                            // B
    	// Is the (A,B) same, right?
    string te2 = (test1.ATest.Length == 0) ? "" : test1.ATest;

    // Is there a shorter spelling?

    It is difficult to describe the problem, for the experts to find an answer I know it.
    But maybe you can give me some ideas, what is possible, what is a good/clean code.
       
    The new spelling is difficult to read. If I use it more often, as is more common. Is there nowhere a table.
    OLD -- New standard
    Then you can see it, like a formulary.

    // --------------
    >Therefore, the following two lines of code are actually not relevant.
    >When you execute the second piece of code, you do not need Eventhandler ProgressChanged.

    #region Assembly mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1\mscorlib.dll
    #endregion
    
    namespace System
    {
        public class Progress<T> : IProgress<T>
        {
            public Progress();
            public Progress(Action<T> handler);
    		public event EventHandler<T> ProgressChanged;
            protected virtual void OnReport(T value);
        }
    }
    When I use Progress constructor with Action<T> handler,
    I need not the ProgressChanged EventHandler, is the same right?
    This is the class from Microsoft, from framework.

    // -------
    I want to use a property like a function. What is the best way to do this?
    Greetings Markus



    Wednesday, February 12, 2020 5:31 PM
  • Hi Markus,

    Thank you for your feedback.

    In this case, the results of A and B are the same, but neither of them makes much sense.

    A uses the trinary operator, and its structure is this:  bool ? V1 : v2.

    If bool is true, the result is v1, if bool is false, the result is v2.

    B uses a null-coalescing operator with the structure: object1 ?? object2.

    If object1 is not null, the result is object1. If object1 is null, continue to determine object2. 

    There can be multiple null-coalescing operators, and it is determined backwards until find the first object is not null , and the result is that object. If all objects are null, the result is null.

    I don't know if what I said is clear, they are not the same, but in the example you gave, their results are the same.

    Looking back at A and B, they are actually the same as "retTimePeriod = test1".

    For this event in the Progress class, the delegate corresponding to it is here:

    I checked the source code of the Progress class, and that event will be triggered in the method OnReport.

    But I'm afraid I can't explane it very clearly, you can take a look for yourself: Reference Source.

    And for youe last question, use a property like a function.

    Could you explain it? I don't particularly understand what you mean.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, February 13, 2020 7:47 AM

  • And for youe last question, use a property like a function.

    Could you explain it? I don't particularly understand what you mean.



    I mean this.
    It's an big calculation.
    The return value is true or false.
    bool IsPossible = GetCalc()
    Now as property get operator

    Maybe I need only lambda

    bool IsPossible = GetCalc()
    I want only check, is ok or nok.

    Greetings Markus

    Thursday, February 13, 2020 5:47 PM
  • Hi,

    The answer is yes, you can call that method in lambda or trinocular operator as you want.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Markus Freitag Monday, February 17, 2020 11:54 AM
    Friday, February 14, 2020 5:55 AM
  • Hi Timon,

    Fine.

    Do you know a page where I can see the traditional spelling and the new possibilities?

    OLD spelling

    New spelling

     

     

    Best regards Markus


    Friday, February 14, 2020 4:11 PM
  • Hi Markus,

    No, I don't know where such a page is.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 17, 2020 2:05 AM