Home

C# Example: Simple Class

 

Introduction

Here is an example of using a simple class in C#:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    public class House
    {
        public char TypeOfHome;
        public int NumberOfBedrooms;
        public double NumberOfBathrooms;
        public byte Stories;
        public bool HasCarGarage;
        public int YearBuilt;
        public double Value;
    }

    class Program
    {
        static void Main(string[] args)
        {
            House condominium = new House();
            
            condominium.HasCarGarage = false;
  	    condominium.YearBuilt = 2002;
	    condominium.NumberOfBathrooms = 1;
	    condominium.Stories = 18;
	    condominium.Value = 155825;
	    condominium.NumberOfBedrooms = 2;
	    condominium.TypeOfHome = 'C';

	    Console.Write("Type of Home:        ");
	    Console.WriteLine(condominium.TypeOfHome);
	    Console.Write("Number of Bedrooms:  ");
	    Console.WriteLine(condominium.NumberOfBedrooms);
	    Console.Write("Number of Bathrooms: ");
	    Console.WriteLine(condominium.NumberOfBathrooms);
	    Console.Write("Number of Stories:   ");
	    Console.WriteLine(condominium.Stories);
	    Console.Write("Year Built:          ");
	    Console.WriteLine(condominium.YearBuilt);
	    Console.Write("Has Car Garage:      ");
	    Console.WriteLine(condominium.HasCarGarage);
	    Console.Write("Monetary Value:      ");
	    Console.WriteLine(condominium.Value);
        }
    }
}

This would produce:

Type of Home:        C
Number of Bedrooms:  2
Number of Bathrooms: 1
Number of Stories:   18
Year Built:          2002
Has Car Garage:      False
Monetary Value:      155825
Press any key to continue . . .

 

 

Home Copyright 2006 FunctionX, Inc.