Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Interface Properties

Interface Properties:

You can declare properties on interface. The accessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.

Syntax:

[attributes] [new] type identifier {interface-accessors}

 

attributes (Optional):Same as properties on classes.
type:Same as properties on classes.
identifier:The property name.
interface-accessors:The property accessors..

Points to remember:

1.  The accessor of an interface property does not have a body.
2.  Use of accessors is to indicate whether the property is read-write, read-only, or write-only.

Example:

implements the Name property on the ICollage interface, while the following declaration:
 public interface ICollage
    {
        // Property declaration:
        string Name
        {
            get;
            set;
        }
 
        int Counter
         {
          get;
         }
    }

 

implements the Name property on the TraningInstitute interface, while the following declaration:

    

public interface TraningInstitute
    {
        // Property declaration:
        string Name
        {
            get;
            set;
        }
    }

 

string ICollage.Name
{
    get { return "Employee Name"; }
    set { }
}
 

 

string TraningInstitute.Name
{
    get { return "techare name Name"; }
    set { }
}

 

 

ICollage interface implemented with Teacher class:

 

    public class Teacher :ICollage
    {
        public static int numberOfTeachers;
 
        private string name;
        public string Name  // read-write instance property
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
 
        private int counter;
        public int Counter  // read-only instance property
        {
            get
            {
                return counter;
            }
        }
 
        public Teacher()  // constructor
        {
            counter = ++counter + numberOfTeachers;
        }
    }

  


class Program
    {
 
      static void Main(string[] args)
        {
            Console.Write("Enter number of Teacher: ");
            string s = Console.ReadLine();
            Teacher.numberOfTeachers = int.Parse(s);
            Employee e1 = new Employee();
            Console.Write("Enter the name of the new Teacher: ");
            e1.Name = Console.ReadLine();
            Console.WriteLine("The Tecaher information:");
            Console.WriteLine("Tecaher id: {0}", e1.Counter);
            Console.WriteLine("Tecaher name: {0}", e1.Name);
 
        }
     }

Class Fields

Class Fields:

Class fields are important class members, fields also called as data members used to describe class objects.

 A field is just a class-level variable, outside any method. If you make your field public, it’s accessible using an object of your class; for example product in class product variable ProductUseCategory holds message that weather product is expired or active.

Syntax:

Access_modifier class class_name
{
//fields
Access_modifier data_type field_name;
}

Example:

You can also initialize fields with initializes, which are just like the initial values you assign to variables.

public class ChkIsProductValid
    {
    //field
     string ProductUseCategory;
     //field with initilization
     public string ActiveMessage = "Product is Active.";
     public string ExpiredMessage = "Product is Expired.";
     public ChkIsProductValid(string cetgory)
        {
            ProductUseCategory = cetgory;
        }
 
       public void DisplayMessage()
       {
           if (ProductUseCategory == "Expired")
           {
               System.Console.WriteLine(ExpiredMessage);
           }
           else
           {
               System.Console.WriteLine(ActiveMessage);
           }
       }
 
       public bool isProductSellable()
       {
           if (ProductUseCategory == "Expired")
           {
               return false;
           }
           else
           {
               return true;
           }
       }
    }

 

Static fields:

A static field is a class field; the static field is always accessed by the class name, not the instance name. You can declare Static fields using Static keyword before field name. When you declare a static field inside a class, it can be initialized with a value. All un-initialized static fields get automatically initialized to their default values when the containing class is loaded for the first time.

1.  Static fields can be accessed directly from the class by simply using class name before field name.
2.  Static variable will be created only once and it can be shared by all objects.
3.  If we declare static variable as public, then we can access that variable with.
4.  Although a field cannot be declared as static const, a const field is essentially static in its behavior.

Example:

You can check student count using following static field example.
  public class student
       {
           public static int numberOfStudents = 0;
           public student()
           {
               numberOfStudents++;
           }
       }

 

Class and object

Class:

Class is simply blueprint or template for type of object, class describes the details of object. In real world example class Vehicle has some common properties like engine, seat and tiers for all objects car, bus, trucks these properties describes the details of object car, bus, truck.

Classes are types, but are far more powerful than the simple types like int and float. Not only can you customize your data storage using classes, but you can also add methods to classes. Classes are reference types you can create instance of class using new keyword.

Syntax:

[attributes] [modifiers] class identifier [:base-list] { class-body }[;]
Here are the parts of this statement:
Attributes (Optional)—Attributes hold additional declarative information.
Modifiers (Optional)—the allowed modifiers are new, static, virtual, abstract,
Override and a valid combination of the four access modifiers.
Identifier—the class name.
Base-list (Optional)—A list that contains the base class and any implemented interfaces,
Separated by commas.
Class-body—Declarations of the class members.

Object:

Object is termed as an instance of a class, and it has its own state, behavior and identity. To create an object or instance of a class, you use the new keyword.

Syntax:

Class_Name  Object_Name=New Class_Name (Optional parameters depends upon class constructor);

Example for class and object:

class Program
    {
 
        static void Main(string[] args)
        {
            Product MicromaxMobile = new Product("NotExpired");
            
            var isProductavailable= MicromaxMobile.isProductSellable();
        }
     }
 


public class Product
    {
        string ProductUseCategory;
 
        public Product(string cetgory)
        {
            ProductUseCategory =cetgory;
        }
       
        public int ProductID { get; set; }
 
        public string ProductName { get; set; }
      
        public string Description { get; set; }
 
        public double? UnitPrice { get; set; }
 
        public int? CategoryID { get; set; }
       
        public bool isProductSellable()
        {
            if (ProductUseCategory== "Expired")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
       
    }