Saturday, 15 April 2017

CSHARP CLASSES ~ mkniit

C# CLASSES

In many programming tutorials, will store information about category for much later. However, because C # is all about object-oriented programming and then categories, we will create a basic introduction to things that matter more now.

First class collection of relevant methods and variables. Category that describes that, in most cases, you can create an instance of this class, which is now referred to as objects. On this object, you can use the method that is defined and variable. Of course, you can create multiple instances of your classes because you want to. Classes, and object-oriented programming in General, a huge subject. We will cover some of them in this chapter also in later chapters, but not all.


When you define a class, you can set the schema for the data type. This actually doesn't know any data, but knows the meaning of the name class. What is the object class consists of and what operations can be performed on the object. An example of object class. Methods and variables class called class members.


<access specifier> class  class_name
{
   // member variables
   <access specifier> <data type> variable1;
   <access specifier> <data type> variable2;
   ...
   <access specifier> <data type> variableN;
   // member methods
   <access specifier> <return type> method1(parameter_list)
   {
      // method body
   }
   <access specifier> <return type> method2(parameter_list)
   {
      // method body
   }
   ...
   <access specifier> <return type> methodN(parameter_list)
   {
      // method body
   }
}

In this chapter, we saw the Hello world class that is used for the first time, because everything is in C#, built on top of the class. Let's extend the example our Hello world with a class we have built in our area.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class tutorialsProgrammings
    {
        static void Main(string[] args)
        {
            Pen pen;
            pen = new Pen("Red");
            Console.WriteLine(pen.Describe());
            pen = new Pen("Green");
            Console.WriteLine(pen.Describe());
            Console.ReadLine();
        }
    }
    class Pen
    {
        private string color;
        public Pen(string color)
        {
            this.color = color;
        }
        public string Describe()
        {
            return "This color pen is " + Color;
        }
        public string Color
        {
            get { return color; }
            set { color = value; }
        }
    }
}

When the above code is compiled and executed, the result is:
C# CLASSES

Here we learn many new things, but almost all of them are based on things that we have been learned on a previous tutorial. As you can see, we've defined a new class, called Pen. Expressed in the same project as our main application instance. However, a new class is usually defined in the project of their own. It defines a variable called color, which of course is used to tell the color of the pen. We are declared as private, which is good practice-to access the variable from outside must be made using the properties. The color property defined at the end of the class, giving access to a variable color.

Constructor

The constructor is a special method, used when instantiating the class. A constructor can never go back to nothing, which is why you do not need to specify the type of returns for it. The normal method is defined like this:

public string Tutorials();

A constructor can be defined like this:

public Pen();

In our example this chapter, we have a Pen class, with the constructor that takes a string as an argument. Of course, the constructor can be overloaded too, means we can have several constructors, with the same name but different parameters. Here is an example:

public Pen()
   {
           
   }
public Pen(string color)
   {
      this.color = color;
   }

A Constructor above can call another constructor, which can come in handy in several situations. Here is an example:

public Pen()
   {
      Console.WriteLine(“Constructor with no parameters called!”);
   }
public Pen(string color) : this()
   {
      this.color = color;
      Console.WriteLine(“Constructor with color parameters called!”);
   }

If you run this code, you will notice that the constructor with no parameters is called first. This can be used for instantiating numerous objects in the default constructor for the class, which can be called from another constructor of the class. If you want to call the constructor takes parameters, you can do it too. The following is a simple example:

public Pen(string color) : this()
   {
      this.color = color;
      Console.WriteLine(“Constructor with color parameters called!”);
   }
public Pen(string param1, string param2) : this(param1)
   {

   }

If you call the constructor which takes 2 parameters, the first parameter will be used to invoke the constructor that takes 1 parameter.

Destructors

Because C # is garbage collected, meaning that framework will liberate objects that you no longer use, there may be a time when you need to perform some manual cleanup. Evil, a method called once discarded objects, can be used for cleaning the resources used by the object. Destructors do not look very similar to other methods in C #. The following is an example of evil for our car class:

~Pen()
   {
     Console.WriteLine(“Out”);
   }

Once the object is collected by the garbage collector, this method is called.
Static member of class C#

We can define the members of a static class using the static keyword. When we declare a static class member, it means that no matter how many objects of the class are created, there is only one copy of a static member.

The keyword static implies that only one instance of existing members for the class. Static variables are used to define constants because their values can be obtained by applying the class without creating an instance of it. Static variables can be initialized outside of the definition of the function or class members. You can also initialize a static variable inside a class definition.
The following example demonstrates the use of static variables:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class StaticVariable
    {
        public static int number;
        public void counter()
        {
            number++;
        }
        public int getNumber()
        {
            return number;
        }
    }
    class StaticTutorialsProgrammings
    {
        static void Main(string[] args)
        {
            StaticVariable sv1 = new StaticVariable();
            StaticVariable sv2 = new StaticVariable();
            sv1.counter();
            sv1.counter();
            sv1.counter();
            sv2.counter();
            sv2.counter();
            sv2.counter();
            Console.WriteLine("Variable number for sv1: {0}", sv1.getNumber());
            Console.WriteLine("Variable numer for sv2: {0}", sv2.getNumber());
            Console.ReadKey();
        }
    }
}

When the above code is compiled and executed, the result is:
C# CLASSES

No comments:

Post a Comment