-
Sep 28th, 2020, 12:03 PM
#1
There Must be a Better Way
I haven't worked much with C#. The language is familiar enough, but using the language in VS 2019 is not. Therefore, I'm way behind the curve on this and figure that I HAVE to be missing a bunch of tips for working with the IDE, because this is painful.
1) I declared a class which had to implement an interface. That's easy enough, put in the class name : interface name. It doesn't automatically implement the interface?!? I have to tell it to do so? Seriously? Under what circumstances would I implement an interface and NOT want the class to implement the interface? Why does it need to be told? What am I missing there?
3) I remember somebody (Techgnome, I think it was) mentioning a quick way to get close brackets, parenthesis, etc, but I don't remember what it was. The formatting that I end up with is not to my liking. I prefer a style like this:
Code:
public bool Dividable
{
get
{
return false;
}
}
Which I can do, but not easily. The IDE prefers to put the } on the same line as the {, and is happy with the {} on the same line as the method name, so there are a couple extra keystrokes to change it. I'm not so concerned with not having the brackets on the same line, but it would certainly not be my preference to do it that way.
Is there a simple stylistic way to get my preferred layout?
If anybody is wondering, there WAS a question #2, and it was FAR more involved and significant than the trivial questions that remain. It was also a style choice, and after taking a deep dive into the options for the IDE, I found one that changed the behavior to the way I want it. That's one of the great things about the IDE=>If your prefer it to be one way, you can generally get it to behave that way. Thus I leave these two questions, and have removed the one that was more complex and had the simpler answer.
My usual boring signature: Nothing
-
Sep 28th, 2020, 03:21 PM
#2
Re: There Must be a Better Way
Originally Posted by Shaggy Hiker
I haven't worked much with C#. The language is familiar enough, but using the language in VS 2019 is not. Therefore, I'm way behind the curve on this and figure that I HAVE to be missing a bunch of tips for working with the IDE, because this is painful.
1) I declared a class which had to implement an interface. That's easy enough, put in the class name : interface name. It doesn't automatically implement the interface?!? I have to tell it to do so? Seriously? Under what circumstances would I implement an interface and NOT want the class to implement the interface? Why does it need to be told? What am I missing there?
3) I remember somebody (Techgnome, I think it was) mentioning a quick way to get close brackets, parenthesis, etc, but I don't remember what it was. The formatting that I end up with is not to my liking. I prefer a style like this:
Code:
public bool Dividable
{
get
{
return false;
}
}
Which I can do, but not easily. The IDE prefers to put the } on the same line as the {, and is happy with the {} on the same line as the method name, so there are a couple extra keystrokes to change it. I'm not so concerned with not having the brackets on the same line, but it would certainly not be my preference to do it that way.
Is there a simple stylistic way to get my preferred layout?
If anybody is wondering, there WAS a question #2, and it was FAR more involved and significant than the trivial questions that remain. It was also a style choice, and after taking a deep dive into the options for the IDE, I found one that changed the behavior to the way I want it. That's one of the great things about the IDE=>If your prefer it to be one way, you can generally get it to behave that way. Thus I leave these two questions, and have removed the one that was more complex and had the simpler answer.
One reason for case 1 is that in C# you can implement an interface explicitly or implicitly and you need to tell it which way, I just get into the habit of hitting Ctrl + . once I have typed the class declaration and specified the interface name and then choose how I want the interface to be implemented.
Not sure about number 3 but perhaps an .editorconfig file might work... https://docs.microsoft.com/en-us/vis...s?view=vs-2019 and https://kent-boogaart.com/blog/edito...r-c-developers might have something useful.
-
Oct 1st, 2020, 09:23 PM
#3
Re: There Must be a Better Way
You should be able to adjust your formatting under Tools > Options > Text Editor > C# > Code Style > Formatting.
-
Oct 5th, 2020, 03:40 PM
#4
Re: There Must be a Better Way
It's worth noting that if you're setting up a getter function in that exact manner, it can be reduced to:
Code:
public bool Dividable() => false;
-
Oct 5th, 2020, 07:07 PM
#5
Re: There Must be a Better Way
Yeah, it will reformat like that, but I prefer the old style.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|