Has anyone come a cross a good explanation of Class, Method, Function, etc or possibly how the C# structure works(not necessarily just a technical document)? I’m just learning C# and I’ve got the basics down so far I think, but now that we are starting to get out of “Main” I’m having a hard time understanding how things relate to each other and how it is structured/how to structure the code. Sorry if i’m asking this in the wrong place. I’m new to reddit too! Thanks!
It would probably be more appropriate to ask on a learning focused subreddit such as r/learncsharp.
That said, there are resources listed in the community information.
If you’re on PC, there are resources listed to the right of the screen.
If you’re on mobile you may have to do some digging based on your app to find the community info, but the data should be there as well.
Look for the Object Oriented Programming principles to help you get started.
Could be some nice stuff here too: https://www.youtube.com/playlist?list=PLLWMQd6PeGY2GVsQZ-u3DPXqwwKW8MkiP
Think of it like jars.
The biggest jar that holds everything is a “namespace”. You’ll usually see one at the top of your file, and it usually has a name similar to the project name and the folder, if any, holding the file. So something like namespace WindowsApplication1
. Many files can say they’re in the same namespace. Like the name implies, this becomes part of the name of the types you create. We don’t usually mess with this, we just tell the IDE how we want it generated. There’s not a concept of “accessing namespaces” in code.
The next-biggest jar is a “class”. This is an actual type, or object, thus it’s the unit of organization we work with the most in C#. If you’re talking about simple console projects, you start with one class: Program
, inside a file named Program.cs
. We usually like our files to be named after the class within them, and only one class per file. Remember: classes are inside a namespace.
Next is “method”, though this could also be “function”. In C# we don’t really say “function”, we call them all “methods”. Some languages use different words. But if you say “function” most people still know what you mean. Methods go inside classes and, in OOP, are the things the class “does”. Code goes inside methods. You can’t really put code anywhere else. (Nitpickers will say code can go in properties, but properties are just lipstick over two methods.
So if you just had a simple C# program, you’d have this familiar looking structure:
You could add a class into another file and it would look like this, preferably in “Example.cs”:
Now back in your “Program.cs” file, you can update the code to use it:
We do this because our programs can get very large, and it helps to put code that does two different things in two different places. Right now, all of your code probably goes in Main()
and it can get quite large. In most large applications, there is very little code in Main()
because its job is just to create the first set of classes the application needs, then call some method that starts the whole thing.
C# devs
null reference exceptions