-1

I can imagine the first reaction when you read the title of my question: "How can you have such a high reputation here and ignore what a class is?"

My point is the following: until now I have always worked with C++, Delphi, Java, ... and there it's quite simple: a class is a type definition of an object. You need to reserve some space in memory to start using it (hence the constructor) and afterwards, don't forget to free that memory (if your programming language does not support garbage collection).

Today, however, I had a problem concerning type definitions and constants in C#, and I fell on this URL, mentioning such pieces of source code:

class Calendar1
{
  public const int Months = 12;
}

In order to use this, you just need to do:

using Calendar1;

And you can use Months as a constant.

But here's my question: where's the constructor? If this class is the type definition of an object, which object are we talking about?

So, if I understand correctly, C# is based on the idea "Everything is a class", but in order to make this work, the C# inventors have extended the definition of a class, so now we get (C# definition):

A class is one of the following:

  • a type definition for an object. In that case, a constructor is needed for creating the object.
  • ...

Can somebody finish the definition?

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 7
    I suspect you mean `using static Calendar1;` otherwise it wouldn't compile. There's a constructor supplied by the compiler, but you don't need it because all constants are static - you don't need an instance of `Calendar1` to access `Months`. Is the implicit static nature of `const` all that's confusing you here? – Jon Skeet May 28 '21 at 15:11
  • Maybe [this](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/introduction#members) can help? – Guru Stron May 28 '21 at 15:13
  • You can only do `using static Calendar1` if the class is a static class. What he is doing is fine and very common when working in Entity Framework for dtos and entities. – Jeff B May 28 '21 at 15:15
  • `"A class is a data structure that may contain data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors and static constructors), and nested types."` - from [the C# standard](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes). (I'd add: members may be static or non-static.) – Matthew Watson May 28 '21 at 15:21
  • Does this answer your question? [What is the difference between an interface and a class](https://stackoverflow.com/questions/10914802/what-is-the-difference-between-an-interface-and-a-class-and-why-i-should-use-an) and [What is abstraction](https://stackoverflow.com/questions/58765776/what-is-abstraction-in-c/) and [What is encapsulation](https://stackoverflow.com/questions/58257849/how-to-choose-between-private-and-protected-access-modifier-to-encapsulate-membe/) and [What is polymorphism](https://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used/) –  May 28 '21 at 15:52
  • [What is a class](https://en.wikipedia.org/wiki/Class_(computer_programming)) and [OOP Basic Principles](https://www.c-sharpcorner.com/UploadFile/mkagrahari/introduction-to-object-oriented-programming-concepts-in-C-Sharp/) and [What Are OOP Concepts](https://stackify.com/oops-concepts-in-java/) and [OOP Theory](https://en.wikipedia.org/wiki/Object-oriented_programming) –  May 28 '21 at 15:54

3 Answers3

1

This is a pretty common practice in C#. Classes are often used to create "sacks" to hold constants, or commonly as a entity or dto object. These are usually made without a user defined constructor. If a class does not have a constructor, one is defined at compile time which amounts to an empty constructor:

public Calendar1()
{
}

This answer goes into much further detail: C# class without constructor

Jeff B
  • 975
  • 8
  • 25
1

You don't need this using. using is to make namespaces available.

  • A constant is static. This means that it is not an instance member but a member of the type. Thus, you can access it through the type name: Calendar1.Months or, with a using static Calendar1; just with Months.
  • In C# a class implicitly creates a parameterless public constructor, if you don't declare one explicitly.
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
1

When you are creating a instance of a class you are allocating memory (using the keyword new) Constants are created not in runtime, they are created in compile time and stored in the assembly metadata. So when you are accessing a constant you will be not accessing an instance of a class - you will be accessing the constant from the metadata directly. Have a look at this post: How are C# const members allocated in memory?

Fuffelschmerz
  • 150
  • 1
  • 10