{"id":273,"date":"2022-01-02T15:59:10","date_gmt":"2022-01-02T08:59:10","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=273"},"modified":"2022-06-03T13:28:44","modified_gmt":"2022-06-03T06:28:44","slug":"csharp-class","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-class\/","title":{"rendered":"C# class"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the C# class and how to define a custom class. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the objects and classes<\/h2>\n\n\n\n<p>Objects are one of the essential concepts in object-oriented programming. Objects have states and behaviors: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>States represent the data that the object holds at a particular point in time. <\/li><li>Behaviors represent the actions that the object can perform to manipulate its states.<\/li><\/ul>\n\n\n\n<p>C# uses class-based object-oriented programming. Before creating objects, you need to define a class. A class is a blueprint for creating objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Define a class<\/h2>\n\n\n\n<p>To define a new class, you use the <code>class<\/code> keyword followed by the class name. By convention, a class name is in the Pascal case, such as <code>Person<\/code>, <code>SalesPerson<\/code>, etc.<\/p>\n\n\n\n<p>To create a new class, you follow these steps: <\/p>\n\n\n\n<p>First, create a new <code>Person.cs<\/code> file. <\/p>\n\n\n\n<p>Second, define the <code>Person<\/code> class in the <code>Person.cs<\/code> file:<\/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\">class<\/span> <span class=\"hljs-title\">Person<\/span>\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<h2 class=\"wp-block-heading\">Create objects from the class<\/h2>\n\n\n\n<p>To create objects from the <code>Person<\/code> class, you follow these steps:<\/p>\n\n\n\n<p>First, create the <code>Program.cs<\/code> file.<\/p>\n\n\n\n<p>Second, create a new object from the <code>Person<\/code> class by using the <code>new<\/code> keyword:<\/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\">Person p1 = <span class=\"hljs-keyword\">new<\/span> Person();<\/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>Or you can use the <code>var<\/code> keyword:<\/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-keyword\">var<\/span> p1 = <span class=\"hljs-keyword\">new<\/span> Person();<\/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>Or <\/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\">Person p1 = <span class=\"hljs-keyword\">new<\/span>();<\/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>In this example, the <code>p1<\/code> is a new object of the <code>Person<\/code> class. It is also called an instance of the <code>Person<\/code> class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add fields to the class<\/h2>\n\n\n\n<p>The following add three fields to the <code>Person<\/code> class: <code>FirstName<\/code>, <code>LastName<\/code>, and <code>Age<\/code>. These fields represent the states of a <code>person<\/code> object:<\/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-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    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">byte<\/span> Age;\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>The <code>public<\/code> keyword controls the access level to access the fields from both inside and outside the <code>Person<\/code> class. See the <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/c-public-private\/\">public &amp; private access modifier tutorial<\/a> for more information.<\/p>\n\n\n\n<p>Since the <code>Person<\/code> class has three fields, and all of its objects can access them. For example, the following creates a new instance of the <code>Person<\/code> class and assigns the values to each field:<\/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-keyword\">using<\/span> HR;\n\nPerson p1 = <span class=\"hljs-keyword\">new<\/span>();\n\np1.FirstName = <span class=\"hljs-string\">\"John\"<\/span>;\np1.LastName = <span class=\"hljs-string\">\"Doe\"<\/span>;\np1.Age = <span class=\"hljs-number\">25<\/span>;<\/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>From the <code>Person<\/code> class, you can create as many objects as you want to. For example, the following creates two Person&#8217;s objects with the names <code>p1<\/code> and <code>p2<\/code>:<\/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-keyword\">using<\/span> HR;\n\nPerson p1 = <span class=\"hljs-keyword\">new<\/span>();\np1.FirstName = <span class=\"hljs-string\">\"John\"<\/span>;\np1.LastName = <span class=\"hljs-string\">\"Doe\"<\/span>;\np1.Age = <span class=\"hljs-number\">25<\/span>;\n\n\nPerson p2 = <span class=\"hljs-keyword\">new<\/span>();\np2.FirstName = <span class=\"hljs-string\">\"Jane\"<\/span>;\np2.LastName = <span class=\"hljs-string\">\"Doe\"<\/span>;\np2.Age = <span class=\"hljs-number\">22<\/span>;<\/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>Note that <code>p1<\/code> and <code>p2<\/code> have the same set of fields. However, their field values are different.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add methods to a class<\/h2>\n\n\n\n<p>The following adds the <code>GetFullName()<\/code> method to the <code>Person<\/code> class:<\/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\"><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    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">byte<\/span> Age;\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\">{FirstName}<\/span> <span class=\"hljs-subst\">{LastName}<\/span>\"<\/span>;\n    }\n}<\/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>The <code>GetFullname()<\/code> is like a <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-functions\/\">function<\/a> with the <code>public<\/code> keyword. When you define a function inside a class, it is called a method. <\/p>\n\n\n\n<p>The <code>GetFullName()<\/code> method concatenates the first name with the last name and returns the full name as a string. <\/p>\n\n\n\n<p>The <code>public<\/code> keyword indicates that it is accessible from both inside and outside the <code>Person<\/code> class.<\/p>\n\n\n\n<p>To call the <code>GetFullName()<\/code> method from a <code>Person<\/code> object, you use the object name, dot operator, and the method name. For example:<\/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\"><span class=\"hljs-comment\">\/\/ Program.cs<\/span>\n\nPerson p1 = <span class=\"hljs-keyword\">new<\/span>();\np1.FirstName = <span class=\"hljs-string\">\"John\"<\/span>;\np1.LastName = <span class=\"hljs-string\">\"Doe\"<\/span>;\np1.Age = <span class=\"hljs-number\">25<\/span>;\nConsole.WriteLine(p1.GetFullName());\n\n\nPerson p2 = <span class=\"hljs-keyword\">new<\/span>();\np2.FirstName = <span class=\"hljs-string\">\"Jane\"<\/span>;\np2.LastName = <span class=\"hljs-string\">\"Doe\"<\/span>;\np2.Age = <span class=\"hljs-number\">22<\/span>;\nConsole.WriteLine(p2.GetFullName());<\/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>Output:<\/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\">John Doe\nJane Doe<\/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>Objects have states and behaviors.<\/li><li>C# uses a class-based object-oriented programming approach.<\/li><li>A class is a blueprint for creating objects. Objects are instances of a class.<\/li><li>Use the <code>class<\/code> keyword to define a class.<\/li><li>A class has fields and methods.<\/li><li>Use the <code>new<\/code> keyword to create a new object from a class.<\/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=\"273\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-class\/\"\n\t\t\t\tdata-post-title=\"C# class\"\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=\"273\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-class\/\"\n\t\t\t\tdata-post-title=\"C# class\"\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 the C# class and how to define a custom class. Introduction to the objects and classes Objects are one of the essential concepts in object-oriented programming. Objects have states and behaviors: States represent the data that the object holds at a particular point in time. Behaviors represent the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":27,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-273","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/273","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=273"}],"version-history":[{"count":4,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/273\/revisions"}],"predecessor-version":[{"id":691,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/273\/revisions\/691"}],"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=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}