<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Edvin</title>
    <description>The latest articles on DEV Community by Edvin (@edvin).</description>
    <link>https://dev.to/edvin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F462461%2F0c981371-d6cd-4eea-983f-6498e7da2d02.jpeg</url>
      <title>DEV Community: Edvin</title>
      <link>https://dev.to/edvin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edvin"/>
    <language>en</language>
    <item>
      <title>What is a Class? (C#)</title>
      <dc:creator>Edvin</dc:creator>
      <pubDate>Tue, 17 Nov 2020 15:02:02 +0000</pubDate>
      <link>https://dev.to/edvin/what-is-a-class-c-example-59en</link>
      <guid>https://dev.to/edvin/what-is-a-class-c-example-59en</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Object-Oriented Programming (OOP for short) is something many of us are introduced to early in our programming journey.&lt;br&gt;
A particular term that is very common to hear about, are &lt;strong&gt;classes&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition
&lt;/h2&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;A class is a blueprint of an object that we can imagine.&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;There are countless definitions of what a class is. &lt;br&gt;
If you are having trouble grasping the concreteness of it, do not worry. &lt;br&gt;
Sometimes it is best to learn by looking at examples, and not spending five and a half hours on a Tuesday night researching - just so you can finish that stupid school essay.&lt;br&gt;
That is based on a true story. &lt;/p&gt;




&lt;p&gt;In programming, the terms &lt;strong&gt;class&lt;/strong&gt; and &lt;strong&gt;object&lt;/strong&gt; are often used together. This is because: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;class&lt;/strong&gt; is a blueprint for an &lt;strong&gt;object&lt;/strong&gt;. (The "concept" of a dog)&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;object&lt;/strong&gt; is a particular instance of a &lt;strong&gt;class&lt;/strong&gt;. (Your neighbors white labrador Willy, that barks consistently at 3AM)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O-B7Vcek--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Labrador_on_Quantock_%25282175262184%2529.jpg/800px-Labrador_on_Quantock_%25282175262184%2529.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O-B7Vcek--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Labrador_on_Quantock_%25282175262184%2529.jpg/800px-Labrador_on_Quantock_%25282175262184%2529.jpg" alt="That loud dog I mentioned."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;Dogs, sailboats, cake and machine-guns are all examples of what a class could be. &lt;br&gt;
There are an endless amount of options, as it is up to you as the programmer to define them in your application.&lt;/p&gt;




&lt;p&gt;Let us use a &lt;strong&gt;book&lt;/strong&gt; as an example. A book is a real-world object that is commonly found all across the world. &lt;br&gt;
In order to make a class that represents a book, we need to think about what &lt;strong&gt;properties&lt;/strong&gt; a book has. When I say 'properties', I really mean the features that makes a book - a book.&lt;/p&gt;

&lt;p&gt;For myself, I can think of these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Author&lt;/li&gt;
&lt;li&gt;Date published&lt;/li&gt;
&lt;li&gt;Cover photo&lt;/li&gt;
&lt;li&gt;Number of pages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let us now try to implement the concept of a Book into code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code 1 - Getting into the Code
&lt;/h3&gt;

&lt;p&gt;I have created a project called "Playground" in Visual Studio 2019. After creating this project, I went to File -&amp;gt; New -&amp;gt; File and then selected the "Class" option. &lt;br&gt;
I named mine "Book", and it gets the extension &lt;em&gt;.cs&lt;/em&gt;. This is common for C# classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b3Z8zeFz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/07ctdhndvfescdppyz9b.PNG" alt="The values arranged in the Book.cs class"&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Code 2 - Constructor
&lt;/h3&gt;

&lt;p&gt;A class needs a &lt;strong&gt;constructor&lt;/strong&gt; in order to define how an object is created. This is where you pass in the specific characteristics for a particular object. &lt;br&gt;
The constructor is seen on line 14 to line 20, and only has the job of setting the properties we give it, to that book-objects values. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xU6Kpm4j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/aba16lgshxrbeliduvuc.PNG" alt="A constructor for our book class"&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Code 3 - A method
&lt;/h3&gt;

&lt;p&gt;Let us now create a &lt;strong&gt;method&lt;/strong&gt; that will print out a book's values to the console window. A method is a way for an object to perform a set of actions / instructions. &lt;br&gt;
The &lt;code&gt;PrintBookDetails()&lt;/code&gt; method only has the job of printing these values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Author&lt;/li&gt;
&lt;li&gt;Published date&lt;/li&gt;
&lt;li&gt;Number of pages&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;As an "Image" would be strange to print to the console window, I have ignored that value for now. &lt;br&gt;
&lt;strong&gt;NOTE:&lt;/strong&gt; The Image class is a class I created myself. If you try to replicate these steps, you might see that your code will get red underlines here. &lt;br&gt;
If so, just ignore the cover-image property of the Book class.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pg1sxT1f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jcwr7r7mjsy8wotk867p.PNG" alt="A method that performs a set of instructions, in this case printing values to the screen"&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Code 4 - The entry-point
&lt;/h3&gt;

&lt;p&gt;This application has a class called "Program.cs". This is a file you get after creating a new Console Application in Visual Studio. &lt;br&gt;
&lt;code&gt;private static void Main(string[] args)&lt;/code&gt; might look weird, but all you need to know is that it is the application's &lt;strong&gt;entry-point&lt;/strong&gt;.&lt;br&gt;
Everything inside the &lt;br&gt;
&lt;code&gt;private static void Main(string[] args)&lt;/code&gt;&lt;br&gt;
block defines what happens the moment your program is being started, like when you double-click a program on your desktop.&lt;/p&gt;

&lt;p&gt;In our case, we are just creating a &lt;code&gt;Book&lt;/code&gt; object in the way we defined it in our constructor earlier.&lt;br&gt;
This book has the values: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Albert (for the Author-name)&lt;/li&gt;
&lt;li&gt;The date 17th of November, 2000 (for the published date)&lt;/li&gt;
&lt;li&gt;A cover-image :)&lt;/li&gt;
&lt;li&gt;146 pages&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qWmpjSeJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gjg8h8g79zmh9n7gg17p.PNG" alt="The entry-point, i.e what happens when we run our program"&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Code 5 - Printing the values
&lt;/h3&gt;

&lt;p&gt;Nothing will really happen right now as we start the program, however. &lt;br&gt;
While a book-object is created from the &lt;code&gt;Book&lt;/code&gt; class, we have not specified anything that should be printed to the screen. &lt;/p&gt;

&lt;p&gt;Let us use the &lt;code&gt;PrintBookDetails()&lt;/code&gt; method that we created earlier, which actually prints out values. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--06IPIc9h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wd4vxlwtpnqhofoo8kpo.PNG" alt="Using a method"&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Code 6 - The result
&lt;/h3&gt;

&lt;p&gt;By now running our program, we can see that the console window displays the properties of Albert's book.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cHg-0kLj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zros06tcorc9kt54ocgj.PNG" alt="Values being printed onto the screen"&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;This is just one example of how you could model a class in your application. I highly encourage you to do a class-modelling exercise yourself. &lt;br&gt;
Look around in your room, and fixate on an object you see. Write down its properties, and consider how you could implement this in code.&lt;/p&gt;

&lt;p&gt;I am new to writing articles like these, so I would love your feedback if you have any. :)&lt;/p&gt;

&lt;p&gt;Feel free to contact me if you have a question about this article (or what I discussed), and I hope you have a great journey with your dream-project. &lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
