2

I have add extension method to ArraySegment as follows, but when I using it as

     var lines = TextControl.Lines;
     ArraySegment<String> myArrSegOfRichTextControl = 
                              new ArraySegment<string>(lines,0,2);

I cannot find the visual studio prompt GetSegment method after I type myArrSegOfRichTextControl. So how can I call the method of the extension? Thanks.

namespace ArraySegmentExtension
{
    class ArraySegmentExtension 
    {
        #region ArraySegment related methods

        public static ArraySegment<T> GetSegment<T>(this T[] array, int from, int count)
        {
            return new ArraySegment<T>(array, from, count);
        }

        public static ArraySegment<T> GetSegment<T>(this T[] array, int from)
        {
            return GetSegment(array, from, array.Length - from);
        }

        public static ArraySegment<T> GetSegment<T>(this T[] array)
        {
            return new ArraySegment<T>(array);
        }

        public static IEnumerable<T> AsEnumerable<T>(this ArraySegment<T> arraySegment)
        {
            return arraySegment.Array.Skip(arraySegment.Offset).Take(arraySegment.Count);
        }

        public static T[] ToArray<T>(this ArraySegment<T> arraySegment)
        {
            T[] array = new T[arraySegment.Count];
            Array.Copy(arraySegment.Array, arraySegment.Offset, array, 0, arraySegment.Count);
            return array;
        }

        #endregion
    }
}
1
  • class ArraySegmentExtension should also be a static. It must me public static class ArraySegmentExtension Commented Apr 6, 2013 at 3:51

2 Answers 2

4

Extension methods must be defined on a static class. From the official documentation:

  1. Define a static class to contain the extension method.

Try defining your class like this:

public static class ArraySegmentExtension
{
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

How about adding using ArraySegmentExtension; on top of your code file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.