Talk:C Sharp syntax

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject Microsoft / .NET (Rated B-class, Low-importance)
WikiProject icon This article is within the scope of WikiProject Microsoft, a collaborative effort to improve the coverage of articles relating to Microsoft on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
B-Class article B  This article has been rated as B-Class on the project's quality scale.
 Low  This article has been rated as Low-importance on the project's importance scale.
Taskforce icon
This article is supported by WikiProject .NET (marked as Low-importance).


Quote from the article:

The compiler will automatically replace every T in the code with the type you want to replace it with.

.NET parameterized types are instantiated at runtime rather than by the compiler

 Done. Feel free to point out any more mistakes Aly89 (talk) 23:27, 4 November 2008 (UTC)


  • From article:

This article contains new features of the upcoming C# 3.0. These features are not yet officially released.

Since C# 3.0 has been released already, this paragraph needs to be updated. (talk) 11:40, 19 January 2009 (UTC)


Recently I've been writing a draft for this article. The purpose is to re-write the article to make it an even better one.

If you want to read it or perhaps edit it you'll find it here: User:Sundström/Drafts/C_Sharp_syntax

Contributions are needed and comments are appreciated. Robert Sundström (talk) 11:37, 24 July 2009 (UTC)

Merge proposal[edit]

I have not got any responses from any other user. Do anyone think that we could merge this article with the draft. Is that okay? Robert Sundström (talk) 21:44, 3 August 2009 (UTC)

I think that this article has been abandonned and I have taken the initiative of merging these articles, Robert Sundström (talk) 10:36, 28 August 2009 (UTC)

I believe that the articles are well enough that they should be merged --Azatos (talk) 01:58, 26 September 2009 (UTC)


The article contains no information on the lock keyword. OrangeDog (talk • edits) 22:18, 29 September 2009 (UTC)


The use of "you" (i.e., first person voice) needs to be eliminated from this article. For example, "You can assign a value to a variable" should be rewritten as "A value can be assigned to a variable". — Loadmaster (talk) 23:48, 13 November 2009 (UTC)

Pointer syntax[edit]

In the pointers example, shouldn't the second line read:

   Console.WriteLine("Value of b: {0}. pointed by b: {1}", b, *b);

