Method parameters – overview

Syntax of C# enables multitude of ways how to pass parameters into a method. The most basic way is to provide a parameter type and name. This will enforce that whenever this method is called this parameter must be supplied and the method will produce a result based on that. However this is not the only way, and surprisingly (at least for beginners) C# offers quite wide range of customisation of method signatures.

Optional parameter
A parameter can be optional, and in case it is not passed in a call its default value will be used.

In this case there are two things to be aware of:
i) optional parameters must be at the end, any other parameter cannot be after optional (a compiler will not allow that);
ii) if a method with more than one optional parameter is called the order of them matters, so in the example above if first optional parameter is to be provided, the second is to be omitted the you simply call the method without the last one which will take its default value:

However if one wish to provide only second parameter and use default value of the first optional parameter one must pass it as named parameter:

Reference parameters
In .NET there is distinction between value types and reference types [more]. So generally objects are passed by reference (as they are reference types), primitive types are passed by value (as they are value types, as well as structs). This means, in simple words, that changes a method do to an object will be visible outside. That doesn’t happen to a value type. The ref keyword allows us to declare in method’s signature that a parameter is to be passed by reference (even if it’s a value type).

Let’s consider the above code: if the RefParam() is to be called it will expect a reference to an integer variable which will be modified inside the method and saved to a reference. It will not accept hard-coded value. Note that the ref keyword needs to be present in both: method’s declaration and method’s call.

The above code will output value of n variable which is now increased by the RefParam() method.

There are two more ways to pass a parameter by reference: the in and out keywords. The former enforces that the parameter is not modified inside a method, the latter enforces that the parameter is set inside the method.

So as you can see a compiler will not allow any code that would potentially change value of reference parameter with in keyword. The advantage is that since it is passed by reference it will not be boxed (performance advantage) and you can clearly state at design that this parameter is not to be modified but read-only. Note that the in keyword is available in C# 7.2.

In a method which one parameter is declared as out there must be a statement that would set its value. If not a compiler would generate an error. In this case (similarly as with the in keyowrd) we have this explicit declaration that in the method we need to modify value of a parameter. Another advantage is that we can new-up a variable when a method is called:

This may be used as a way return more that one value by a method [more].

Multiple parameters of the same type
In case we want to pass unknown number of parameters of the same type we can use the params keyword.

If we want the above to work we could declare an overloaded method, however using the params keyword makes it way easier.

However, in this case we need to remember that type of this parameter must be an array (one dimensional, compiler won’t allow anything else) and it must be declared as the last parameter in a method’s signature.

That is short overview how flexible declaring a method can be. I encourage to experiment with it to get comfortable with all the possibilities C# language offers in this matter.

I’m back and Git – rookie’s thoughts

Hello there!
It’s certainly been a while since my last post! It was more or less 6 months ago which is a long time but I wasn’t idle, oh no! If you briefly scan all my posts you’ll notice that my field of interests was Windows apps, mostly WPF technology and MVVM pattern. However this has recently changed – I’ve started ASP.NET app! The project is a web app for acquainted company which purpose is to organise and keep track of service jobs they provide to their client. It is not an open project so I can’t share the code at the moment. Let me say only that it is an MVC app using what comes by default with an MVC template in Visual Studio (Entity Framework, Identity, bootstrap, jQuery etc.) so as you can see a lot of new things!

Here you go a screenshot of the early version of the current app (first version is up and running, but it is very very basic so they have anything to start with). Let me stop here about this project and let’s move on to the next thing which is:

Git – my first thoughts

So, I’ve already been using GitHub (here’s my account) but recently I did a quick course on Git (on LinkedIn Learning [link]) so I thought why not to try it on my own. So I downloaded packages, created separate partition on my hard drive and installed it and started using it. As I’m a beginner I use only some basic commands that you definitely know:

  • git status – shows current changes and what’s been staged
  • git add .  – adds all changes to commit stage, the dot indicates that all files must be added, you could replace it by a specific file name
  • git commit -m”<message>” – commits staged changes to a repository with a message, I generally try to always commit with a message
  • git commit -am”<message>” – adds to stage all changes (except new files) and commits them to a repository
  • git log –x – prints out a log of a commit where x is an ordinal number of a commit (1 – is the newest)
  • git push – pushes (uploads) all commits to a remote repository

And that’s more or less it. That’s enough for me to start.
Now, a word about remote repository. You can use GitHub to host your remote repositories, they offer unlimited number but in a free plan they can only be public repositories (it’s easy to find how to set it all up on their help pages). If you’re looking for a private repo you can simply use your local drive (once you have a directory set up simply run git bash, navigate to this directory and execute git init, or start your project in Visual Studio and tick the Create new Git repository option). However if you still need the remote repository to push your commits (for instance to share the source code with others working with you on a project, or for backup) what you can do is to use Visual Studio Team Services . They offer private repositories. To set a remote address for your repo in VSTS you need to:
a) once you are registered and logged to VSTS you need to create new project with version control set to Git
b) go to where xxx is your alias and click on your project’s name, this will bring you to project’s homepage
c) unfold second category as below
d) execute the above commands in your git bash (of course your URL will differ)

It’s all set! This basically links your local repo with a remote repo at VSTS. In the above command ‘origin’ is a name for the remote repo. If your case is that you use only one remote repository you don’t need to specify it when executing the push command in future.
So, that’s about it what I wanted to say about my first steps with git. Maybe as a side note I’d add that help files that come together with git installation are really really helpful so I encourage to look for solution there in case you encounter any problem.