Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6168227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:39:02+00:00 2026-05-23T22:39:02+00:00

A senior member here gave me this code: public static string Truncate(this string value,

  • 0

A senior member here gave me this code:

public static string Truncate(this string value, int maxChars)
{
    return value.Length <= maxChars ? value : value.Substring(0, maxChars) + " ..";
}

He said to use it as an extension method. But where do I put this method? It looks like it adds something to .Net

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:39:03+00:00Added an answer on May 23, 2026 at 10:39 pm

    Consider a class named StringExtensions like so:

    static class StringExtensions
    {
        public static string Truncate(this string value, int maxChars)
        {
            return value.Length <= maxChars ? 
                   value : 
                   value.Substring(0, maxChars) + " ..";
        }
    }
    

    Be sure that whatever namespace you put this class in, you include a using declaration for that namespace.

    Thus, for a full example:

    StringExtensions.cs:

    namespace My.Extensions
    {
        static class StringExtensions
        {
            public static string Truncate(this string value, int maxChars)
            {       
                return value.Length <= maxChars ?
                       value :
                       value.Substring(0, maxChars) + " ..";
            }
        }
    }
    

    Program.cs:

    using System;
    using My.Extensions;
    
    namespace My.Program
    {
        static class Program
        {
            static void Main(string[] args)
            {
                string s = "Hello, World";
                string t = s.Truncate(5);
                Console.WriteLine(s);
                Console.WriteLine(t);
            }
        }
    }
    

    By the way, you are not adding it to .NET. You are not even adding a new method to the class String. Rather, it’s a compiler trick that makes static methods living in static classes with their first parameter declared as this *TypeName* *valueParameter* where *TypeName* is the name of a type, and *valueParameter* is the name of the parameter can be made to appear as an instance method on instances of the type with type name *TypeName*. That is

    string t = s.Truncate(5);
    

    is translated by the compiler into

    string t = StringExtensions.Truncate(s, 5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently had my code reviewed by a senior developer in my company. He
This blogpost and one of our senior developers at work believe that web development
During a code review, a senior dev commented on some nesting I had going
I want to do something like this: @Stateless @Path(/sensors/{sensorid}/version) @Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML}) @Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML}) public
A senior developer at my workstation codes like this: table {border-collapse: collapse;border-spacing: 0;} fieldset,img
I am a senior level developer but I haven't had a lot of formal
I am trying to convince some senior people at my company of the advantages
I've been working on a senior project for the last several months now, and
When I hire developers for general mid-to-senior web app development positions, I generally expect
Another chapter from the arguments between myself and the other senior developer series :P

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.