Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

extension method in c#

class Program
    {
        static void Main(string[] args)
        {
            string post = "This is supposed to be a very long blog post blah blah blah...";
            var shortenedPost = post.Shorten(5);
 			Console.WriteLine(shortenedPost);
          
           IEnumerable<int> numbers = new List<int>() { 1, 5, 3, 10, 2, 18 };
           var max = numbers.Max();
           Console.WriteLine(max);
           Console.ReadLine();
        }
    }
public static class StringExtensions
    {
        public static string Shorten(this String str, int numberOfWords)
        {
            if (numberOfWords < 0)
                throw new ArgumentOutOfRangeException("numberOfWords should be greater than or equal to 0.");

            if (numberOfWords == 0)
                return "";

            var words = str.Split(' ');

            if (words.Length <= numberOfWords)
                return str;

            return string.Join(" ", words.Take(numberOfWords)) + "...";
        }
    }
Comment

Extension method C#

public static void ExtensionMethodName(this ClassToExtend o)
{
    //Do stuff
}
Comment

extension method c#

// list of useful extension methods
https://www.extensionmethod.net/csharp
https://csharp-extension.com/
Comment

PREVIOUS NEXT
Code Example
Csharp :: transform.lookat 2d 
Csharp :: dapper extension 
Csharp :: c # 
Csharp :: camera is rendering black screen unity 
Csharp :: Task timed out after 10.02 seconds 
Csharp :: if exist request c# 
Csharp :: deploy c# applications on ubuntu 
Csharp :: entity framework linq join 2 tables c# 
Csharp :: how to system func bool unity 
Csharp :: how to extract data from a document using c# 
Csharp :: .net core 3 entity framework constraint code first image field 
Csharp :: how to edit a c# list 
Csharp :: c# aabb box rotate 
Csharp :: c sharp right rotation 
Csharp :: générer un nombre aléatoire en c# 
Csharp :: convert string csv line to list c# 
Csharp :: serenity get id from insert repository 
Csharp :: unity soundclip mix 
Csharp :: datagridview mouse click event c# 
Csharp :: == vs equals c# 
Csharp :: user control equivalent event for form.shown c# 
Csharp :: when should i use struct rather than class in c# 
Csharp :: "??" in C# 
Csharp :: add css class based on model value razor 
Csharp :: tuple parameter name 
Csharp :: c# use readonly array as method default 
Csharp :: c# gridview summary item displayformat 
Csharp :: insert keys automatically dictionary in c# 
Csharp :: c# enum variable set to nonthing 
Csharp :: how to save in mongo different name field than model? c# 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =