{"id":1356,"date":"2011-04-07T22:45:35","date_gmt":"2011-04-07T17:15:35","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1356"},"modified":"2011-04-07T22:45:35","modified_gmt":"2011-04-07T17:15:35","slug":"final-keyword-in-c-sealed-with-const-and-readonly","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/final-keyword-in-c-sealed-with-const-and-readonly\/","title":{"rendered":"Final Keyword in C# &#8211; sealed with const and readonly"},"content":{"rendered":"<p>In java there is keyword &#8220;<strong>final<\/strong>&#8220;\u009d, which is used to avoid overloading \/ inheritance of method \/ class respectively.<\/p>\n<p>In c# there is no keyword like &#8220;final&#8221;\u009d but the same thing is achieved by keyword &#8220;<strong>sealed<\/strong>&#8220;\u009d.<\/p>\n<p><strong>A class which is marked by keyword sealed cannot be inherited.<\/strong><\/p>\n<p>If you have ever noticed, structs are sealed. You cannot derive a class from a struct.<!--more--><\/p>\n<p>Example:<\/p>\n<pre class=\"brush: csharp; highlight: [3]; title: ; notranslate\" title=\"\">\nnamespace OOPS_Concept\n{\n    sealed class SealedClassDemo\n    {\n        public void test1()\n        {\n            Console.WriteLine(&quot;Method Test1&quot;);\n        }\n    }\n\n    class childclass : SealedClassDemo\n    {\n    }\n}\n<\/pre>\n<p>Above code will generate an error saying that class cannot be inherited.<\/p>\n<p><strong>Sealed Method:<\/strong><\/p>\n<p>In C#, Methods cannot be &#8220;sealed&#8221;\u009d directly. <strong>Methods of only derived class can be made sealed with keyword sealed and override.<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nnamespace OOPS_Concept\n{\n    class SealedMethodDemo\n    {\n        public virtual void Method1()\n        {\n            Console.Write(&quot;Base class Method1&quot;);\n        }\n    }\n\n    class ChildClass : SealedMethodDemo\n    {\n        public sealed override void Method1()\n        {\n            Console.Write(&quot;Derived class Method1&quot;);\n        }\n    }\n}\n<\/pre>\n<p>One of the best usage of sealed classes is when you have a class with static members. For example, the <strong>Pens <\/strong>and <strong>Brushes <\/strong>classes of the<strong> System.Drawing<\/strong> namespace.<\/p>\n<hr \/>\n<p><strong>readonly:<\/strong><\/p>\n<p>The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the <strong>declaration or in a constructor<\/strong> in the same class.<\/p>\n<p><a title=\"readonly keyword in c#\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/acdd6hb7(v=vs.71).aspx\" target=\"_blank\">refer this MSDN document to read more on keyword &#8220;readonly&#8221; in C#<\/a><\/p>\n<hr \/>\n<p><strong>const:<\/strong><\/p>\n<p>The <strong>const <\/strong>keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable cannot be modified. A constant declaration introduces one or more constants of a given type. The declaration takes the form:<\/p>\n<blockquote><p>public const double x = 1.0, y = 2.0, z = 3.0;<\/p><\/blockquote>\n<p><strong>Note :<\/strong> The <strong>readonly <\/strong>keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a <strong>compile-time<\/strong> constant, the readonly field can be used for <strong>runtime constants<\/strong>, as in the following example:<\/p>\n<blockquote><p>public static readonly uint l1 = (uint) DateTime.Now.Ticks;<\/p><\/blockquote>\n<p><a title=\"MSDN on const keyword\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/e6w8fe1b(v=vs.71).aspx\" target=\"_blank\">MSDN article on const keyword<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example of Final Keyword in C# &#8211; sealed with const and readonly <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[22],"tags":[51],"class_list":["post-1356","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1933,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/virtual-override-and-new-keyword-in-c\/","url_meta":{"origin":1356,"position":0},"title":"Virtual, Override and new Keyword in C#","author":"Jitendra","date":"April 7, 2011","format":false,"excerpt":"Virtual, Override and new Keyword : Polymorphism , Method Hiding and Overriding in C#","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"Multilevel Inheritance","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Multilevel-Inheritance.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":685,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/static-class\/","url_meta":{"origin":1356,"position":1},"title":"Static class","author":"Jitendra","date":"August 12, 2010","format":false,"excerpt":"Example of Static Class, its usage and benefits","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2376,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/access-modifiers-specifiers-in-c-language\/","url_meta":{"origin":1356,"position":2},"title":"Access Modifiers \/ Specifiers in C# Language","author":"Jitendra","date":"August 11, 2011","format":false,"excerpt":"Available Access Modifiers \/ Specifiers in C# Language","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1937,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/virtual-function-in-java\/","url_meta":{"origin":1356,"position":3},"title":"Virtual Function in JAVA","author":"Jitendra","date":"April 8, 2011","format":false,"excerpt":"Example and explanation of Virtual Function in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":702,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-between-interfaceinheritance-abstract-class\/","url_meta":{"origin":1356,"position":4},"title":"Interface, Inheritance and abstract class","author":"Jitendra","date":"August 6, 2010","format":false,"excerpt":"What is abstract class, interface and inheritance in java. Source code and example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Inheritance, Interface and abstract class","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/Inheritance1-300x173.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2445,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/create-a-custom-web-service-in-salesforce-and-consume-it-in-c-net-application\/","url_meta":{"origin":1356,"position":5},"title":"Create a custom Web service in Salesforce and consume it in C#.Net application","author":"Jitendra","date":"September 23, 2011","format":false,"excerpt":"In this tutorial, we will create the web service in salesforce and consume it in c#.Net application","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"Create Custom web service in salesforce using apex and consume using C#.Net","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/09\/Create-Custom-web-service-in-salesforce-using-apex-and-consume-using-C.Net_.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1356"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1356\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}