In c# how to prevent a class from being modified

SerenityNow

Let's say I have an interface:

interface IPerson
{
   int Age { get; set; }
   string Name { get; set; }
   bool Alive { get; set; }
}

and a Class:

public class Person : IPerson
{
   public int Age { get; set; }
   public string Name { get; set; }
}

That would not compile since Person does not implement the Alive Property.

What I would like to know is if there is a way to have the same behaviour, if Person adds an extra property that is not found in its interface.

interface IPerson
{
   int Age { get; set; }
   string Name { get; set; }
}

and a Class:

public class Person : IPerson
{
   public int Age { get; set; }
   public string Name { get; set; }
   public bool Alive { get; set; }  <---- This should prevent it from compiling as well.
}

I would want it to not compile as well, or at the very least give me a compile warning.

Servy

No. Interfaces define what members an object must implement. They cannot define members that an object can't implement. You could potentially use your own custom, or third party code analysis tools, to identify cases like this, but there is nothing in the language itself that would support it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to prevent class instance from being modified?

From Dev

How to prevent class instance from being modified?

From Dev

How to prevent files from being modified in elFinder?

From Dev

How to prevent SBT from recompiling modified .class files?

From Dev

Prevent a Class from being instantiated

From Dev

How can I prevent methods from being added to a class?

From Dev

How to prevent a class from being build into the release DLL?

From Dev

How to prevent abstract model class from being included in migrations

From Dev

prevent derived class from being destroyed

From Dev

Is there a way to protect a class variable from being modified outside of a function

From Dev

Is there a way to protect a class variable from being modified outside of a function

From Dev

In c++, if a member pointer point to some data, how to protect that data from being modified?

From Dev

How to prevent timestamp from being reorder out?

From Dev

How to prevent the JPanel from being updated?

From Dev

How to prevent the Content of a UserControl from being overridden?

From Dev

How to prevent a process from being killed?

From Dev

How to prevent embedded manifest from being used?

From Dev

How to prevent a custom QWidget from being resized?

From Dev

How to prevent RSpec helper from being loaded

From Dev

DBGrid: how to prevent a row from being selected?

From Dev

How to prevent a Singleton object from being cloned?

From Dev

How to prevent a process from being killed?

From Dev

How to prevent a partition from being mounted?

From Dev

How to prevent directory from being deleted by user?

From Dev

How to prevent data from being written into a directory?

From Dev

How to prevent an email from accidentally being forwarded?

From Dev

How to prevent a module from being loaded

From Dev

How to prevent a custom QWidget from being resized?

From Dev

How to prevent "this" from being rebound by event handlers

Related Related

  1. 1

    How to prevent class instance from being modified?

  2. 2

    How to prevent class instance from being modified?

  3. 3

    How to prevent files from being modified in elFinder?

  4. 4

    How to prevent SBT from recompiling modified .class files?

  5. 5

    Prevent a Class from being instantiated

  6. 6

    How can I prevent methods from being added to a class?

  7. 7

    How to prevent a class from being build into the release DLL?

  8. 8

    How to prevent abstract model class from being included in migrations

  9. 9

    prevent derived class from being destroyed

  10. 10

    Is there a way to protect a class variable from being modified outside of a function

  11. 11

    Is there a way to protect a class variable from being modified outside of a function

  12. 12

    In c++, if a member pointer point to some data, how to protect that data from being modified?

  13. 13

    How to prevent timestamp from being reorder out?

  14. 14

    How to prevent the JPanel from being updated?

  15. 15

    How to prevent the Content of a UserControl from being overridden?

  16. 16

    How to prevent a process from being killed?

  17. 17

    How to prevent embedded manifest from being used?

  18. 18

    How to prevent a custom QWidget from being resized?

  19. 19

    How to prevent RSpec helper from being loaded

  20. 20

    DBGrid: how to prevent a row from being selected?

  21. 21

    How to prevent a Singleton object from being cloned?

  22. 22

    How to prevent a process from being killed?

  23. 23

    How to prevent a partition from being mounted?

  24. 24

    How to prevent directory from being deleted by user?

  25. 25

    How to prevent data from being written into a directory?

  26. 26

    How to prevent an email from accidentally being forwarded?

  27. 27

    How to prevent a module from being loaded

  28. 28

    How to prevent a custom QWidget from being resized?

  29. 29

    How to prevent "this" from being rebound by event handlers

HotTag

Archive