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 :: how to make a character jump c# 
Csharp :: unity color alpha not working 
Csharp :: int to char c# 
Csharp :: how to know pm or am C# 
Csharp :: How to decode Microsoft Local token in service 
Csharp :: v-slot 
Csharp :: c# check port in remote pc 
Csharp :: unity collapse hierarchy script 
Csharp :: disable alt + f4 in c# forms 
Csharp :: c# on variable change property get set 
Csharp :: unity2d switch camera 
Csharp :: c# signalr console app server example 
Csharp :: unity c# image invisible 
Csharp :: raq query ef core 
Csharp :: vb.net windows version check 
Csharp :: generate a dropdown list from array data using razor .net mvc 
Csharp :: C# random multiple of 5 in range 
Csharp :: .net core get runtime version 
Csharp :: c# copy all files in directory and subdirectories 
Csharp :: SQLite Parameters 
Csharp :: declare prop array c# 
Csharp :: How to execute script in C# 
Csharp :: c# HttpResponseMessage postResponse = client.PostAsync 
Csharp :: c# how to initialize an array 
Csharp :: math.pow in C# using loop 
Csharp :: add to ienumerable 
Csharp :: Entity framwork update parent entity added new sub entity 
Csharp :: Unity Input Key Message 
Csharp :: c# get program version 
Csharp :: c# namespace name form1 could not be found 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =