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

c# extension

public static class MyClassExtension
{
  // This is the method you are adding to a class "MyClass"
  public static string GetValue(this MyClass myClass)
  {
    return value.toString();
  }
}

// This is how you can use it in other parts of your code
MyClass myClass = new MyClass();
string s = myClass.GetValue();
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 :: 2d look at unity 
Csharp :: c# decimal 
Csharp :: c# int array add number 
Csharp :: c# join array 
Csharp :: set main camera unity 
Csharp :: is it possible to be palindrome 
Csharp :: c# get first word of string 
Csharp :: how to round in c# 
Csharp :: c# String Uppercase and Lowercase method 
Csharp :: unity iOS app rating widget 
Csharp :: c# loop example 
Csharp :: c# if int is even 
Csharp :: unity 2d platformer movement script rigidbody 
Csharp :: c# get regedit value 
Csharp :: function on program stops unity 
Csharp :: C# a program to reverse each word in the given string. 
Csharp :: project programing languages in codecademy 
Html :: google material icons cdn 
Html :: removing filepath from url using htaccess 
Html :: html hello world 
Html :: display html jupyter 
Html :: Javascript getelementbyid hide element 
Html :: lorum picsum 
Html :: how to link your js file to html 
Html :: html text justify 
Html :: font awesome reload icon 
Html :: icons burger css font awesome 
Html :: ckeditor cdn 
Html :: html preselected radio button 
Html :: react html mouseover 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =