{"id":1433,"date":"2023-04-08T21:37:06","date_gmt":"2023-04-08T14:37:06","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=1433"},"modified":"2023-04-09T10:31:51","modified_gmt":"2023-04-09T03:31:51","slug":"csharp-internal","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-internal\/","title":{"rendered":"C# Internal"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll how to use the C# <code>internal<\/code> keyword to restrict types and their members to be accessible within the same assembly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to C# internal keyword<\/h2>\n\n\n\n<p>In .NET, an assembly is a package of code and resources that the .NET runtime can deploy, version, and execute, and developers can use to create applications or libraries.<\/p>\n\n\n\n<p>To create an assembly from code, you compile the code into <code>DLL<\/code> or <code>EXE<\/code> file. Then, you or other developers can reference the assembly from another project.<\/p>\n\n\n\n<p>C# <code>internal<\/code> keyword specifies that types like <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-class\/\">classes<\/a> and <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-interface\/\">interfaces<\/a> or members of types should be accessible only within the same assembly, also known as assembly scope. In other words, if a type or member of a type is marked as <code>internal<\/code>, other assemblies cannot access it directly.<\/p>\n\n\n\n<p>The <code>internal<\/code> keyword allows you to effectively hide implementation detail. It also ensures that your library is used as intended, without exposing the internal details to external assemblies. This makes your code more maintainable and secure.<\/p>\n\n\n\n<p>Let&#8217;s say you develop a C# library called <code>MyLib<\/code> that contains a <code>Utility<\/code> class that you want to use in your projects within the same solution. But you don&#8217;t want other developers to access the <code>Utility<\/code> class and its methods from their assemblies outside of your library.<\/p>\n\n\n\n<p>To do it, you can use the <code>internal<\/code> keyword for the <code>Utility<\/code> class and its method like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">MyLib<\/span>\n{\n    <span class=\"hljs-keyword\">internal<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Utility<\/span>\n    {\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">internal<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Calculate<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n        {\n        }\n    }\n\n    <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span>\n    {\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">DoSomething<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n        {\n            Utility.Calculate();\n            \n            <span class=\"hljs-comment\">\/\/ ...<\/span>\n        }\n    }\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we mark the <code>Utility<\/code> class and its <code>Calculate()<\/code> method as <code>internal<\/code>. It means that we only allow access to them within the same assembly (<code>MyLib<\/code> project).<\/p>\n\n\n\n<p>On the other hand, we mark the <code>MyClass<\/code> as <code>public<\/code>, meaning that we allow other assemblies to reference the <code>MyClass<\/code>.<\/p>\n\n\n\n<p>It&#8217;s important to note that if you attempt to access the <code>Utility<\/code> class or its <code>Calculate()<\/code> method from another assembly, you&#8217;ll get a compilation error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Protected Internal<\/h2>\n\n\n\n<p>The <code>protected internal<\/code> access modifier is a combination of the <code>protected<\/code> and <code>internal<\/code> modifiers. The <code>protected internal<\/code> allows access to members from within the same assembly, as well as from within derived classes in any assembly, more specifically:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <code>protected internal<\/code> member can be accessed from any class within the same assembly, just like an <code>internal<\/code> member.<\/li>\n\n\n\n<li>A <code>protected internal<\/code> member can also be accessed from a derived class in any assembly, just like a <code>protected<\/code> member.<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s an example of using the <code>protected internal<\/code> access modifier:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Assembly1<\/span>\n\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">MyLib<\/span>;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyBaseClass<\/span>\n{\n    <span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">internal<\/span> <span class=\"hljs-keyword\">int<\/span> MyProtectedInternalField;\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyDerivedClass<\/span>: <span class=\"hljs-title\">MyBaseClass<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">DoSomething<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">var<\/span> <span class=\"hljs-keyword\">value<\/span> = MyProtectedInternalField;\n        Console.WriteLine(<span class=\"hljs-keyword\">value<\/span>);\n    }\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>MyBaseClass<\/code> has a <code>protected internal<\/code> member. Hence, we can access it from the <code>DoSomething()<\/code> method of the derived class within the same assembly.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Assembly2<\/span>\n<span class=\"hljs-keyword\">using<\/span> MyLib;\n\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">MyLib2<\/span>;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span> : <span class=\"hljs-title\">MyDerivedClass<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">MyMethod<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ reference the protected internal field <\/span>\n        <span class=\"hljs-comment\">\/\/ from the MyDerivedClass in another assembly<\/span>\n        <span class=\"hljs-keyword\">var<\/span> <span class=\"hljs-keyword\">value<\/span> = MyProtectedInternalField;\n        \n        <span class=\"hljs-comment\">\/\/...<\/span>\n        \n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this assembly, we define the <code>MyClass<\/code> that extends the <code>MyDerivedClass<\/code> class from <code>Assembly1<\/code>. <\/p>\n\n\n\n<p>The <code>MyMethod()<\/code> of the <code>MyClass<\/code> can access the <code>MyProtectedInternalField<\/code> member of the <code>MyDerivedClass<\/code> of the <code>Assembly1<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the C# <code>internal<\/code> keyword to allow types and their members to be accessible within the same assembly.<\/li>\n\n\n\n<li>Use he <code>protected internal<\/code> modifier to allow a member to be accessed within the same assembly, as well as from the derived class from the same or another assembly.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1433\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-internal\/\"\n\t\t\t\tdata-post-title=\"C# Internal\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1433\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-internal\/\"\n\t\t\t\tdata-post-title=\"C# Internal\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll how to use the C# internal keyword to restrict types and their members to be accessible within the same assembly. Introduction to C# internal keyword In .NET, an assembly is a package of code and resources that the .NET runtime can deploy, version, and execute, and developers can use to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":56,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1433","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=1433"}],"version-history":[{"count":5,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1433\/revisions"}],"predecessor-version":[{"id":1444,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1433\/revisions\/1444"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/7"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=1433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}