We help IT Professionals succeed at work.

C# - Function parentheses

smithmrk
smithmrk asked
on
OK, so I'm NEW to C#, because I have used VB.net for years.
Question...when I select something like FileWriter.Flush from the .net dropdown why doesn't it automatically put the () at the end like this:  FileWriter.Flush();?

I have to put the () manually on every function...why?
Also after hitting Enter to go to the next line how come it doesn't automatically add the ";" Semi-Colon?

Thanks,
Mark
Comment
Watch Question

it_saigeDeveloper
Distinguished Expert 2019

Commented:
Because of a couple of reasons.

First, not everything that is seperated by the '.' represents a function.  Properties do not require ().  Consider the following:
using System;

namespace EE_Q28627754
{
	class Program
	{
		static void Main(string[] args)
		{
			MyClass awsome = new MyClass();

			Console.WriteLine(awsome.MyFunction());
			Console.WriteLine(awsome.MyProperty);
			Console.ReadLine();
		}
	}

	class MyClass
	{
		public string MyProperty { get { return "My Property Is Awsome..."; } }

		public string MyFunction()
		{
			return "My Function Is Awsome...";
		}
	}
}

Open in new window

Produces the following output -Capture.JPG
Second, unlike VB.NET, C# does not care about whitespace.  Instead, the semi-colon represents the end of an instruction.

If you take the code that I wrote above and did this to it:
using 
System;

namespace 
	EE_Q28627754
{
	class 
		Program
	{
		static 
			void 
			Main
			(
			string
			[
			] 
			args
			)
		{
			MyClass 
				awsome 
				= 
				new 
					MyClass();

			Console.
				WriteLine
				(
				awsome.
				MyFunction()
				);

			Console.
				WriteLine
				(
				awsome.
				MyProperty
				);

			Console.
				ReadLine
				(
				);
		}
	}

	class MyClass
	{
		public 
			string 
			MyProperty 
		{ 
			get 
			{ 
				return 
					"My Property Is Awsome..."; 
			} 
		}

		public 
			string 
			MyFunction
			(
			)
		{
			return 
				"My Function Is Awsome...";
		}
	}
}

Open in new window


It would still compile and run (although, this becomes a tad unreadable).

-saige-

Author

Commented:
OK, I've added a screen shot to help with my question.
So in my screen shot I picked "Close" from the .net list of functions...then I hit enter button.

In VB.net it would automatically put .Close() for me...why doesn't C# put the () at the end of the Close?

Thanks,
Mark
CloseFunction.jpg
it_saigeDeveloper
Distinguished Expert 2019
Commented:
I did not misunderstand what you meant.  I gave you the reasons why you see this functionality.  This is the default IDE functionality and one of the many differences between C# and VB.NET.

In order to change this default functionality, you would have to use a third party extension like ReSharper or CodeRush.

-saige-
AndyAinscowFreelance programmer / Consultant

Commented:
Ask your question another way.
Q. If I use X then it will do x but if I use Y then it behaves differently.  Why?
A. Because X and Y are different - why do you expect them to work the same way?

Author

Commented:
Well I was just hoping to save key strokes and X and Y are both Microsoft so I figured at least some things would be the same.

I guess I will just have to type three more key strokes...
1. (
2. )
3. ;

I just thought it might be some type of setting I needed to turn on in Tools / Options but I guess not.
If you still don't understand my question...start a VB.net application and initialize a StreamWriter (i.e. Dim FileWriter as New StreamWriter("")...then do FileWriter.Close then hit enter it will automatically put the () on the end of the close instead of having me type ().

VB.net Example Steps...
Dim FileWriter As New StreamWriter("")
FileWriter.Close..."now hit enter"

Result = FileWriter.Close()

It is what is, so I guess more typing in C# then VB.

Thanks,
Mark
AndyAinscowFreelance programmer / Consultant

Commented:
The important point as it_saige has already mentioned is that for example the following is perfectly valid code in C#

int i = 3
+ 4
+ 5;

So if the editor put an end of code line (semi-colon) at the end of each line automatically then the above would get butchered and you would have to edit it to make it work - despite having typed in valid code.

Also the following is valid where o is an object that has a property and a function defined in such a way to allow the code to work

int i = o.SomeProperty;
int j = o.SomeFunction();

Note one has a brackets pair and the other doesn't.

Author

Commented:
OK I understand in some situations like your example above is fine...but can you try my example in VB.net so you can see what I'm talking about because this is INVALID in C# but in VB.net when you hit enter after the word close it automatically puts the ()

FileWriter.Close;

I guess I just have to get use to the differences between the two languages and deal with having to type () when needed and leave it off when not needed.  The nice thing about both languages is that at least it puts the red lines underneath to tell you it doesn't like something!

Thanks,
Mark
Freelance programmer / Consultant
Commented:
I understand your position but in C# some things are perfectly valid which are not in VB - hence some more auto text finish support in the VB editor.
You just have to put up with it.  

Would you complain you had to fill your new diesel engined car up at a diesel pump instead of a petrol pump on the basis your old car worked fine with petrol.

Author

Commented:
Thanks for everyone's help!

Mark
it_saigeDeveloper
Distinguished Expert 2019

Commented:
@Mark - I program in multiple languages (VB being one of them), I do *know* exactly what you are talking about.  Understand that each language has it's differences, for C# and VB.NET this is one of many differences.

Here is a good link that points out some other differences:

http://www.harding.edu/fmccown/vbnet_csharp_comparison.html

-saige-

Author

Commented:
Thanks Saige!

You would thing since it is all Microsoft it would act the same across all languages.

Mark