Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / C# - C# help--What do all of these things mean, and where do I use them?

Author
Message
Yodaman Jer
User Banned
Posted: 21st Nov 2009 02:47 Edited at: 24th Nov 2009 05:24
Hello everyone!

I have a few (possibly several) questions about C#. I've been attempting to learn C# for close to a month now, and while I understand some basic things and some of the syntax, I'm finding it difficult to learn just how and where to use commands, and why one would use them, etc,.

For example, let me list the following example:



Okay...so, I know what the writing/user input commands are and how to use them, but what about setting up the "Main()" part of the program? What's the difference between "static void Main()" and "public void/public static void Main()"? How does a person know when, where and why to use either of those three?

And what about something like this? (Copied example)


How does that program know to access the "protected override void OnPaint" function if it's not being called in the Main()? I mean, really? Am I missing something obvious here?

That's really all I've got for now, but I'm sure there's going to be plenty of other questions in the future. I've been trying to learn this for a while now, so any help is GREATLY appreciated!

-CoffeeCoder

Formerly known as Yodaman Jer
NeX the Fairly Fast Ferret
21
Years of Service
User Offline
Joined: 10th Apr 2005
Location: The Fifth Plane of Oblivion
Posted: 21st Nov 2009 13:10
Ah, I think OnPaint is overriding the call which draws the window - so whenever the window is being drawn, OnPaint is being called.

Athlon64 2.7gHz->OC 3.9gHz, 31C, MSi 9500GT->OC 1gHz core/2gHz memory, 48C, 4Gb DDR2 667, 500Gb Seagate + 80Gb Maxtor + 40Gb Maxtor = 620Gb, XP Home
Air cooled, total cost £160
Yodaman Jer
User Banned
Posted: 21st Nov 2009 23:25
That makes sense. "override" would override whatever is above it, or at least, that's what it looks/sounds like it does.

Thanks NeX!

"Life is like a basketball; it has its ups and downs, and it's always controlled by people who are taller and make more money than you."
-Omega Gamer 89
RalphY
21
Years of Service
User Offline
Joined: 6th Sep 2004
Location: 404 (UK)
Posted: 25th Nov 2009 23:13
1) The static void Main() method is the default entry point for C# applications (you can actually have multiple Main methods defined, for unit testing etc, but you have to inform the compiler which to use with /main).

public, protected, private, and internal are access modifiers, they determine from where the method/property/field can be accessed. Public methods can be accessed from any code, protected only from the declaring class and any that inherit it, private only by the class which defines them, and internal by any code within the same assembly. The default for methods in C# is private I believe, so static void Main() is effectively the same as private static void Main() (The Main method is a special case though as it is the access point to your executable).

The static modifier dictates that the method is accessed at the class level instead of the instance level, so instead of having to call Main via:

Test testInstance = new Test();
testInstance.Main();

You instead call:

Test.Main();

From this you can determine that WriteLine() and ReadLine() must both be static methods as they are called via:

Console.WriteLine("Some message!");

Instead of:

Console myConsole = new Console();
myConsole.WriteLine("You're doing it wrong!");

The Main() method has to be static as that is how it is defined in the C# specification. As for when you would use access modifiers/ the static keyword in your own code, that depends on your object model. Typically the static keyword is often used for utility methods.

2) NeX is correct, you will see AForm inherits Form which a quick check on MSDN (http://msdn.microsoft.com/en-us/library/system.windows.forms.form_members.aspx) will show you inherits from Control the virtual method (a virtual method can be overridden in a derived class by using the override keyword) OnPaint which is called whenever the form is to repaint itself.

Go banana! | Super Nintendo Chalmers! | When I grow up I'm going to Bovine University!
Yodaman Jer
User Banned
Posted: 26th Nov 2009 08:51
Thank you RalphY!! That all makes sense now!

Another thing I was originally having trouble with were classes and the idea of object-oriented programming. However, once I realized that classes are to C# what Types are to BASIC, everything became much clearer and I know understand what object-oriented programming is, and why it's preferred in a lot of cases. It just makes sense! Sure, sequential numbers are a good idea, but I think I'll really like using this variant of programming in the long run...

I think the next step for me is to start learning game development with XNA and C#. I saw some examples of a 2D game in C#, and it didn't look as hard as I originally feared things would be. I'm going to tackle that after Thanksgiving.

I like me some coffee.

Login to post a reply

Server time is: 2026-06-10 09:32:11
Your offset time is: 2026-06-10 09:32:11