C# Help with my first C# Hello World!

Recommended Posts

Hi,

I am currently trying to learn the basics of Unity - most of the decent tutorials I have found imply that you have at least a basic knowledge of C#, so I am busy trying to pick up the basics of that first.

Initially, I just downloaded the free version of Unity which happens to come with 'MonoDevelop-Unity' - so I am using this to write my code and play along with the C# tutorials I have found.

The problems I am having are this (Disclaimer: I am using a Mac for this)

1) I am not sure what the differences between 'running' and 'building' code are. In MonoDevelop-Unity, there is a little play button at the top which seems to cause a little 'Terminal' box to pop up when you press it. The code seems to do it's job and the terminal box returns the following message:

Hello World!
Press any key to continue...

Have I built or run my code?

2) There are several bits of jargon that keep being thrown around, without much in the way of explanation: 'Classes', 'The Main Method', 'Namespace'. These are the main confusing terms - what do they mean and how do they apply to my code?

3) This is the main one, I would be super grateful if someone were able to provide a line-by-line explanation of what each line of this code is doing/means? I wrote it, and it makes a message appear on screen, which is fine - but I want to know what is happening (i.e. which words/parenthesis are doing what)?

Here's my code:

using System;

namespace HelloWorldTwo
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
}
}
}


Console.Writeline (); is presumably telling the machine to "Write whatever is in these brackets () to the console (I assume 'console' is C# lingo for the terminal dialogue box?).

Any feedback here would be greatly appreciated.

Thanks!

Share on other sites

Sometimes it can be counterproductive to learn C# separately from Unity because the way you structure a program in Unity is very idiomatic. So bear that in mind.

I am not sure what the differences between 'running' and 'building' code - Building code prepares it to be run. Running code actually executes the program. Often a button to 'Run' will check to see if it needs building, and will build it if necessary, and then run it. If you are seeing your program's output, it's running.

There are several bits of jargon that keep being thrown around, without much in the way of explanation: 'Classes', 'The Main Method', 'Namespace'. - These are big, complex concepts and you're not going to get explanation from tool-tips or whatever. This is a whole programming language with books dedicated to it. If you don't want to get a book your best bet is to get used to using the online references, for example here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/

In the meantime:

class - a grouping of data with the functions that operate on that data. Objects in your program are usually instances of a class. Each class might have zero, one, or many instances.
main method - the function that gets called at the very start of your program, and that then calls other functions to get everything done.
namespace - a way to group classes together under a related name for ease of use and to avoid naming clashes.

line-by-line explanation - why did you write it if there wasn't an explanation for it? Try finding a tutorial that actually explains what it is telling you do to. Don't get into the habit of typing code from the internet unless someone is explaining what it does.

"using system" - this gives you access to a bunch of pre-defined objects in the 'system' namespace. In this case, the only one you're using is 'console'

"namespace helloworldtwo" - this puts everything inside the next pair of braces into the 'helloworldtwo' namespace. In this code, this actually does nothing special, but if code in other files wanted to access the class or function in here, it would need to prefix it with "helloworldtwo", or use a "using" statement like you did with 'system'.

"class Mainclass" - this denotes the start of a class called 'Mainclass'. The name is arbitrary. A class just groups data and functions (functions are usually called 'methods' when in a class), but in this case there is no data, so it's just holding the function.

"public static void Main (string[] args) - " this starts to define a function. The function is public, meaning any part of your program can access it, static, meaning you don't need a specific instance of that class to use the function, it returns 'void', which is a fancy way of saying 'nothing', and it accepts an array of strings as an argument, and calls them 'args'. Note however that your function currently ignores those strings.

A function is a block of code which takes some arbitrary input, does some stuff with it, and then returns some arbitrary output. In mathematics, an example of a very simple function is "square-root". If you pass 25 into a square root function, you get 5 out. And so on. In programming you can also have statements that cause side-effects - like Console.WriteLine - so your functions can do more than just return values, and this makes it practical to build entire programs out of functions.

"Console.WriteLine ("Hello World!")" - you already figured this bit out.

Share on other sites

Thanks,

I've taken what you said about learning Unity and C# with more integrated tutorials on board, it's certainly simplified things - outside of game development, I still think I'm going to persevere with C# outside of Unity too, as programming is much less intimidating than I initially suspected.

The Unity mixed with C# tutorials also tend to talk through the practical applications of the code, so the explanations of each individual part of the syntax are more forthcoming. It's a bit more rewarding when you get something pretty happening as a result of your code.

Cheers for the rundown!

(FYI, I'm using a Unity basics tutorial by a YouTuber named Brackeys - he seems to be very clear and easy to follow, considering that I know nothing about Unity or programming. Conversely, the tutorial I was using initially encouraged me to pick up C# first, then move onto Unity - hence the baffled original post).

Share on other sites

I would also suggest to stick to the C# Programming Guide (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/) and the C# Reference, @Kylotan was referring to, as the Unity way of coding is rather odd and doesn't follow standard principles. So in short, anything C# related you should consult the Programming Guide first, before trying to find somebody explaining something using Unity3d.

Create an account

Register a new account

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 22
• 9
• 31
• 18
• Forum Statistics

• Total Topics
632617
• Total Posts
3007475
• Who's Online (See full list)

There are no registered users currently online

×