CrafterEric

Member
I'm just starting to learn C# specifically and only for the purpose of working on my own personal shard that runs locally. I got the ServUO Repack running with several addons such as runecrafting and tameable wyrms, and I understand enough to do basic editing, like making runecrafting super easy to do, but I really need to learn the language to be able to use it properly. I thought about several different avenues but finally stumbled on a youtube video that teaches the entire language in one course. It covers the entire language in 1.5 hours. I watch like 2 minutes, then practice it. I figure I'll watch Microsoft's Academy after this, and any further help I need I'll get reading. That should get me pretty well started, but I've run into a basic C# coding question. I realize that I'm going to have future questions, but since I'm *only* learning C# so that I can code ServUO, is it appropriate to ask basic coding questions here? Here's the type of thing I'm running into:

At around 7:50 in the video, he's describing how basic math works, and uses this code:

int i = 0;
Console.WriteLine("i++ = " + (i++));
Console.WriteLine("++i = " + (++i));

That produces the following output:

i++ = 0
++i = 2

He says that when you write the operator to the right of the variable, like i++, it shows the variable *then adds* to it, but when writing the operator to the left of the variable, like ++i. that performs the operation *and then* prints it to the screen. My question is just this:

uh, what? :D

Is this just the quickest way to declare a variable, then use it *and* perform a function on it all on the same line?

So a very simple question on something anyone could answer, but is this the best place to look for basic help learning C# since the only reason I'm learning is to work on ServUO? I'm happy to go somewhere else for basic info like this, but I figured since this is the only project I'll ever be working on, maybe I should just take it straight to you.


Thoughts?
 
i++ means "tell me the value of i and then increment it"

++i means "increment i and then tell me the value"

In both cases the variable is incremented, they are pre-increment, post-increment operators. If you take the value of both expressions in exactly the same cases, the result will differ.

the most common used is i++

-Grim

To view this content we will need your consent to set third party cookies.
For more detailed information, see our cookies page.
 
Last edited:
Oh wow, ok cool, thanks Grim, that's easily understandable. It looks like whenever you call a second variable using that operator, either i++ or ++i, that value is not only assigned to the second variable, but also to i, like this:

int i = 1;
int j = ++i;

after compiling, both i and j are equal to 2. Is that right?
 
Yes sins you first add 1 to i then increase the value and send it to j in next step.

Then you starts with loops you can test it and see how it is working.

Code:
class ForLoopTest
{
    static void Main()
    {
        for (int i = 1; i <= 5; i++)
        {
            Console.WriteLine(i);
        }
    }
}
 
Last edited:

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back