I'm sitting in C# for about 2 days and it's already like lifting 300KG block.
I don't get it, if I Googled how to get SHA512 hash of string in C#, I get many wonderful
variations on how-to. But when I copy paste this:

        public static string GetCrypt(string text)
        {
            string hash = "";
            SHA512 alg = SHA512.Create();
            byte[] result = alg.ComputeHash(Encoding.UTF8.GetBytes(text));
            hash = Encoding.UTF8.GetString(result);
            return hash;
        }

It says that "SHA512" doesn't exist in this context. I don't understand it. I haven't done anything wrong, I just copy pasted it into fresh project and pasted it here.

Now, problem may be that return would work with consoles. But even then when I want to find snippet e.g. "how to get MAC address C# WPF". They give me great code which should message-box me, but nope! Instead everything is either marked green or red, and when I look at error checker.

AND I'M SO TIRED. I thought it would be like PHP or JavaScript. You copy paste snippet, modify where required, play with it, delete it, write your own, learnt. But with C#, you're already taken down on 1st step.

Does anybody have any hints for me, on how should I switch my way of thinking? Program can't be wrong, it's me not having enough knowledge to use it properly. So once again, does anybody have idea on how to think while using Visual Studio 2013 while programming C# for someone who knows only PHP, JavaScript and DOS?

Sorry for spamming you guys, I know you have better things to do, then sit on your chairs and facepalming all the way. But I don't know what's holding me back, so far I know many snippets are already considered an error for "not's sake". Help, please :).

Is there something more I should take in consideration? One-liners "Laws of Reconsider and Remember" in C#?

Recommended Answers

All 9 Replies

Did you receive that error on compiling? Make sure you have the following using statement:

  • using System.Security.Cryptography;

Note: You may have to add "System.Security" as a reference.

System.Security.Cryptography Namespace

Make sure you have the following using statement:

Oh! So I have to include library! I heard from C# tutor online (professional one), that C# compiler is pretty versatile when it comes to finding out problems, so I thought "Hey, if it's that versatile and it will find SHA512 it will probably include itself.".

But now, after I included that one line, SHA512 was undeclared an error and it got it's color. Now, I would just run it as in:

public static string GetCrypt(string text) {
    string hash = "filled";
    SHA512 alg = SHA512.Create();
    byte[] result = alg.ComputeHash(Encoding.UTF8.GetBytes(text));
    hash = Encoding.UTF8.GetString(result);
    return hash;
}
string GetCrypt("halleluyah");

I didn't expect it to work since it's not a terminal, so instead I tried to change return hash; to a message box. MessageBox.Show (hash, "My Application");. But it ain't working. I really need to produce an output. ALSO, string GetCrypt("halleluyah"); the "halleluyah" is marked red because "Type expected". The second error I got is Error 2 'PROGRAMX.MainWindow.GetCrypt(string)': not all code paths return a value.

What is your level of experience? Are you new to programming?

Depends what you mean by new to programming. I did my part of PHP and JavaScript, creating and bugs solving. I have been through 3 hours out of 8 hours of essential training of Lynda.com for C# in Visual Studio 2010 (I've got 2013). I'm not THAT new to programming but I'm kinda fresh to C#.

When you type in code, it hapens often as you type that error messages are generated. The compiler compiles while you are typing.
Most of those errors disapear as your coding becomes completed.
Errors that stay often dissapear also by building your solution.
The errors that stay after this action are the real errors.

commented: Thanks. It's "Hint #1". +2

The errors that stay after this action are the real errors.

So if I debug (Start from panel above), and I move to editor, the red underlined ones are errors and the rest are normals?

Do you mean runtime errors? Then yes I guess so. You cannot run your program any way before all the errors inyour editingare resolved.

In Visual Studio, you will save yourself some headaches if you build the program first before clicking "Start Debugging".

  • Click Build (in menu)
  • Select Rebuild <project name> (or Rebuild Solution)

Then, run the program in the debugger:

  • Click "Debug"
  • Select "Start Debugging"

It isn't required, but when you have errors, it makes it easier to figure out if it is a compiler error or a runtime error.

Additionally, you may be interested in changing some of the options. Not sure what version you are using but likely have some similar options. In 2010,

To turn on line numbers:

  • Click "Tools" (in menu)
  • Select "Options"
  • Expand "Text Editor"
  • Click "All Languages"
  • Check "Line numbers"

Then you can use "Ctrl-G" to jump to a line number.

To change default project location:

  • Click "Tools" (in menu)
  • Select "Options"
  • Click "Projects and Solutions"
  • Change value of "Projects location"

@cgeier Thanks I found it out already.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.