Question I have a question about indenting and general editing.

dudeguy

New member
Joined
Mar 8, 2022
Messages
2
Programming Experience
Beginner
Hello I'm super new to coding and C#/VS studio, and I'm learning how to use a text editor. My question is how do I insert a new line (above or below) and not change my lines indention after inserting a new line, using CTRL+ENTER and SHIFT+ENTER. Basically how do i set a permanent indentation for my cursor and lines while adding lines? Any helpful tips on how to organize and write code in an editor.
vs code help 1.png
vs code help 2.png
 
Visual Studio's auto indent is pretty smart as long as you have your curly braces at the right place. And if you don't like it's settings you can go to Tools.Options... and override the indentation and formatting.

Personally, I recommend sticking with the default Allman style indents. C# code should look like C# code, not Java or JavaScript code.

There's not really a need for the vi-like insert blank line above or below current line. Just press Enter to create a new line.
 
thanks for the info, I read about different indention styles, didn't know about them. Do you declare all your variables in the same spot in the code like this?

variable code help.png
 
You can try following the basic C# style guide:
C# Coding Conventions

or the more advanced ones:

but in the end it really goes down to one thing:

“Programs must be written for people to read, and only incidentally for machines to execute.”​

- Harold Abelson

Your primary job as a developer is communicate your ideas to other programmers. Those other programmers might be other people in your team, or if you are working solo -- your future self when you have to go back to maintain the code, or more likely when you have to steal and reuse some code. If you work in a team, by all means follow the conventions of your team. If your team has bad conventions, rally support to change them.
 
Because you would have people starting to write:
C#:
private class Car {
    public const int MAXSPEED = 150;

    private int year;

    public int getYear() {
        return year;
    }

    public void setYear(int newYear) {
        year = newYear;
    }

    public void validateInput(string input) {
        if (input.Equals("GoodValue") {
        }
    }
}

instead of the C# style;
C#:
class Car
{
    public const int MaxSpeed = 150;

    public Year { get; set; }

    public void ValidateInput(string input)
    {
        if (input == "GoodValue")
        {
        }
    }
}

And then next thing you know, you'll have people arranging their files on the file system to look laid out like the way the classes would be laid out within the namespace hierarchy.

And even worse, people get into the Java way of thinking, and they start calling IIS hosts "JVMs" and whenever they are having performance problems with their code, they call you up asking "Can't you just tweak the garbage collection algorithm used for my JVM?" or "Can't you just pass in a different memory setting for my JVM to give it more memory?"

It's like saying that C and C++ have the same language basics. Why not just write C++ code like C code? Although it's true that most C code will compile in a C++ program, there are idiomatic writing style differences and programming approach style differences.
 
Last edited:
Because you would have people starting to write:
C#:
private class Car {
    public const int MAXSPEED = 150;

    private int year;

    public int getYear() {
        return year;
    }

    public void setYear(int newYear) {
        year = newYear;
    }

    public void validateInput(string input) {
        if (input.Equals("GoodValue") {
        }
    }
}

instead of the C# style;
C#:
class Car
{
    public const int MaxSpeed = 150;

    public Year { get; set; }

    public void ValidateInput(string input)
    {
        if (input == "GoodValue")
        {
        }
    }
}

And then next thing you know, you'll have people arranging their files on the file system to look laid out like the way the classes would be laid out within the namespace hierarchy.

And even worse, people get into the Java way of thinking, and they start calling IIS hosts "JVMs" and whenever they are having performance problems with their code, they call you up asking "Can't you just tweak the garbage collection algorithm used for my JVM?" or "Can't you just pass in a different memory setting for my JVM to give it more memory?"

It's like saying that C and C++ have the same language basics. Why not just write C++ code like C code? Although it's true that most C code will compile in a C++ program, there are idiomatic writing style differences and programming approach style differences.
Well, we were discussing indenting, not small language differences between Java and C# or the implementation under the hood. If Java style is to put opening brackets at the end of the line (which I detest) and C# style is to match opening and closing brackets (which I've always done in Java too) then I agree with you. Apart from that there's not a whole lot of difference between these two snippets (and indeed the languages) that would necessitate a style difference. Just my opinion FWIW. I loved Java when it was new and hot, but C# lured me away and now I like C# much better.
 
Back
Top Bottom