<p>OK, I know what you're thinking, &quot;why write a method you do not want people to use?&quot; Right?</p>
<p>Well, in short, I have a class that needs to be serialized to XML. In order for the <a href="https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer%28v=vs.110%29.aspx" rel="noreferrer"><code>XmlSerializer</code></a> to do its magic, the class must have a default, empty constructor:</p>
<pre><code>public class MyClass
{
  public MyClass()
  {
    // required for xml serialization
  }
}
</code></pre>
<p>So, I need to have it, but I don't want people to <em>use</em> it, so <strong>is there any attribute that can be use to mark the method as &quot;DO NOT USE&quot;?</strong></p>
<p>I was thinking of using the <em>Obsolete</em> attribute (since this can stop the build), but that just seems kinda &quot;wrong&quot;, is there any other way of doing this, or do I need to go ahead and bite the bullet? :)</p>
<h2>Update</h2>
<p>OK, I have accepted Keith's answer, since I guess in my heart of hearts, I totally agree. This is why I asked the question in the first place, I don't like the notion of having the <em>Obsolete</em> attribute.</p>
<h3>However...</h3>
<p>There <em>is</em> still a problem, while we are being notified in intellisense, ideally, we would like to break the build, so is there any way to do this? Perhaps create a custom attribute?</p>
<p><strong>More focused question has been created <a href="https://stackoverflow.com/questions/28150/create-an-attribute-to-break-the-build">here</a>.</strong></p>