(I didn't edit myself since I don't know C♯; but the code doesn't correlate with the comment about both lines producing the same value according to C syntax.) Diggory Hardy (talk) 18:19, 18 November 2009 (UTC)

LINQ Syntax[edit]

The article states rather vaguely that the reason 'from' comes before 'select' in the query syntax is "because it seems more natural writing like this in C#."

Now, I've read or heard (can't remember source which is why I didn't edit myself) that the reason for this is simply because IntelliSense would not work unless 'from' was first. Before the 'from' is entered, the editor cannot know the type of the item and cannot assist with popups etc. Can anyone confirm this? --Isaksavo (talk) 17:39, 18 December 2009 (UTC)

The developers behind the LINQ syntax said that it is more convenient for a C-programmer to write a query this way. Correct me if I am wrong because it has been a while since I actually watched that talk. Maybe I should try to find it. --Robert Sundström (talk) 09:11, 5 April 2010 (UTC)

immutable strings?[edit]

C Sharp syntax#string class says:

string class represents an immutable sequence of unicode characters.

Surely string objects can be changed? Mitch Ames (talk) 11:15, 4 April 2010 (UTC)

No. When you perform an operation on a string you will get a new one. It is possible to use the StringBuilder class but the actual type System.String is immutable. Robert Sundström (talk) 09:03, 5 April 2010 (UTC)
OK, thanks. I've removed the {{dubious}} tag. Mitch Ames (talk) 09:56, 20 April 2010 (UTC)

Covariance and contravariance[edit]

[May 2010] I believe the example for contravariance could be improved. Currently, it seems oversimplified: I found it to be misleading at first, and fear that others could misinterpret it worse than I did.

From the article:

For contravariance, the existing interface IComparer<T> has been redefined as follows:

public interface IComparer<in T>
    int Compare(T x, T y);

Therefore, any class that implements IComparer<Base> for some class Base is also considered to be compatible with IComparer<Derived> for all classes and interfaces Derived that are extended from Base. It makes it possible to write code such as:

IComparer<object> objectComparer = GetComparer();
IComparer<string> stringComparer = objectComparer;

If one reads this snippet of code naively, the reader may falsely conclude that the second assignment (i.e. stringComparer = objectComparer) is acceptable regardless of the kind of object that GetComparer() has returned! In that case, the whole point of "what it does and why" would also be missed.

I am not the best qualified to rewrite this section; it should be done by a knowledgeable user of C# 4.0 who also has experience with 3.0 or 2.0. However, here are some recommendations:

  • The contravariance section should illustrate a real usage.
  • The section should be complete without being very long.
The code in the earlier covariance example meets both of these criteria. One might actually expect to find a PrintAll() function, and it is reasonable to define it as working on any enumerable collection of objects, and later to want to call it on a list of strings. The example is complete enough to show what is going on.

  • The example should also show 1-2 lines of erroneous usage.
This helps many readers better understand the limits of what the example is trying to illustrate. (The covariance example could also benefit from such an addition.)

  • The example should discuss or show what happens when the keyword in was not used.
This helps complete the example and shows why the feature was added. (Again, the covariant example could benefit from a similar addition.)

Finally, instead of using IComparer<T>, I propose the example should use either IComparable<T>, or a set of generic delegate classes. Why IComparable<T>? The reader need only understand the concept of comparability (i.e. ‹ › == !=, etc.) in order to grasp enough of the context to make valid assumptions. (With IComparer<T>, the reader must have much more background knowledge in order to understand the context.)

If not IComparable<T>, then why generic delegates? The code examples required to illustrate all of the bullet points above might be shorter or clearer. I'm really not sure.

-- Forbin1 (talk) 19:21, 27 May 2010 (UTC)

Error in continue statement code sample[edit]

The code sample for the continue statement has an error.

The while loop does : 'while (ch = GetChar())' Since ch and GetChar() are of type int, the type of the expression between the parentheses is int. C\# only accept booleans. The compiler reports the following error : "Cannot implicitly convert type 'int' to 'bool'" —Preceding unsigned comment added by (talk) 08:31, 21 August 2010 (UTC)

I've corrected the code (talk) 20:11, 9 January 2014 (UTC)


The code in the Events section would fail with NullReferenceException.

In the Click method, you should check that the event is not null, or initialize the event with anonymous method/lambda expression:

event MouseEventHandler OnClick = (s, e) => { };


"An event requires an accompanied event handler that is made from a special delegate that in a platform specific library"

This is wrong. events can work with any delegate. There is only a convention to use delegates with two parameters: an object that would receive the object that raised the event, and another object that is inherited from "EventArgs" and would receive the event arguments.

It's not even platform specific, but each event declares which delegate it's using.

It's only by convention that programmers should use the "EventHandler" delegate for parameterless events, or the generic "EventHandler<TEventArgs>" delegate for events which needs parameters. (talk) 09:13, 9 January 2014 (UTC)

External links modified[edit]

Hello fellow Wikipedians,

I have just modified one external link on C Sharp syntax. Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:

When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.

You may set the |checked=, on this template, to true or failed to let other editors know you reviewed the change. If you find any errors, please use the tools below to fix them or call an editor by setting |needhelp= to your help request.

  • If you have discovered URLs which were erroneously considered dead by the bot, you can report them with this tool.
  • If you found an error with any archives or the URLs themselves, you can fix them with this tool.

If you are unable to use these tools, you may set |needhelp=<your help request> on this template to request help from an experienced user. Please include details about your problem, to help other editors.

Cheers.—InternetArchiveBot (Report bug) 20:12, 28 July 2017 (UTC)