Jump to content


explaination of what this code means. plz tell me


2 replies to this topic

#1 Tnnspro77

    New Member

  • Members
  • Pip
  • 3 posts

Posted 29 December 2005 - 01:18 AM

Hey i bought a c# programming book and it tells me how to write hello world but it dpesm't explain the code. so can soemone plz help me out.
tell me what public static void main(string[] args)
the code is :


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


thanks !

#2 Onikhaosifix

    Valued Member

  • Members
  • PipPipPip
  • 117 posts

Posted 29 December 2005 - 02:15 AM

If you're following a book then it should have an explanation of what the code is. Also, you didn't read the replies in the other topic you made about how your "Hello World" code won't compile.

#3 moe

    Valued Member

  • Members
  • PipPipPip
  • 270 posts

Posted 29 December 2005 - 02:28 AM

Basically Onikhaosifix is right any half assed book should explain it sufficiently or it was a total miss buy. But I was in the mood of writing something…

It starts with a namespace:

namespace asdf
{
}

This wraps your code into it’s own space. For such small stuff that’s not very useful. In larger projects you can wrap some classes into the same namespace. The namespaces help to avoid ambitious naming. Since C# is purely object oriented I tend to say namespaces are less important than in C/C++. They are mostly useful when creating/using a library. Then you have less risk of using the same names twice. Especially if someone is using the library and doesn’t have access to its implementation and therefore doesn’t see all the names used. Just give a unique namespace to your own code witch uses the library and you’re good to go. So basically namespaces give your code more structure.

class hello
{
}

As C# is purely object oriented you need to create a class. In this case the class is just surrounding the main-function. You could have additional classes of course.

public static void main(string[] args)
{
}

This is your main function. It appears only once in an application (at least it should). It is the entry point for your application. If your app runs, that’s where it starts.

System.Console.WriteLine(“Hello World!”);

Is the part that will be executed. It’s what your application is doing after the main-function is called. Think of it like this:
You tell the system to use the console. Then you write some text into the console.

Btw. It’s possible that your app runs so fast that you won’t see anything. To avoid that you can add a line witch gets some user input. But as I almost never use C# myself I can’t remember how it’s called. Google for it. It should be something like
string input = System.Console.ReadLine();
Then you read a line from the console and assign it to a string. The app will wait until the user/you makes an input and presses enter.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users