Comment order

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

what goes first: the comment or attribute:

/// This is a Foo class
[Serializable]
public abstract class Foo: INetSerializable
{


}

Or

[Serializable]
/// This is a Foo class
public abstract class Foo: INetSerializable
{


}

Thanks
 
what goes first: the comment or attribute:

/// This is a Foo class
[Serializable]
public abstract class Foo: INetSerializable {}

Or

[Serializable]
/// This is a Foo class
public abstract class Foo: INetSerializable {}

A quick experiment will show if it makes any difference at all. First
things first, attributes can be have an explicitly specified target, or
their target can be implied by their location in the source code.
Certain targets, such as the assembly, must always be explicitly
specified.

Now, let's make a sample program showing both forms:

===== BEGIN CODE
using System;

public class Entry {
public static void Main() {
decimal a = 2;
decimal b = 3;

Console.WriteLine("{0} / {1} = {2}", a, b, a/b);
ShowResult0(a, b);
ShowResult1(a, b);
}

// form 1
[Obsolete("Don't use this method, use ShowResult1 instead")]
public static void ShowResult0(decimal a, decimal b) {
Console.WriteLine("{0} / {1} = {2}", a, b, a/b);
}

[Obsolete("Don't use this method, use ShowResult0 instead")]
// form 2
public static void ShowResult1(decimal a, decimal b) {
Console.WriteLine("{0} / {1} = {2}", a, b, a/b);
}
}
===== END CODE

Okay, now let's compile it:
Friday, 2008-Oct-17 at 15:13:17 - mbt@zest - Linux v2.6.27
Ubuntu Intrepid:[1-18/6198-0]:test> gmcs decimal.cs 2>&1 |fold -s -w 72
decimal.cs(9,5): warning CS0618: `Entry.ShowResult0(decimal, decimal)'
is obsolete: `Don't use this method, use ShowResult1 instead'
decimal.cs(10,5): warning CS0618: `Entry.ShowResult1(decimal, decimal)'
is obsolete: `Don't use this method, use ShowResult0 instead'
Compilation succeeded - 2 warning(s)

We see that the compiler picked up both. If you compile the code and
then take a look at the IL, you'll see that the attribute is recognized
and placed in both methods.

For clarity, I would do the comment first, and the attribute next, and
the method third. But, that is a matter of preference. The C#
compiler does not care, as long as it can apply the attribute to the
next thing in the source code.

HTH,
Mike

--
My sigfile ran away and is on hiatus.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEAREDAAYFAkj45GQACgkQ0kE/IBnFmjCz0QCdG14Gbd7js0v9ChRC0XJcS1Zk
Tp8An1GsI4KX81LcROWUY9n16cSmIwKW
=rpoY
-----END PGP SIGNATURE-----
 
puzzlecracker said:
what goes first: the comment or attribute:

/// This is a Foo class
[Serializable]
public abstract class Foo: INetSerializable
{


}

Or

[Serializable]
/// This is a Foo class
public abstract class Foo: INetSerializable
{


}

Thanks

I use first.
Having an Attribute just before an XML Comment (the triple slash ones)
like your second example has will prevent automatic folding on that Comment(at
least in my VS2008).

Other then that it should make no difference.
 
Back
Top