How to change Java’s final property to C#?

Asked

Viewed 117 times

2

Which property in C# corresponds to final Java, I need to change my variables final String mensagem = "Tokio Marine Seguradora S.A."; to something corresponding in C#.

using System;

namespace ConsoleApplication
{
    public sealed class Criptografia
    {
        private static readonly String ALGORITMO = "AES";

        public static String descriptografar(String mensagem, String chave) 
        {

          //  final Cipher cipher = getCipher(Cipher.DECRYPT_MODE, chave);

          //  final byte[]
          //  descriptografado = cipher.doFinal(Base64.decodeBase64(mensagem));

                //return new String(descriptografado, "UTF-8");

            return "";
        }

        public static void Main(string[] args)
        {
            // Mensagem que sera criptografada.
            final String mensagem = "Tokio Marine Seguradora S.A.";

            // Senha definida da operadora.
            final String chave = "JessicaBiel";

            //// Valor criptografado.
            //String criptografado = Criptografia.criptografar(mensagem, chave);
            //Console.Write("Valor criptografado: '%s' %n", criptografado);

            //// Valor original.
            //String descriptografado = Criptografia.descriptografar(criptografado, chave);
            //Console.Write("Valor descriptografado: '%s'", descriptografado);

            Console.ReadKey();
        }
    }
}
  • http://stackoverflow.com/a/1327549/5524514

  • 2

    Usa const for example private const String Mensagem = xxxxx

  • @Marcogiovanni const should not be used to it, see the link in my reply comparing the mechanisms.

  • @bigown didn’t know that difference, thank you!

  • 1

    @Marcogiovanni actually until you’re right, I had not attacked me for the right question, in that context is const even.

1 answer

6


In the context of the question is const, but in fact it is not usually useful and practically never seen being used. " Constants" within a local scope help very little (slightly helps in performance).

Other contexts

In class or instance variables is the readonly which equals to final java.

If it were in a class, the equivalent would be sealed.

In methods the default C# method is not virtual, so you don’t need it. Unless you want to prevent virtuality in a tree where the method was already virtual, then the use should be sealed also, next to the override.

Extra

Ideally it would be good to convert idiomatically. It gets weird to have a Java style in C#code. And it is easy to end up making some mistake so.

See more: What is the difference between const and readonly?

  • Try this but gave error. The Modifier 'readonly' is not Valid

  • Then the question is already different, would have to see your code, the exact error.

  • Severity Code Description Project File Line Suppression State Error CS0106 The Modifier 'readonly' is not Valid for this item Consoleapplication C: Work stackoverflow Consoleapplication Program.Cs 25

  • I changed the question with the full code, inside the man, I am creating my variables, and end that is java by a C correspondent#.

  • Yeah, I had not attempted to use myself within the method, I changed the answer to that now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.