C# - Function parentheses

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
smithmrkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
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-
0
smithmrkAuthor 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
0
it_saigeDeveloperCommented:
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-
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

AndyAinscowFreelance programmer / ConsultantCommented:
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?
0
smithmrkAuthor 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
0
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0
smithmrkAuthor 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
0
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smithmrkAuthor Commented:
Thanks for everyone's help!

Mark
0
it_saigeDeveloperCommented:
@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-
0
smithmrkAuthor Commented:
Thanks Saige!

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

Mark
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.