[{'answer_id': 27763, 'author': 'hometoast', 'author_id': 2009, 'author_profile': 'https://Stackoverflow.com/users/2009', 'pm_score': 2, 'selected': False, 'text': '<p>I read the heading and immediately thought "obsolete atribute".  How about</p>\n\n<pre><code>    /// &lt;summary&gt;\n    /// do not use\n    /// &lt;/summary&gt;\n    /// &lt;param name="item"&gt;don\'t pass it anything -- you shouldn\'t use it.&lt;/param&gt;\n    /// &lt;returns&gt;nothing - you shouldn\'t use it&lt;/returns&gt;\n    public bool Include(T item) { \n    ....\n</code></pre>\n'}, {'answer_id': 27764, 'author': 'Sir Rippov the Maple', 'author_id': 2822, 'author_profile': 'https://Stackoverflow.com/users/2822', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/system.obsoleteattribute.aspx" rel="nofollow noreferrer"><code>ObsoleteAttribute</code></a> will probably work in your situation - you can even cause the build to break if that method is used. </p>\n\n<p>Since obsolete warnings occur at compile time, and since the reflection needed for serialization occurs at runtime, marking that method obsolete won\'t break serialization, but will warn developers that the method is not there to be used.</p>\n'}, {'answer_id': 27765, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 0, 'selected': False, 'text': '<p>What you\'re looking for is the <a href="http://msdn.microsoft.com/en-us/library/system.obsoleteattribute.aspx" rel="nofollow noreferrer"><code>ObsoleteAttribute</code></a> class:</p>\n\n<pre><code>using System;\n\npublic sealed class App {\n   static void Main() {      \n      // The line below causes the compiler to issue a warning:\n      // \'App.SomeDeprecatedMethod()\' is obsolete: \'Do not call this method.\'\n      SomeDeprecatedMethod();\n   }\n\n   // The method below is marked with the ObsoleteAttribute. \n   // Any code that attempts to call this method will get a warning.\n   [Obsolete("Do not call this method.")]\n   private static void SomeDeprecatedMethod() { }\n}\n</code></pre>\n'}, {'answer_id': 27766, 'author': 'chrissie1', 'author_id': 2936, 'author_profile': 'https://Stackoverflow.com/users/2936', 'pm_score': 0, 'selected': False, 'text': '<p>Yep there is. </p>\n\n<p>I wrote this blogpost about it <a href="http://blogs.lessthandot.com/admin.php?ctrl=items&amp;blog=4&amp;p=104" rel="nofollow noreferrer">Working with the designer</a>.</p>\n\n<p>And here is the code:</p>\n\n<p><pre><code>\npublic class MyClass\n{\n  [Obsolete("reason", true)]\n  public MyClass()\n  {\n    // required for xml serialization\n  }\n}\n</pre></code></p>\n'}, {'answer_id': 27767, 'author': 'Biri', 'author_id': 968, 'author_profile': 'https://Stackoverflow.com/users/968', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m using the <a href="http://msdn.microsoft.com/en-us/library/system.obsoleteattribute.aspx" rel="nofollow noreferrer"><code>ObsoleteAttribute</code></a>.</p>\n\n<p>But also you can have some comments of course.</p>\n\n<p>And finally remove it completely if you can (don\'t have to maintain the compatibility with something old). That\'s the best way.</p>\n'}, {'answer_id': 27796, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 0, 'selected': False, 'text': '<p>Wow, that problem is bugging me too. </p>\n\n<p>You also need default constructors for NHibernate, but I want to force people to NOT use C# 3.0 object initializers so that classes go through constructor code.</p>\n'}, {'answer_id': 27825, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 5, 'selected': True, 'text': '<p>If a class is <a href="https://msdn.microsoft.com/en-us/library/system.serializableattribute%28v=vs.110%29.aspx" rel="noreferrer"><code>[Serialisable]</code></a> (i.e. it can be copied around the place as needed) the param-less constructor is needed to deserialise.</p>\n\n<p>I\'m guessing that you want to force your code\'s access to pass defaults for your properties to a parameterised constructor.</p>\n\n<p>Basically you\'re saying that it\'s OK for the <a href="https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer%28v=vs.110%29.aspx" rel="noreferrer"><code>XmlSerializer</code></a> to make a copy and then set properties, but you don\'t want your own code to.</p>\n\n<p>To some extent I think this is over-designing. </p>\n\n<p>Just add XML comments that detail what properties need initialising (and what to). </p>\n\n<p>Don\'t use <a href="https://msdn.microsoft.com/en-us/library/system.obsoleteattribute%28v=vs.110%29.aspx" rel="noreferrer"><code>[Obsolete]</code></a>, because it isn\'t. Reserve that for genuinely deprecated methods.</p>\n'}, {'answer_id': 27906, 'author': 'rjzii', 'author_id': 1185, 'author_profile': 'https://Stackoverflow.com/users/1185', 'pm_score': 2, 'selected': False, 'text': '<p>I would actually be inclined to disagree with everyone that is advocating the use of the <a href="http://msdn.microsoft.com/en-us/library/system.obsoleteattribute.aspx" rel="nofollow noreferrer"><code>ObsoleteAttribute</code></a> as the MSDN documentation says that:</p>\n\n<blockquote>\n  <p>Marking an element as obsolete informs the users that the element will be removed in future versions of the product.</p>\n</blockquote>\n\n<p>Since the generic constructors for XML serialization should not be removed from the application I wouldn\'t apply it just in case a maintenance developer down the road is not familiar with how XML serialization works. </p>\n\n<p>I have actually been using <a href="https://stackoverflow.com/questions/27758/notify-developer-of-a-do-not-use-method#27825">Keith\'s</a> method of just noting that the constructor is used for serialization in XML documentation so that it shows up in Intellisense.</p>\n'}, {'answer_id': 29068, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 5, 'selected': False, 'text': '<p><strong>Prior to VS2013</strong> you could use:</p>\n<p><code>[<a href="https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.editorbrowsableattribute" rel="nofollow noreferrer">System.ComponentModel.EditorBrowsable</a>(<a href="https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.editorbrowsablestate" rel="nofollow noreferrer">System.ComponentModel.EditorBrowsableState.Never</a>)]\n</code></p>\n<p>so that it doesn\'t show up in IntelliSense. If the consumer still wants to use it they can, but it won\'t be as discoverable.</p>\n<p><a href="https://stackoverflow.com/a/27825/3195477">Keith\'s point</a> about over-engineering still stands though.</p>\n<hr />\n<p><strong>Since VS2013</strong> this feature has been removed. As noted in <a href="https://github.com/dotnet/roslyn/issues/37478" rel="nofollow noreferrer">https://github.com/dotnet/roslyn/issues/37478</a> this was &quot;by design&quot; and apparently will not be brought back.</p>\n'}, {'answer_id': 29079, 'author': 'Sergio Acosta', 'author_id': 2954, 'author_profile': 'https://Stackoverflow.com/users/2954', 'pm_score': 2, 'selected': False, 'text': '<p>You could build your own <a href="https://msdn.microsoft.com/en-us/library/system.attribute(v=vs.110).aspx" rel="nofollow noreferrer"><code>Attribute</code></a> derived class, say <code>NonCallableAttribute</code> to qualify methods, and then add to your build/CI code analysis task the check to monitor if any code is using those methods.</p>\n\n<p>In my opinion, you really cannot force developers to not use the method, but you could detect when someone broke the rule as soon as possible and fix it.</p>\n'}, {'answer_id': 29090, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 3, 'selected': False, 'text': '<pre><code>throw new ISaidDoNotUseException();\n</code></pre>\n'}, {'answer_id': 29212, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Separate your serializable object from your domain object.</p>\n'}, {'answer_id': 60097575, 'author': 'Lukáš Kmoch', 'author_id': 2945197, 'author_profile': 'https://Stackoverflow.com/users/2945197', 'pm_score': 0, 'selected': False, 'text': '<p>There is already quite a lot of good answers.\nBut I think the best option is to use serializer that can use parametrized constructor.\nIt is not serializer you need but for example Entity Framework Core knows how to use parametrized constructors.</p>\n\n<p><strong>Entity Framework Core documentation:</strong></p>\n\n<blockquote>\n  <p>If EF Core finds a parameterized constructor with parameter names and\n  types that match those of mapped properties, then it will instead call\n  the parameterized constructor with values for those properties and\n  will not set each property explicitly.</p>\n</blockquote>\n'}, {'answer_id': 72444002, 'author': 'Saly', 'author_id': 720275, 'author_profile': 'https://Stackoverflow.com/users/720275', 'pm_score': 2, 'selected': False, 'text': '<p>Nowadays you can use <em>code analyzers</em> for such need - thanks to Roslyn compiler in modern .NET.</p>\n<p>Either you can write your own code analyzer. Here are some tips to start with:</p>\n<ul>\n<li><a href="https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix</a></li>\n<li><a href="https://andrewlock.net/creating-a-roslyn-analyzer-in-visual-studio-2017/" rel="nofollow noreferrer">https://andrewlock.net/creating-a-roslyn-analyzer-in-visual-studio-2017/</a></li>\n<li><a href="https://www.meziantou.net/writing-a-roslyn-analyzer.htm" rel="nofollow noreferrer">https://www.meziantou.net/writing-a-roslyn-analyzer.htm</a></li>\n</ul>\n<p>Or use some already existing - this is way I have choosen for my needs:</p>\n<ul>\n<li><a href="https://github.com/dotnet/roslyn-analyzers/blob/5968beca1c5c6c12d4fa81a08b7b4561a3c05d6e/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/BannedApiAnalyzers.Help.md" rel="nofollow noreferrer">BannedApiAnalzyers</a> - <strong>this one is exactly for the &quot;DO NOT USE&quot; warning</strong></li>\n</ul>\n<p>Here is another nice &quot;homepage&quot; for Roslyn Analyzers:\nCybermaxs/awesome-analyzers: A curated list of .NET Compiler Platform (&quot;Roslyn&quot;) diagnostic analyzers and code fixes. Everyone can contribute here!\n<a href="https://github.com/Cybermaxs/awesome-analyzers" rel="nofollow noreferrer">https://github.com/Cybermaxs/awesome-analyzers</a></p>\n'}]
['https://Stackoverflow.com/questions/27758', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/832/']