Func Type and Extension Methods in C#

Here is a sample code Func is useful and Extension Methods.

using System;

class MainClass {
  public static void Main (string[] args) {
   
    var Gouko = new Hero();
    Gouko.Name = "Gouko";
    Gouko.Energy = 100;
    Gouko.LevelUp = x => x.RashingGun().RayGun().KamihamiWave();
    var powerLevel = Gouko.LevelUp(10);
    Console.WriteLine(powerLevel);
   
    var Naruto = new Hero();
    Naruto.Name = "Naruto";
    Naruto.Energy = 50;
    Naruto.LevelUp = x => {
      var z = 5;
      return z * x;
    };
    var naruloLevel = Naruto.LevelUp(5);
    Console.WriteLine(naruloLevel);
  }
 
}

public interface IHero{
}

public class Hero : IHero
{
  public string Name { get; set; }
  public int Energy{ get; set; }
  public Func<int,int> LevelUp { get; set; }
 
}
 
public static class HeroPipe{
  public static int RashingGun(this int power){
    return power * 2;
  }
  public static int RayGun(this int power){
    return power * 10;
  }
  public static int KamihamiWave(this int power){
    return power * 100;
  }
}

Play with the code: Repl.it

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *