{"id":275,"date":"2022-01-03T09:15:50","date_gmt":"2022-01-03T02:15:50","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=275"},"modified":"2022-01-05T09:17:15","modified_gmt":"2022-01-05T02:17:15","slug":"csharp-this","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-this\/","title":{"rendered":"C# this"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about C# <code>this<\/code> keyword and how to use it effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is this keyword in C#<\/h2>\n\n\n\n<p>A <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-class\/\">class<\/a> is a blueprint for creating objects. To reference the current object inside the class, you use the <code>this<\/code> keyword. Therefore, the <code>this<\/code> keyword refers to the current instance of the class.<\/p>\n\n\n\n<p>To explicitly reference a field inside a method of a class, you use the <code>this<\/code> keyword, followed by the dot operator (<code>.<\/code>), and the field name 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\">this<\/span>.FieldName<\/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>Likewise, you can use the <code>this<\/code> keyword to explicitly call a method in another method of the class:<\/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-keyword\">this<\/span>.MethodName(arguments)<\/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<h2 class=\"wp-block-heading\">C# this keyword example<\/h2>\n\n\n\n<p>The following example defines the <code>Person<\/code> class with the fields <code>FirstName<\/code> and <code>LastName<\/code> and two methods <code>GetFullName()<\/code> and <code>SayHi()<\/code>:<\/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\">\/\/ Person.cs<\/span>\n\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>\n{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> FirstName;\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> LastName;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">GetFullName<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">$\"<span class=\"hljs-subst\">{<span class=\"hljs-keyword\">this<\/span>.FirstName}<\/span> <span class=\"hljs-subst\">{<span class=\"hljs-keyword\">this<\/span>.LastName}<\/span>\"<\/span>;\n    }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">SayHi<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">$\"Hi, I'm <span class=\"hljs-subst\">{<span class=\"hljs-keyword\">this<\/span>.GetFullName()}<\/span>.\"<\/span>;\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 example, we use the <code>this<\/code> keyword to reference the <code>FirstName<\/code> and <code>LastName<\/code> fields in the <code>GetFullName()<\/code> method. Also, we use the <code>this<\/code> keyword to call the <code>GetFullName()<\/code> method in the <code>SayHi()<\/code> method.<\/p>\n\n\n\n<p>The following creates a new instance of the <code>Person<\/code> class and call the <code>SayHi()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Program.cs<\/span>\n\n<span class=\"hljs-comment\">\/\/ Create a new instance of the Person class<\/span>\nPerson p1 = <span class=\"hljs-keyword\">new<\/span>();\np1.FirstName = <span class=\"hljs-string\">\"John\"<\/span>;\np1.LastName = <span class=\"hljs-string\">\"Doe\"<\/span>;\n\n<span class=\"hljs-comment\">\/\/ Show the fullname<\/span>\nConsole.WriteLine(p1.GetFullName());\n\n<span class=\"hljs-comment\">\/\/ Display the greeting<\/span>\nConsole.WriteLine(p1.SayHi());<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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>When calling the <code>p1.GetFullName()<\/code> method, the <code>this<\/code> keyword references the <code>p1<\/code> object. So the <code>FirstName<\/code> and <code>LastName<\/code> fields are <code>\"John\"<\/code> and <code>\"Doe\"<\/code> respectively.<\/p>\n\n\n\n<p>The <code>SayHi()<\/code> method calls the <code>GetFullName()<\/code> method via the <code>this<\/code> keyword. Therefore, the <code>p1.GetFullName()<\/code> method returns the <code>\"John Doe\"<\/code> string. Hence, the <code>SayHi()<\/code> method returns the string <code>\"Hi, I'm John Doe.\"<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using C# this keyword for method chaining<\/h2>\n\n\n\n<p>The following example adds two new methods <code>SetFirstName()<\/code> and <code>SetLastName()<\/code> to the <code>Person<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Person.cs<\/span>\n\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>\n{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> FirstName;\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> LastName;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">GetFullName<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">$\"<span class=\"hljs-subst\">{<span class=\"hljs-keyword\">this<\/span>.FirstName}<\/span> <span class=\"hljs-subst\">{<span class=\"hljs-keyword\">this<\/span>.LastName}<\/span>\"<\/span>;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">SayHi<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">$\"Hi, I'm <span class=\"hljs-subst\">{<span class=\"hljs-keyword\">this<\/span>.GetFullName()}<\/span>.\"<\/span>;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Person <span class=\"hljs-title\">SetFirstName<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> firstName<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">this<\/span>.FirstName = firstName;\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">this<\/span>;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Person <span class=\"hljs-title\">SetLastName<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> lastName<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">this<\/span>.LastName = lastName;\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">this<\/span>;\n\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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>SetFirstName()<\/code> and <code>SetLastName()<\/code> methods assign their arguments to the <code>FirstName<\/code> and <code>LastName<\/code> fields and return the current instance of the <code>Person<\/code> class. <\/p>\n\n\n\n<p>The following creates a new <code>Person<\/code> object, calls the <code>SetFirstName()<\/code>, <code>SetLastName()<\/code>, and <code>SayHi()<\/code> methods:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Program.cs<\/span>\n\nPerson p1 = <span class=\"hljs-keyword\">new<\/span>();\n\np1.SetFirstName(<span class=\"hljs-string\">\"John\"<\/span>);\np1.SetLastName(<span class=\"hljs-string\">\"Doe\"<\/span>);\n<span class=\"hljs-keyword\">string<\/span> greeting = p1.SayHi();\n\nConsole.WriteLine(greeting);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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>Since both <code>SetFirstName()<\/code> and <code>SetLastName()<\/code> methods return the current <code>Person<\/code>&#8216;s object, you can replace three statements with one statement as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Program.cs<\/span>\n\n\nPerson p1 = <span class=\"hljs-keyword\">new<\/span>();\n\n<span class=\"hljs-keyword\">string<\/span> greeting = p1.SetFirstName(<span class=\"hljs-string\">\"John\"<\/span>)\n                    .SetLastName(<span class=\"hljs-string\">\"Doe\"<\/span>)\n                    .SayHi();\n\nConsole.WriteLine(greeting);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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>This technique is called <strong>method chaining<\/strong>.<\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>The <code>SetFirstName()<\/code> method returns <code>this<\/code> that is the <code>p1<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">p1.SetFirstName(<span class=\"hljs-string\">\"John\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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>However, you don&#8217;t need to use an immediate variable to store the p1. And you can immediately call <code>SetLastName()<\/code> of the <code>p1<\/code> object like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">p1.SetFirstName(<span class=\"hljs-string\">\"John\"<\/span>)\n  .SetLastName(<span class=\"hljs-string\">\"Doe\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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>Since the <code>SetLastName()<\/code> method also returns <code>this<\/code> that is the <code>p1<\/code> object, you can call the <code>SayHi()<\/code> method on it immediately. Because the <code>SayHi()<\/code> method returns a string, you can assign it to the <code>greeting<\/code> variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Program.cs<\/span>\n\nPerson p1 = <span class=\"hljs-keyword\">new<\/span>();\n\n<span class=\"hljs-keyword\">string<\/span> greeting = p1.SetFirstName(<span class=\"hljs-string\">\"John\"<\/span>)\n                    .SetLastName(<span class=\"hljs-string\">\"Doe\"<\/span>)\n                    .SayHi();\n\nConsole.WriteLine(greeting);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>The C# <code>this<\/code> keyword refers to the current object of the class.<\/li><li>Use C# <code>this<\/code> keyword for method chaining to make the code more concise.<\/li><\/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=\"275\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-this\/\"\n\t\t\t\tdata-post-title=\"C# this\"\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=\"275\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-this\/\"\n\t\t\t\tdata-post-title=\"C# this\"\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 learn about C# this keyword and how to use it effectively. What is this keyword in C# A class is a blueprint for creating objects. To reference the current object inside the class, you use the this keyword. Therefore, the this keyword refers to the current instance of the class. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":28,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-275","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/275","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=275"}],"version-history":[{"count":5,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/275\/revisions"}],"predecessor-version":[{"id":349,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/275\/revisions\/349"}],"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=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}