C
C#12h ago
Holy

Classes in C#

Hello, currently learning C# through the microsoft developer course on youtube. I don't have an intuitive idea of what classes are exactly. For example if I do System.Console.WriteLine(), is System a bunch of System related "classes", and Console is a class which contains different methods related to the console? So basically like a container? When I look for a definition on google it says a class is a blueprint or a template. In this case is WriteLine a method of an instance of Console?
19 Replies
Angius
Angius12h ago
Yes to the second paragraph
Jimmacle
Jimmacle12h ago
couple of things: System is a namespace, which is just a container to logically organize related type definitions (like a folder on your filesystem) Console is a class, but WriteLine is a static method which means you don't need an instance of Console to call it
Angius
Angius12h ago
Far as WriteLine() goes, that is a static method, which does not need an instance
Holy
HolyOP12h ago
i see that makes sense, so kind of like an associated method in Rust. so what is an example of an instance of Console?
Jimmacle
Jimmacle12h ago
none, it's a static class so you are not allowed to instantiate it but in general
var myVariable = new MyClass();
var myVariable = new MyClass();
myVariable now contains an instance of MyClass
Angius
Angius12h ago
Static classes are basically containers for methods, since there are no "free" functions in C#
Jimmacle
Jimmacle12h ago
all classes may have static members, but static classes can only have static members (and you can't create instances of them)
Thinker
Thinker12h ago
Basically a Ruat module which can only contain free functions
Holy
HolyOP12h ago
ok thanks, slightly got my head around that. And what is the correct name for user defined classes? Like if you made a Car class or Person class? Haven't got round to OOP so maybe will learn about that soon.
Nasdack
Nasdack12h ago
just a class
Jimmacle
Jimmacle12h ago
yeah there aren't many intrinsic pieces of C#/.NET, someone wrote the Console class the same way you'd write any of your classes it's right here https://source.dot.net/#System.Console/System/Console.cs,f907d79481da6ba4
Angius
Angius12h ago
Protip: if you want to check the source code of anything in the docs, they added this nifty cool link not long ago:
No description
Holy
HolyOP11h ago
thanks, I'll be reading the docs extensively. Another thing I'm not understanding is that I can run a simple hello world program with and without a main function. How does that work? Like this works:
C#
using System;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");
}
}
}
C#
using System;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");
}
}
}
and also just this: Console.WriteLine("hello"); also I haven't imported or brought in system into scope yet I can do Console.Writeline
Jimmacle
Jimmacle11h ago
tl;dr magic the second one is "top level statements" and the compiler generates all the stuff in the first one around your code
Pobiega
Pobiega11h ago
That's called "top level statements", and how it works is that the compiler generates the class and main method for you
MODiX
MODiX11h ago
Angius
sharplab.io (click here)
Console.WriteLine("hello");
Console.WriteLine("hello");
Try the /sharplab command! | React with ❌ to remove this embed.
Angius
Angius11h ago
See what the compiler generates Sharplab is another useful tool for stuff like this
Nasdack
Nasdack4h ago
Ctrl + Click on Rider ftw although I think ReSharper on VS also lets u do that

Did you find this page helpful?