namespace ConsoleApplication1
{
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
class Program
{
static void Main(string[] args)
{
string s = "1.2.3";
Console.WriteLine( s.WordCount().ToString());
Console.ReadKey();
}
}
}
23 August 2017
Class extension example
Fancy LINQ operation
class Myclass
{
public string name { get; set; }
public decimal age { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<string> l = new List<string>();
l.Add("di"); l.Add("marks");
var list = new List<Myclass> { new Myclass { name = "di",age = 0 }, new Myclass { name = "marks", age = 0 }, new Myclass { name = "grade", age = 0 } };
list.Where(w => l.Contains(w.name)).ToList().ForEach(i => i.age = 10);
list.ForEach(i => Console.WriteLine(i.name + ":" + i.age));
Console.ReadKey();
}
}
Subscribe to:
Posts (Atom)