What do the first few lines of most pieces of C# code mean?

63 viewsOtherTechnology

e.g. “using System;” , “namespace MyApplication” , “class Program” , “static void Main(string\[\] args)” and what classes and namespaces are

In: Technology

5 Answers

Anonymous 0 Comments

[removed]

Anonymous 0 Comments

It’s hard to explain these things while staying ELI5 because what you should be looking for is an introduction to C#.

Classes are like folders where the code is organized in “methods” and “attributes”.

A namespace is like a prefix to the class name. It is used to prevent conflicts. In a huge application with a lot of libraries you will likely end up with two classes of the same name. You use their namespace to differentiate them like so: NamespaceA.MyClass and NamespaceB.MyClass.

If you are only using one version of MyClass and you do not want to type NamespaceA.MyClass everytime, you can add “using NamespaceA” and it will know which namespace to use when referencing MyClass.

“static void Main(string[] args)” is the entry point of your program. When you launch the application this is what your operating system will call first.

Anonymous 0 Comments

`using System` says “there is a library, called **System** that I will be **using** so load it”. in this case the print function is in system

`namespace whatever;` says “to avoid confusion elsewhere, make me a namespace, and put the following in it” a namespace is exactly what it sounds. a space for names. in your daily life you intuitive use namespaces. if I tell you “Will from work” or “Will from highschool” you know they are different Wills. the from tells you the namespace I am using so you don’t get confused about which Will and everyone doesnt need their own unique name.

`class Program` makes a class called program. you will learn more about classes later, for now just know they are re-usable bundles of functions and data that c# requires you to use even if you dont need.

the last line is the program entry point. it defines a function that exact function is what your computer is looking for when you run a c# program. `static` says “take all the cool stuff about classes and stuff it up your ass. this function doesn’t use any of it, basically its just a namespace to me” `void` says “this function isn’t going to return a value” (like int add(int x, int y) is going to return an integer) `Main` is the function name. in this case it has to be `Main` because that is what c# is looking for. `()` says “this is a function” and in between are the arguments to the function. `string[] args` is the 1st argument to main, it is called “args” but could be called whatever you want. it is of type `string[]`. `[]` means it is an array (multiple values in a 1 variable (in an order)) and `string` means its text. when you call a program from command line, that entire command is passed to main in args. so `cp here there` is passed as `[“cp”,”here”,”there”]` to the program `cp` program.

Anonymous 0 Comments

Your code will need to rely on functionalities somebody else built to be any use (even some calculations or display of any kind needs a lot of code). To bypass writing everything from scratch, you reuse somebody elses code, organized into libraries – and that is what usings are, the libraries you want to reuse.

The class line tells your code how your little piece of code is grouped, into a class. A class will be the place you store data but also do stuff, through methods/functions. All of these need to belong somewhere, so your class is the main building block.

Namespace is a group of classes.

Static void main is your method which gets called when the program is executed. Most programs need to have at least the main method, and this going to be your entry point, from where your program can either branch out into more complex stuff or just do simple things and end. Void means it doesn’t give any result, which makes sense because when main itself ends, the program pretty much ends, so there is no use in giving a result.

Anonymous 0 Comments

[deleted]