This message was sent to Shree Mahesh Chand who asked me to post it here:
On many sites including this site(my favourite site), I do not find complete syntax of a NON-GENERIC Class. I feel that it is as follow:
class (Parameters) [: BaseClass, Interfaces]
Whereas many give examples as follows :
public class ManagerData : EmployeeData,
nobody gives and even explains as to how to use class level parameters and the need for them in real life
or how would they help a developer.
i tried to test
public class ManagerData(int intyyyymmdd_Minbirthdate, string strEdunQuali_MinBE) : EmployeeData,
I tried it in Visual studio 2022 but it gives an error saying that this option is under preview.
Kindly guide.
Dilip Nagle
Dilip NaglePosted Oct 12, 2023, 12:26 PM
To
Sam Hobbs,
Prasad Raveendran,
Muhammad Imran Ansari,
Muhammad Tahir Ansari
-------------------------------------
Thank you all for your guidence with vivid examples.
I have understood the way to implement class level parameters.
I am going to store these answers.
With Kind Regards,
Dilip Nagle
e.&o.e.
Sam HobbsPosted Oct 11, 2023, 8:31 PM
In both examples of code there is a comma at the end. I am not aware of any syntax for classes where there would be a comma there. For interfaces a comma is used to separate bases but C# classes do not support multiple bases.
The question asks about non-generic classes therefore the following applies to non-generic classes.
The following is the C# Reference for classes.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/class
That is definitely sparse. Half a century ago I read IBM manuals. IBM language references are much more complete. I am constantly disappointed by Microsoft documentation. At the bottomn of that page, however, is a link to:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/readme
In that is:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes
That is the official specifications showing the exciting details and very many boring details. It is the documentation that a developer of a C# compiler needs to know to accurately recognize C# syntax and semantics. There the complete syntax is:
Note that there is a precise definition of the syntax used for specifying the syntax of the language; the syntax is not C#. And the precise details of all of that (such as attributes and class_modifiers) is provided.
Prasad RaveendranPosted Oct 9, 2023, 7:15 PM
Hello @Muhammad Imran Ansari: There is a feature called Primary Constructor in C# 12. I would request you to take a look
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/primary-constructors
Muhammad Imran AnsariPosted Oct 9, 2023, 4:18 AM
In your above code, constructor parameters should be defined within a constructor, not in the class declaration itself.
As per your question, In C#, class-level parameters typically refer to class-level fields or properties that store values accessible to all instances of the class. These parameters can be used to configure or customize the behavior of class instances, and they are not specific to any particular instance. They are shared among all instances of the class.
You might need Class-Level parameters:
1. Class-level parameters can be used to store configuration settings or constants that are shared among all instances of the class (connection string etc)
2. In some cases, you may want to maintain shared state or counters across instances
3. Class-level parameters can be used to define global constants or default values for the class.
4. They can be used for performance optimization by caching and reusing data or objects that should be shared among instances
Prasad RaveendranPosted Oct 8, 2023, 4:51 PM
In C#, a non-generic class is a class that doesn't have type parameters. Let me break down this definition for better understanding:
Class: A class is a blueprint for creating objects in object-oriented programming. It defines the structure and behavior of objects that can be created based on that class.
Generic Class: C# allows you to define classes with type parameters, which are often referred to as "generic classes." These type parameters allow you to create classes that can work with different data types without specifying them in advance.
Type Parameters: Type parameters are placeholders for data types. They allow you to create classes, methods, or structures that can work with different data types without specifying the data types until you use them. For example,
Listin C# is a generic class, andTis a type parameter that represents the type of elements the list can hold.Non-Generic Class: A non-generic class, on the other hand, is a class that does not have type parameters. It is a class where the data types of its fields, properties, and methods are fixed and do not depend on any placeholder or generic type.
Here's an example to illustrate the difference:
In the example above,
MyGenericClassis a generic class that can work with different data types (Tis a type parameter). In contrast,MyNonGenericClassis a non-generic class with a fixed data type (in this case,int) for its propertyMyProperty.So, in summary, a non-generic class is a class that doesn't have type parameters, and its members (fields, properties, methods) work with specific, predetermined data types.
Use Case: Geometric Shapes
Suppose you are modeling different geometric shapes like circles and rectangles in your application. You want to create a hierarchy of shape classes, with a base non-generic class
Shapeand derived classesCircleandRectangle.In this example:
The
Shapeclass is a non-generic base class that represents a generic geometric shape. It has fields for common properties likewidthandheight, a constructor to set these properties, and methods for calculating the area and displaying information about the shape. Thevirtualkeyword is used to allow derived classes to override these methods.The
Circleclass is a derived class fromShape. It overrides theCalculateAreamethod to provide a specific implementation for calculating the area of a circle.The
Rectangleclass is another derived class fromShape. It also overrides theCalculateAreamethod to provide a specific implementation for calculating the area of a rectangle.Here's how you can use these classes:
This code demonstrates how you can create instances of the
CircleandRectangleclasses, call their methods, and use inheritance to share common properties and behaviors through the non-generic base classShape.Tahir AnsariPosted Oct 7, 2023, 3:23 PM
In this example, the ManagerData class has two class-level parameters: intyyyymmdd_Minbirthdate and strEdunQuali_MinBE. These parameters are used to enforce the following business rules:
All managers must be at least 18 years old.
All managers must have a Bachelor's degr
ee.
To create a new
ManagerDataobject, you must pass in values for theintyyyymmdd_MinbirthdateandstrEdunQuali_MinBEparameters. If the values you pass in do not meet the business rules, the constructor will throw an exception.Class-level parameters are a new feature that is still under development, but they have the potential to make your code more robust, reusable, and generic.
Class-level parameters are a new feature in C# 11 that is currently in preview. This means that it is not yet available in the stable version of C#.
The preview version of C# 11 is available in Visual Studio 2022 Preview and .NET 7.0 Preview. To use class-level parameters, you must create a new project and select the .NET 7.0 Preview framework.