Unable to call static method in class from another

前端 未结 2 717
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 14:08

I have a class file which contains a function to hash an input string.

using System;
using System.Security.Cryptography;
using System.Linq;
using System.Text;
u         


        
相关标签:
2条回答
  • 2021-01-29 14:24

    Are you trying to do this?

        HashingSystem hs = new HashingSystem();
        hs.sha256("Hello World"); //This wont work as static methods cannot be called via instances
    

    Use the below way instead

        HashingSystem.sha256("Hello world");//Calling directly via class
    
    0 讨论(0)
  • 2021-01-29 14:37

    You need to call static members against the class, not against an instance of a class. So you need to use:

    HashingSystem.sha256("texthere");
    

    Also, consider changing:

    class HashingSystem
    

    to:

    public class HashingSystem
    

    Classes are internal by default. I would recommend you always be explicit about visibility (i.e. always specify internal, public or private).

    0 讨论(0)
提交回复
热议问题