1

I know 2 places where the class keyword is used in c#.

  1. In class declaration
  2. In generic constrains (class GenericClass where T : class)

But is there anything I've missed? It's importan to me, becouse I'm writing a program wich counts classes in a file. Can find no info in msdn or C# specification.

Edit

#if class
partial class C<T> where T : class
{
    // class!
    int @class = 123;
    string c = "class";
}
partial class C<T> where T : class {}
#else
partial class D {}
partial class D {}
partial class D {}
#endif

This is accumulation of several answers.

er-v
  • 4,405
  • 3
  • 30
  • 37
  • 2
    "class" is also usable as variable names (@class) as well as in string which I'm sure you already know. – Joshua Enfield May 01 '12 at 22:30
  • 2
    You would get a more reliable count by using reflection against the assemblies, but this might be what you are looking for if you are analysing the source. – benPearce May 01 '12 at 22:33
  • 3
    You could use the visual studio extension APIs to count classes. It's probably better at it than you are. – Sam Axe May 01 '12 at 22:34
  • 2
    What kind of "file"? A .cs source file? A compiled DLL? – Dour High Arch May 01 '12 at 22:35
  • @benPearce This is good solution, that I would use in real life, but this program will be tested on realy big files (1gb), so reflection will not handle compiled assembly – er-v May 01 '12 at 22:36
  • If he is counting a single file, reflection/APIs won't help him much. His only garaunteed way of counting them in a single file is to build a lightweight parser. – Joshua Enfield May 01 '12 at 22:36
  • 2
    May be you have some partial classes and this causes to counting them two times. – Saeed Amiri May 01 '12 at 22:43
  • @er-v Why is it you think just because the file is >=1GB that reflection or codeDOM won't handle it? – Joshua Enfield May 01 '12 at 22:50
  • @JoshuaEnfield actually you right, the size of sourse file does not means anything, but number of types does. When I call GetTypes for assembly with 1 000 000 classes I get StackOverflowException. – er-v May 01 '12 at 22:54
  • Have you considered [Roslyn](http://msdn.microsoft.com/en-us/roslyn)? – Brian May 02 '12 at 13:06

6 Answers6

11

Just counting occurences of "class" does not tell you how many classes are in a file:

#if FOO
partial class C 
{
    // class!
    int @class = 123;
    string c = "class";
}
partial class C {}
#else
partial class D {}
partial class D {}
partial class D {}
#endif

The string "class" appears eight times in there, but the file only defines a single class.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • @EricLippert Well, the question is not about how to solve the problem. Partial classes is not a big problem. I can use stack and build names depends on namespaces, classes and nested classes. Is there any other places where keyword class can be used in C#? – er-v May 01 '12 at 23:07
4

You might want to use something like ANTLR ( http://www.antlr.org/ ) and use for example something similar to this SO post Partial grammar for counting class count . I think it is safer than trying to write your own parser.

Community
  • 1
  • 1
Styxxy
  • 7,462
  • 3
  • 40
  • 45
3

It's also possible to use class as an identifier by preceding it with an "at" sign.

int @class = 1;

I'm not saying it's advisable to write that, but you might want to consider it if you're trying to count classes.

Related

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 2
    finally i know how to give my variables the name `@var` and `@param`! – thumbmunkeys May 01 '12 at 22:31
  • 1
    I know it, but keyword class is not used here. Just the _word_ class is used ) – er-v May 01 '12 at 22:33
  • Note that this is not only for variables but for any identifier. – Stilgar May 01 '12 at 22:34
  • @er-v: What else are you expecting? The two that you have listed are the _only_ two real uses of the keyword. Look in the spec... look in the documentation... it's all there. – Jeff Mercado May 01 '12 at 22:37
  • @JeffMercado Actually there is no place in documentation where it's clearly stated, that this is only real uses. I'm expecting that this the is only, but I can't be shure. – er-v May 01 '12 at 22:46
  • 1
    @er-v: Yes, it is. Read the grammar that is *fully* specified in the, wouldn't you know, specification. – jason May 02 '12 at 00:17
  • @pivotnig: Not sure about `param` off the top of my head, but you can already give your variables the name `var` (but please don't). You can even have a class named var, but future users of your code will hate you. Code like `var var = new var {q = "3"};` is legal if you have a `var` class. – Brian May 02 '12 at 13:09
  • @Brian thanks, you're right, `param` and `var` work as any other identifier without the @... TIL: something useless :) – thumbmunkeys May 02 '12 at 13:58
1

It seems that you can have class defined as a compilation conditional symbol, e.g.

#if class
#else
#endif

...and it compiles if you define it in the project properties / build.

NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
0

Can't think of any other usage but remember that you can use keywords as identifiers if they are prefixed with @

Stilgar
  • 22,354
  • 14
  • 64
  • 101
0

It's possible to use the keyword class in a generic method definition:

foo<T>(T object) where T:class

Use object, if you want to be able to pass y as anything:

foo(string x, class y) { }
Lion
  • 18,729
  • 22
  • 80
  • 110