tagged [language-design]

Why can't C# member names be the same as the enclosing type name?

Why can't C# member names be the same as the enclosing type name? In C#, the following code doesn't compile: The question is: why? More exactly, I understand that this doesn't compile because (I quote...

7 Nov at 14:1

Why are const parameters not allowed in C#?

Why are const parameters not allowed in C#? It looks strange especially for C++ developers. In C++ we used to mark a parameter as `const` in order to be sure that its state will not be changed in the ...

15 Jan at 12:35

Is C# platform neutral?

Is C# platform neutral? Today I purchased C# 3.0 Pocket Reference (O'Reilly Publishers). In that book in the first para of the first page it is given that "" If I am not wrong, Platform Neutral mean t...

14 Dec at 11:2

C# error when class shares name with namespace

C# error when class shares name with namespace I discovered today that the above gives error `Type name expected but namespace name found`. I find this surprising. As far as I'm aware, you can't decla...

Combined post-operators?

Combined post-operators? We're all familiar with the pre- and post-increment operators, e.g. and the "combined operators" which extend this principle: I've often had a need for a 'post-combined operat...

Has the C# spec (team? committee?) ever considered this object creation syntax?

Has the C# spec (team? committee?) ever considered this object creation syntax? In the interest of keeping everything I care about as close to the left margin as possible, I keep wishing I could write...

What does Eric Lippert mean by "you need to know what the base class is to determine what the base class is"?

What does Eric Lippert mean by "you need to know what the base class is to determine what the base class is"? I just read this interesting article by Eric Lippert, [Top 10 Worst C# Features](http://ww...

19 Aug at 15:13

Case Statement Block Level Declaration Space in C#

Case Statement Block Level Declaration Space in C# Is there a reason I am missing that a block within a case statement isn't considered a block level declaration space? I keep getting an error (variab...

30 Aug at 14:53

How do you force constructor signatures and static methods?

How do you force constructor signatures and static methods? You can't obviously use interfaces for this, and I know that it will have a limited usage. One instance in which I do find it useful is when...

C# - what are the benefits of "partial" classes?

C# - what are the benefits of "partial" classes? I'm asking this because I find it quite a dangerous feature to distribute the class definition so that you can't really be sure if you know all about i...

23 May at 11:51

Why can't an interface implementation return a more specific type?

Why can't an interface implementation return a more specific type? If an interface specifies a property or method to return another interface, why is it not allowed for implementations of the first in...

Why C# won't allow field initializer with non-static fields?

Why C# won't allow field initializer with non-static fields? Why C# will allow this : But won't allow () this I thought that it's the that is guaranteed (with static fields) to be sequential initial...

29 Nov at 21:18

Why aren't C# static class extension methods supported?

Why aren't C# static class extension methods supported? I know from [this question](https://stackoverflow.com/questions/249222/can-i-add-extension-methods-to-an-existing-static-class) that extension m...

23 May at 12:34

Why can't we define a variable inside an if statement?

Why can't we define a variable inside an if statement? Maybe this question has been answered before, but the word `if` occurs so often it's hard to find it. The example doesn't make sense (the express...

2 Jul at 19:52

Why does a collection initializer expression require IEnumerable to be implemented?

Why does a collection initializer expression require IEnumerable to be implemented? Why does this generate a compiler error: ``` class X { public void Add(string str) { Console.WriteLine(str); } } sta...

15 Sep at 10:21

What are the rules for named arguments and why?

What are the rules for named arguments and why? Consider a method like this I like explicitly naming the arguments of methods like this when I call them because it's easy for someone to mix up the `fi...

30 Dec at 20:31

Foreach can throw an InvalidCastException?

Foreach can throw an InvalidCastException? Imagine the following code: I wonder, why is this foreach behavior so... not C#-like? What happens he

31 Oct at 05:36

Why did the C# designers attach three different meanings to the 'using' keyword?

Why did the C# designers attach three different meanings to the 'using' keyword? The `using` keyword has three disparate meanings: 1. type/namespace aliasing 2. namespace import 3. syntactic sugar for...

8 May at 14:38

C#, weird optimization

C#, weird optimization I'm trying to read my compiled C# code. this is my code: But! We all know that a using gets translated to this: ``` { OleDbCommand insertCommand = new OleDbCommand("...", conn...

How does "this" keyword work within a function?

How does "this" keyword work within a function? I just came across an interesting situation in JavaScript. I have a class with a method that defines several objects using object-literal notation. Insi...

Why are private fields private to the type, not the instance?

Why are private fields private to the type, not the instance? In C# (and many other languages) it's perfectly legitimate to access private fields of other instances of the same type. For example: As t...

Is it possible to create C# language modifications as did LINQ?

Is it possible to create C# language modifications as did LINQ? I've been looking quite a bit at [Mr. Skeet's blog on how to re-implement LINQ](http://msmvps.com/blogs/jon_skeet/archive/2011/02/23/rei...

c# switch statement more limited than vb.net 'case'

c# switch statement more limited than vb.net 'case' I was reading an interesting article [here](http://visualstudiomagazine.com/Articles/2011/05/01/pfcov_Csharp-and-VB.aspx?Page=2) and it made an inte...

Why built-in types in C# are language keywords?

Why built-in types in C# are language keywords? In C#, identifiers such as `int` or `string` are actually language level keywords. What is the reason for that? Some clarifications based on answers: 1....

21 Jul at 11:54

Why does C# allow for an abstract class with no abstract members?

Why does C# allow for an abstract class with no abstract members? The C# spec, [section 10.1.1.1](http://msdn.microsoft.com/en-us/library/aa645615.aspx), states: > An abstract class is permitted (but ...

14 Apr at 22:41