Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C#: Declaring class

Class in java declared with a keyword "class"

Syntax
class ClassName{
}

Example
class Person{
 }

How to declare properties of a class?
Each property is represented by a variable.
                                   
class Person
{
    float Height, weight;
    String color, country;
}

Since height and weight are real type values, so these are declared as a float. Whereas color and country are of type String. So these are declared as type string.

How to define behaviors to a class?
 Each behavior in C# represented by a method.

  Method is a block of statements, with a name associated with it.
      Syntax:
         returnType methodName(parameters){

         }
      Example
         int sum(int a, int b){
            return (a+b);
         }

      here,
         returnType is int
         parameters are a, b.

Return value must match with the return type of the method.
After adding the behaviors for the class Person, code changed like below      

class Person
{
    float height, weight;
    String color, country;

    public float getHeight()
    {
        return height;
    }

    public Void setHeight(float height)
    {
        this.height = height;
    }

    public float getWeight()
    {
        return weight;
    }

    public void setWeight(float weight)
    {
        this.weight = weight;
    }

    public String getColor()
    {
        return color;
    }

    public void setColor(String color)
    {
        this.color = color;
    }

    public String getCountry()
    {
        return country;
    }

    public void setCountry(String country)
    {
        this.country = country;
    }

}

Following is the complete working application.

Person.cs
using System;

class Person
{
float height, weight;
String color, country;

public float getHeight()
{
return height;
}

public void setHeight(float height)
{
this.height = height;
}

public float getWeight()
{
return weight;
}

public void setWeight(float weight)
{
this.weight = weight;
}

public String getColor()
{
return color;
}

public void setColor(String color)
{
this.color = color;
}

public String getCountry()
{
return country;
}

public void setCountry(String country)
{
this.country = country;
}

}

class Program
{

static void printPerson(Person person)
{
Console.WriteLine("Color : {0}", person.getColor());
Console.WriteLine("Country : {0}", person.getCountry());
Console.WriteLine("Weight : {0}", person.getWeight());
Console.WriteLine("Height : {0}", person.getHeight());
}

static void Main(string[] args)
{
Person person1 = new Person();
person1.setColor("White");
person1.setCountry("India");
person1.setHeight(5.9f);
person1.setWeight(67.5f);

printPerson(person1);
}

}

Output
Color : White
Country : India
Weight : 67.5
Height : 5.9



Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

C#: Declaring class

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×