C# & Visual Studio Suggestions

C# is an amazing language. Here are some ways I think C#/Visual studio could be improved to write even more beautiful code. Please go vote for the ones you like, or give me feedback on why not. I know this might be too many changes, or provide too much syntatic sugar or noise, so please help choose the best ones! Some suggestions are still missing voting links since I only have 10 votes, and some of the links lead to others suggestions when I found that the suggestion has already been made.

If you want to contact me directly feel free to do so at jsmars@gmail.com - Jonathan Smars

Visual Studio

Toggle visibility of comments & summary lines

Be able to toggle visibility of full comment lines (//) and summary lines (///) for better code visibility. When these are hidden a one-pixel line in the comment color is shown between the lines. A quick hotkey will toggle all visibility, or hovering over a line will show a tooltip with the comment contents.

Vote/feedback on uservoice.com

Scrollbars to object explorer and better multi dimensional array view

To avoid scrolling forever when looking at big collections. Preferably also a index input box, and the ability to "pin" the whole view so you don't accidentally hover off it when you've found your correct range.

Also a better way to view multi dimensional arrays, possibly a scroll bar for each dimension.

Vote/feedback on uservoice.com

Update all parameter references

When changing parameters in a method, be able to “update all references” of any overriden methods parameters. Would save a lot of manual work.

Vote/feedback on uservoice.com

Update all modifier references

When changing a class or members modifier, be able to update all references.

Vote/feedback on uservoice.com

C# Langauge

Automatic constructor parameter setters

Parameters may be set to members of the same name instead of having to have tons of lines of copying code.

public MyConstructor(this int MyPropertyName)
{
}
View before & after example code Vote/feedback on uservoice.com

Before:

public string Name { get; set; }
public int Age { get; set; }
public int Height { get; set; }

public Person(string Name, int Age, int Height)
{
    this.Name = Name;
    this.Age = Age;
    this.Height = Height;
}

After:

public string Name { get; set; }
public int Age { get; set; }
public int Height { get; set; }

public Person(this string Name, this int Age, this int Height)
{
}

static extension methods

Be able to add functionality to built in classes easily found be other coders on your team.

public static extension Matrix CreateFromUpRight(Vector3 up, Vector3 right)
{
}
Vote/feedback on uservoice.com

Inherit superclass constructor

A syntax to inherit constructor from base class. For easiest implementation, the ability to right-click menu the class and "implement constructor... " with a list of possible constructors, in the same fashion VS suggests simplifications.

public base myClass(stringboolintdouble)

Resolved related error:

Error CS7036 There is no argument given that corresponds to the required formal parameter 'has' of 'baseClass.baseClass(string, bool, int, double)'
View before & after example code Vote/feedback on uservoice.com

Before:

class baseClass
{
    public baseClass(string has, bool lots, int of, double parameters)
    {

    }
}

class myClass : baseClass
{
    myClass(string has, bool lots, int of, double parameters) 
        : base(has, lots, of, parameters)
    {

    }
}

After:

class baseClass
{
    public baseClass(string has, bool lots, int of, double parameters)
    {

    }
}

class myClass : baseClass
{
    public base myClass(stringboolintdouble)
    {

    }
}

"Power of" operator

Makes for better looking code segments when the power of operator is needed, and for use in constants. MSDN Quote: It would be possible to add a power operator to the language, but performing this operation is a fairly rare thing to do in most programs, and it doesn't seem justified to add an operator when calling Math.Pow() is simple.

View before & after example code

Before:

const float radius = 100;
const float distSq = (radius + 100) * (radius + 100);

nodeWorld.DrawDistanceSq = (Section.Size * nodeWorld.LoadLength) * (Section.Size * nodeWorld.LoadLength);

After:

const float radius = 100;
const float distSq = (radius + 100) ^ 2;

nodeWorld.DrawDistanceSq = (Section.Size * nodeWorld.LoadLength) ^ 2;

Implicit static members for static classes

Static classes cannot have non-static members. Don't require static syntax in static classes on any members to avoid tons of redundant code.

static class MyStaticClass
{
    int member;
}

Resolved related error:

‘MyClass.MyMember’: cannot declare instance members in a static class
View before & after example code Vote/feedback on uservoice.com

Before:

static class MyStaticClass
{
    public static int One { get; set; }
    public static int Two { get; set; }
    public static int Three { get; set; }
    public static int Four { get; set; }

    static int number = 5;

    public static void DoSomething()
    {

    }
}

After:

static class MyStaticClass
{
    public int One { get; set; }
    public int Two { get; set; }
    public int Three { get; set; }
    public int Four { get; set; }

    int number = 5;

    public void DoSomething()
    {

    }
}

Enum Functionality

Enum.Length(typeof(MyEnum));

var second = Enum.Next(typeof(MyEnum), MyEnum.First);
var first = Enum.Previous(typeof(MyEnum), MyEnum.Second);
Vote/feedback on uservoice.com

Add operation to automatic property set;

Auto properties can have one line additions to set, making a function call or one operation possible.

public int Something { get; set; } += doSomething();
View before & after example code Vote/feedback on uservoice.com

Before:

public WindowStyle Style
{
    get
    {
        return style;
    }
    set
    {
        style = value;
        updateWindow();
    }
}
private WindowStyle style = WindowStyle.Window;

public bool Resizable
{
    get
    {
        return resizable;
    }
    set
    {
        resizable = value;
        updateWindow();
    }
}
private bool resizable;

After:

public WindowStyle Style { get; set; } = WindowStyle.Window += updateWindow();
public bool Resizable { get; set; } += updateWindow();

Inline pre-processor conditionals

Allow preprocessors to start and end on same lines as code to be able to inline them to avoid lots of lines of code.

#if WIN     public bool MyVar { get; set; } #endif
View before & after example code

Before:

            public bool One { get; set; }
            public bool Two { get; set; }
            public bool Three { get; set; }
            public bool Four { get; set; }

            public bool One { get; set; }
#if WIN
            public bool Two { get; set; }
#endif
            public bool Three { get; set; }
#if WIN
            public bool Four { get; set; }
#endif

            public void Something(
#if WIN
                int winOnlyParameter
#endif
                )
            {

            }

After:

            public bool One { get; set; }
#if WIN     public bool Two { get; set; } #endif
            public bool Three { get; set; }
#if WIN     public bool Four { get; set; } #endif

            public void Something(#if WIN int winOnlyParameter #endif)
            {

            }

break on

One line breaks that do one line of code. Similar to the use of return, but for use when you don't need to return something.

break doSomething();
View before & after example code Vote/feedback on uservoice.com

Before:

public void Check(int var)
{
    if (var == 0)
    {
        doSomething();
        break;
    }
    // something else
}

After:

public void Check(int var)
{
    if (var == 0)
        break doSomething();
    // something else
}

if finally

A clause which is run after any of the conditions in an if tree are true. Note: When used with else, the finally clause will only run if any of the other conditions are true. Sort of like an else-else.

if (something)
    doOne();
else if (something)
    doTwo();
finally
    ifOneOrTwo();
View before & after example code Vote/feedback on uservoice.com

Before:

var done = true;
if (something)
{

}
else if (somethingelse)
{

}
else
    done = false;

if (done)
{
    // do something
}

After:

if (something)
{

}
else if (somethingelse)
{

}
finally
{
    // do something
}

loop finally

A final clause which is run if the loops exits from its own condition, not breaking. Instead of reduntantly checking the i variable. Should work for both foreach and for loops.

foreach (var item in collection)
    // run loop
finally
    // do something
View before & after example code Vote/feedback on uservoice.com

Before:

for (int i = 0; i < 10; i++)
{
    // run loop
    if (i + 1 == 10)
        // do something
}

After:

for (int i = 0; i < 10; i++)
    // run loop
finally
    // do something

return void

Return can take void members if return type is void.

public void DoSomething()
{
    if (something)
        return Something();
}
View before & after example code Vote/feedback on uservoice.com

Before:

public void DoSomething()
{
    if (something)
    {
        Something();
        return;
    }
    // something else
}

After:

public void DoSomething()
{
    if (something)
        return Something();
    // something else
}

returnif

One line returns if an expression is true.

returnif myVar;
View before & after example code Vote/feedback on uservoice.com

Before:

bool check()
{
    if (myVar)
        return true;
    if (something())
        return true;
    return false;
}

After:

bool check()
{
    returnif myVar;
    returnif something();
    return false;
}

returnsame

Auto set property option to return if the old value is the same as the new value.

public WindowStyle Style { getreturnsame set; }
View before & after example code Vote/feedback on uservoice.com

Before:

public WindowStyle Style
{
    get
    {
        return style;
    }
    set
    {
        if (style == value)
            return;
        style = value;
    }
}

After:

public WindowStyle Style { getreturnsame set; }

One-line summary after line

Allow one-line summaries after the deceleration to conserve lines. I know this one is a bit different than most people like to organize their code, but I like to keep the amount of lines to a minimum and keep my list of members as readable as possible in the source file itself. While I'd much rather prefer the visibility toggle of comments/summaries, this could be an alternative.

public int MyProperty { get; set; } /// <summary> A description.. </summary>
View before & after example code

Before:

/// <summary>
/// A description..
/// </summary>
public int MyProperty { get; set; }
/// <summary>
/// A description..
/// </summary>
public int MyOtherProperty { get; set; }

/// <summary>
/// A description..
/// </summary>
public void DoSomething()
{

}

After:

public int MyProperty { get; set; } /// <summary> A description.. </summary>
public int MyOtherProperty { get; set; } /// <summary> A description.. </summary>

public void DoSomething() /// <summary> A description.. </summary>
{

}