Create an object reference in its own method

-2

Problem

I want to get the last digits of this string , so I used the Substring() method, however I had to use the variable "numero" again within the Substring() method.

string numero = "123456789";
string final = numero.Substring(numero.Length - 4);
Console.Write(final);

Is there any way to reference the numero variable generically in its own method? Something like:

string final = numero.Substring(reference.Length - 4);
  

I used the word "reference" only as an example, I am aware that   so that it will not work.

Because I want this

As this is very simple to use the variable number, however in the code in which I need to perform this procedure the variable I am using is in a chain of methods and submodes, so the reference leaves the code confusing. As a palliative I did the following:

string numero = referenciaMuitoGrandeBlaBla;
string final = numero.Substring(numero.Length - 4);
Console.Write(final);

It does help, but I do not want to use it this way, as there may be a way to reference it.

    
asked by anonymous 13.12.2018 / 12:57

2 answers

1

Create an extension method to create your custom Substring , within this method you implement whatever logic you want. To create an extension method we need to create a static class and static methods that have the keyword this that will reference our current object, here's an example:

namespace ExemploExtensao
{
    public static class Extensions
    {
        public static string SubstringCustomizado(this string texto, int length)
        {
            //Implemente a lógica que desejar. 
            //this string texto se refere ao objeto que chamar esse método
            return texto.Substring(texto.Length - length);
        }
    }

To use this method, just reference the namespace and call in the same way that you call the Substring method:

using ExemploExtensao    
{
    static void Main(string[] args)
    {
        string teste = "0123456789";

        //Quando chamar seu SubstringCustomizado, a variável teste irá ser o 
        //parâmetro this do método, assim você não precisa passar como 
        //parâmetro conforme sua pergunta
        Console.WriteLine(teste.SubstringCustomizado(4));
    }
}

More information about extension methods you can check in the Microsoft documentation: link

    
13.12.2018 / 14:01
2

In C # 8 you can do this:

Write("123456789"[^4..]);

This is a track ( range ).

Meanwhile you want to have a method called Right() , that is, a method that takes the characters to the right. In fact the only way to do this is to use the variable twice, so to avoid this duplicity in syntax you need to create an abstraction (the lost art of programmers). The most interesting way to do this is to create an extension method for String in System , so in all strings this method will be available as if it were a normal method of type: p>

using System;
using static System.Console;

public class Program {
    public static void Main() {
        WriteLine("123456789".Right(4));
    }
}

namespace System {
    public static class StringExtensions {
        public static string Right(this string text, int length) => text.Substring(text.Length - length);
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

    
17.12.2018 / 11:43