C# for Java Developers, Part II: Methods

This is the second part of my C# for Java Developers series. Let's have a look at how to write methods in C#.

Note that this is the second and last post in this series. I’ve decided that there are a lot more interesting languages to learn than C#. The reason I’m finally publishing this second part of the guide - over two years after the first part was published - is that I’m going through my old drafts.

Please consider reading the first part before you jump into this one. All the code you see - and more - is available in the C# for Java Developers GitHub repository. A word of warning: I’m writing this guide as I learn C# myself, so don’t except everything to be 100% accurate. Consider reading this as a way to kick start your own C# adventure.

With that out of the way, let’s have a look at methods.

Methods

Like most of what we’ve looked at so far, methods in C# is pretty damn similar1 to methods in Java.

Java

package net.vegard.csharpforjavadevs.methods;

public class Methods {
    public static int area(int x, int y) {
        return x * y;
    }
}

C#

namespace VegardNet.CSharpForJavaDevelopers.Methods
{
    class Methods
    {
        public static int Area(int x, int y) {
            return x * y;
        }
    }
}

Wrap up

The original plan was to cover methods, access modifiers, strings, and numbers in the second part of the guide. But it turned out that the section about methods got a lot longer than expected. Things seldom turn out the way we plan, am I right? That’s why you shouldn’t use the waterfall model, morons!

Anyway.

With the topics we’ve covered so far, you should be able to write some basic C#. Download Visual Studio, and get started.


  1. But we’ll cover expression body definitions in a later part of this series. ↩︎

  2. I have no idea what this kind of modifiers are called, so “implementation keyword” is my name for them. If you know the actual name, please let me know. ↩︎


Feedback

This post has no feedback yet.

Do you have any thoughts you want to share? A question, maybe? Or is something in this post just plainly wrong? Then please send an e-mail to vegard at vegard dot net with your input. You can also use any of the other points of contact listed on the About page.