{"id":94906,"date":"2023-08-27T08:00:36","date_gmt":"2023-08-27T06:00:36","guid":{"rendered":"https:\/\/code-maze.com\/?p=94906"},"modified":"2023-08-27T09:49:38","modified_gmt":"2023-08-27T07:49:38","slug":"csharp-readonly-modifier","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-readonly-modifier\/","title":{"rendered":"Readonly Modifier in C#"},"content":{"rendered":"<p>In this article, we are going to learn about the readonly modifier in C#.<\/p>\n<p>However, before we go into detail on this topic, it is important to understand the concepts of mutability and immutability in programming.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/csharp-intermediate-topics\/ReadOnlyModifierInCSharp\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s begin!<\/p>\n<h2><a id=\"mutvsimm\"><\/a>Mutable vs. Immutable in C#<\/h2>\n<p><strong>In C#, <a href=\"https:\/\/code-maze.com\/csharp-immutable-collections\/\" target=\"_blank\" rel=\"noopener\">immutability<\/a> refers to the inability to change the state of an object once we create it.<\/strong><\/p>\n<p>When an object is mutable, we can change its state. Thus, it is flexible but potentially prone to unintended changes.<\/p>\n<p>Immutable objects provide a level of thread safety since we can&#8217;t modify them after creation. However, we must create a new instance to have an updated version of an immutable object, which can be less efficient.<\/p>\n<p><strong>Hence, immutable objects are preferable for situations where data consistency is critical, whereas mutable objects offer more flexibility in cases where frequent modifications are necessary.<\/strong><\/p>\n<h2><a id=\"readonly\"><\/a>Readonly Modifier in C#<\/h2>\n<p>We use the <code>readonly<\/code> modifier to make a variable immutable.\u00a0<\/p>\n<p>Once initialized, the value of a <code>readonly<\/code> variable can&#8217;t be changed throughout the object&#8217;s lifetime. It ensures that the variable remains constant after initialization, enforcing immutability on the variable.<\/p>\n<p>Let&#8217;s understand how the <code>readonly<\/code> modifier works:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Circle\r\n{\r\n    private readonly double _radius;\r\n\r\n    public Circle(double radius)\r\n    {\r\n        _radius = radius;\r\n    }\r\n}\r\n<\/pre>\n<p>Here, we can set the value of the <code>_radius<\/code> field at the time of declaration, or within the constructor. However, if we try to set the field value outside the constructor:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void ModifyRadius()\r\n{\r\n    _radius = _radius + 2;\r\n}<\/pre>\n<p>We get a compiler error:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Error CS0191 A readonly field cannot be assigned to (except in a constructor or init-only setter of the type in which the field is defined or a variable initializer)<\/code><\/p>\n<p>In C#, we also have the <code>const<\/code>\u00a0 keyword that helps us enforce a form of immutability. However, <a href=\"https:\/\/code-maze.com\/csharp-differences-between-const-and-readonly\/\" target=\"_blank\" rel=\"noopener\">it works differently<\/a> from the <code>readonly<\/code> modifier. It&#8217;s typically useful for values that are universal constants, like mathematical constants.<\/p>\n<p><strong>The value of a <code>const<\/code> variable is directly embedded into the compiled code<\/strong>, and any attempt to modify it, even in the constructor, results in a compile-time error.<\/p>\n<h2><a id=\"rFields\"><\/a>Use Readonly Modifier in C# With Fields<\/h2>\n<p><span style=\"font-size: 16px;\">To declare a <\/span><code style=\"font-size: 16px;\">readonly<\/code><span style=\"font-size: 16px;\"> field, we use the <\/span><code style=\"font-size: 16px;\">readonly<\/code><span style=\"font-size: 16px;\"> keyword followed by the type and field name. <\/span><\/p>\n<p><span style=\"font-size: 16px;\">We can initialize the field either at the time of declaration:<\/span><\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private readonly double _radius = 2.5;<\/code><\/p>\n<p><span style=\"font-size: 16px;\">Or within the constructor of the class:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public Circle(double radius)\r\n{\r\n    _radius = radius;\r\n}<\/pre>\n<p><span style=\"font-size: 16px;\">Once set, we can&#8217;t modify the value of this field elsewhere in the class. However, we can use them in any method or property of the class:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public double GetCircumference()\r\n{\r\n    return 2 * Math.PI * _radius;\r\n}<\/pre>\n<h3><a id=\"impactrf\"><\/a>Impact on Value Types vs. Reference Types<\/h3>\n<p><a href=\"https:\/\/code-maze.com\/csharp-value-vs-reference-types\/\" target=\"_blank\" rel=\"noopener\">Value types<\/a> are stored directly on the stack that holds the entire object. When we use the readonly modifier in C# on value type fields, it ensures that the value of the field can&#8217;t be modified after initialization.<\/p>\n<p>Let&#8217;s create a read-only struct <code>Point<\/code> (later on in the article, we will explain how <code>readonly<\/code> keyword affects the structure):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public readonly struct Point\r\n{\r\n    private readonly int _x;\r\n    private readonly int _y;\r\n\r\n    public int X =&gt; _x;\r\n    public int Y =&gt; _y;\r\n\r\n    public Point(int x, int y)\r\n    {\r\n        _x = x;\r\n        _y = y;\r\n    }\r\n}<\/pre>\n<p>Here, we have read-only fields <code>_x<\/code> and <code>_y<\/code>. We encapsulate them within the properties <code>X<\/code> and <code>Y<\/code> respectively.<\/p>\n<p>The <code>readonly<\/code> modifier ensures that once we create the <code>Point<\/code> object, we can&#8217;t modify the value of its fields.<\/p>\n<p>Also, our properties encapsulate the <code>private<\/code> read-only fields and those properties are also only-getters (readonly), so if we try to modify their values, we get a compilation error:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"4,5\">var point = new Point(2, 3);\r\nConsole.WriteLine($\"Coordinates: X - {point.X}, Y - {point.Y}\");\r\n\r\npoint.X = 6; \/\/error\r\npoint.Y = 10; \/\/error<\/pre>\n<p>Reference types, on the other hand, store a reference to an object on the heap. The <code>readonly<\/code> modifier ensures that the reference to the object can&#8217;t be changed after initialization.<\/p>\n<p>However, we can still modify the state of the object itself as we saw in the previous <code>Circle<\/code> class example. We couldn&#8217;t change the value of the <code>_radius<\/code> field outside the constructor but could still use the field value to calculate the circumference without any issues.<\/p>\n<p><strong>It&#8217;s important to note that the <code>readonly<\/code> modifier only makes the reference to the object immutable, and not the actual object itself. <\/strong>If we want to create a fully immutable object, we need to design the object itself to be immutable, such as by using <code>readonly<\/code> fields or properties within the object.<\/p>\n<h2><a id=\"READONLYP\"><\/a>Readonly Modifier in Properties<\/h2>\n<p>We can&#8217;t use the <code>readonly<\/code> modifier with properties to create read-only properties.\u00a0However, we can make a property read-only in other ways.<\/p>\n<h3><a id=\"autoimp\"><\/a>Readonly Auto-Implemented Properties<\/h3>\n<p>With this approach, we use auto-implemented properties with the <code>get<\/code> accessor only. We omit the <code>set<\/code> accessor, making the property read-only:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public int Age { get; }<\/code><\/p>\n<p>Here, we can only set the value of the property at the time of declaration or within the constructor of the class.<\/p>\n<p>We can also have a similar implementation of\u00a0a <code>public<\/code> readonly property:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Person\r\n{\r\n    private readonly int _age;\r\n           \r\n    public int Age =&gt; _age;\r\n\r\n    public Person(int age)\r\n    {\r\n        _age = age;\r\n    }\r\n}<\/pre>\n<p>Here, we can assign the value to the field inside the constructor and use the <code>Age<\/code> property to access the value of the <code>_age<\/code> field. The <code>Age<\/code> property is only-getter so it makes it readonly.<\/p>\n<h3><a id=\"pvtset\"><\/a>Readonly Properties With Private Setters<\/h3>\n<p>Here, we use public property with a private setter:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Person\r\n{\r\n    public string Name { get; private set; }\r\n    public int Age { get; }\r\n\r\n    public Person(string name, int age)\r\n    {\r\n        Name = name;\r\n        Age = age;\r\n    }\r\n\r\n    public void ChangeName(string changedName)\r\n    {\r\n        Name = changedName;\r\n    }\r\n}<\/pre>\n<p>This restricts the property from any external modification:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"4\">var person = new Person(\"Jack\", 21);\r\nConsole.WriteLine($\"Name: {person.Name}\");\r\n\r\nperson.Name = \"Emily\";<\/pre>\n<p>So, directly trying to change the <code>Name<\/code> property gives us a compiler error.\u00a0However, we can still set the property within the class or constructor:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">person.ChangeName(\"Emily\");<\/code><\/p>\n<p>As the <code>ChangeName()<\/code> method is present within the <code>Person<\/code> class, we can change the property value using this method. <strong>Thus, it is important to note that this approach makes a property read-only only from the perspective of the consumer<\/strong>. Internally, we can still change the state of such property.<\/p>\n<h3><a id=\"custget\"><\/a>Readonly Properties With Custom Getters<\/h3>\n<p>We can also create read-only properties with custom getters:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public double Area\r\n{\r\n    get { return Math.PI * _radius * _radius; }\r\n}<\/pre>\n<p>This allows us to calculate the value of the property based on some logic every time we access it.<\/p>\n<p>We looked at various ways to create a read-only property with or without using the <code>readonly<\/code> modifier. Let&#8217;s also understand how it differs from a read-write property.<\/p>\n<h2><a id=\"readonlym\"><\/a>Readonly Modifier in Methods<\/h2>\n<p>In C#, the concept of <code>readonly<\/code> is primarily used for fields and properties to create read-only data, and it does not have a direct counterpart for methods.<\/p>\n<p>However, we have related concepts such as <em>&#8220;read-only methods&#8221;<\/em> and <em>&#8220;pure functions&#8221;<\/em> to explore immutability in methods.<\/p>\n<h3><a id=\"rmpf\"><\/a>Readonly Methods and Pure Functions<\/h3>\n<p>A read-only method refers to a method that does not modify the state of the object it belongs to. In other words, a read-only method does not change the values of any fields or properties of the class. Instead, it operates only on its input parameters.<\/p>\n<p><strong>A pure function is a special type of read-only method that produces the same output for the same input and has no side effects<\/strong>. It solely depends on its input parameters and avoids modifying any external state or variables.<\/p>\n<p>The <code>GetCircumference()<\/code> method we discussed earlier is an example of a pure function as it calculates the circumference of a circle using the read-only <code>_radius<\/code> field without modifying any value.<\/p>\n<h2><a id=\"readonlyc\"><\/a>Readonly Modifier in Classes<\/h2>\n<p>We can&#8217;t directly apply the <code>readonly<\/code> modifier to a class. <strong>However, we can make a class read-only by making all its members immutable<\/strong>. For example, using only read-only fields and properties within the class.<\/p>\n<p>When we make the class read-only, we can only initialize it once during construction, and can&#8217;t modify its state afterward.<\/p>\n<p>Read-only classes are useful in specific scenarios where we want to ensure that the state of an object remains constant and can&#8217;t be replaced.\u00a0 This can be particularly valuable in multithreaded environments when we need to guarantee that the object won&#8217;t change unexpectedly.<\/p>\n<h2><a id=\"readonlys\"><\/a>Readonly Modifier in Structs<\/h2>\n<p>When we apply the <code>readonly<\/code> modifier to a struct, it indicates that instances of the struct are immutable, meaning that their values cannot be changed after initialization. <strong>All the fields and properties of the readonly struct must be readonly as well.<\/strong> If they are not, we will get an error: <code>CS8340: Instance fields of readonly structs must be readonly<\/code>.<\/p>\n<p>We can still create multiple instances of the struct:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var point = new Point(2, 3);\r\nvar otherPoint = new Point(1, 4);<\/pre>\n<p>However, we can&#8217;t modify the value of the struct members.<\/p>\n<p>Now that we have explored the usage of the <code>readonly<\/code> modifier in fields, properties, methods, classes, and structs, let&#8217;s look into some essential best practices for using it.<\/p>\n<h2><a id=\"bestprac\"><\/a>Best Practices<\/h2>\n<p>We should use the <code>readonly<\/code> modifier for fields or properties that are designed not to change their values after initialization. This helps enforce immutability and prevents accidental modifications.<\/p>\n<p>To enforce immutability on collections, we should prefer the use of <a href=\"https:\/\/code-maze.com\/csharp-immutable-collections\/\" target=\"_blank\" rel=\"noopener\">immutable collections<\/a> such as <code>ImmutableList<\/code>, or <code>ImmutableDictionary<\/code>.<\/p>\n<p>We should use <code>readonly<\/code> properties with custom getters for values that are computed based on business logic but do not change over the object&#8217;s lifetime. This allows us to cache computed values while not modifying the object&#8217;s state.<\/p>\n<p>While <code>readonly<\/code> modifier helps with immutability, it does not automatically guarantee thread safety. If the code involves multithreading scenarios, we should ensure proper <a href=\"https:\/\/code-maze.com\/csharp-locking-mechanism\/\" target=\"_blank\" rel=\"noopener\">synchronization<\/a> when needed.<\/p>\n<p>Not everything needs to be immutable. We should try to strike a balance between mutability and immutability by using mutable objects where we require flexibility.<\/p>\n<h2><a id=\"conc\"><\/a>Conclusion<\/h2>\n<p>In this article, we&#8217;ve looked at the readonly modifier in C# and its impact on fields, properties, methods, classes, and structs. Understanding when and how to use <code>readonly<\/code> allows us to enforce immutability and prevent unintended modifications, leading to a more manageable codebase.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to learn about the readonly modifier in C#. However, before we go into detail on this topic, it is important to understand the concepts of mutability and immutability in programming. Let&#8217;s begin! Mutable vs. Immutable in C# In C#, immutability refers to the inability to change the state of [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12,506],"tags":[1811,1914,1931],"class_list":["post-94906","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","category-intermediate","tag-c","tag-readonly","tag-readonly-modifier","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Readonly Modifier in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we are going to learn about readonly modifier in C#. Let&#039;s understand what is mutable and immutable.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-readonly-modifier\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Readonly Modifier in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we are going to learn about readonly modifier in C#. Let&#039;s understand what is mutable and immutable.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-readonly-modifier\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-27T06:00:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-27T07:49:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"Readonly Modifier in C#\",\"datePublished\":\"2023-08-27T06:00:36+00:00\",\"dateModified\":\"2023-08-27T07:49:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/\"},\"wordCount\":1441,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"C#\",\"readonly\",\"readonly modifier\"],\"articleSection\":[\"C#\",\"Intermediate\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-readonly-modifier\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/\",\"url\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/\",\"name\":\"Readonly Modifier in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2023-08-27T06:00:36+00:00\",\"dateModified\":\"2023-08-27T07:49:38+00:00\",\"description\":\"In this article, we are going to learn about readonly modifier in C#. Let's understand what is mutable and immutable.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-readonly-modifier\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-readonly-modifier\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Readonly Modifier in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Readonly Modifier in C# - Code Maze","description":"In this article, we are going to learn about readonly modifier in C#. Let's understand what is mutable and immutable.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-readonly-modifier\/","og_locale":"en_US","og_type":"article","og_title":"Readonly Modifier in C# - Code Maze","og_description":"In this article, we are going to learn about readonly modifier in C#. Let's understand what is mutable and immutable.","og_url":"https:\/\/code-maze.com\/csharp-readonly-modifier\/","og_site_name":"Code Maze","article_published_time":"2023-08-27T06:00:36+00:00","article_modified_time":"2023-08-27T07:49:38+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-readonly-modifier\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-readonly-modifier\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"Readonly Modifier in C#","datePublished":"2023-08-27T06:00:36+00:00","dateModified":"2023-08-27T07:49:38+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-readonly-modifier\/"},"wordCount":1441,"commentCount":1,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-readonly-modifier\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["C#","readonly","readonly modifier"],"articleSection":["C#","Intermediate"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-readonly-modifier\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-readonly-modifier\/","url":"https:\/\/code-maze.com\/csharp-readonly-modifier\/","name":"Readonly Modifier in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-readonly-modifier\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-readonly-modifier\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2023-08-27T06:00:36+00:00","dateModified":"2023-08-27T07:49:38+00:00","description":"In this article, we are going to learn about readonly modifier in C#. Let's understand what is mutable and immutable.","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-readonly-modifier\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-readonly-modifier\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-readonly-modifier\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-readonly-modifier\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Readonly Modifier in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/94906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=94906"}],"version-history":[{"count":3,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/94906\/revisions"}],"predecessor-version":[{"id":94910,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/94906\/revisions\/94910"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=94906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=94906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=94906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}