Skip to main content

Structure,Property,Indexer in c#


Date
Monday, December 02, 2013
Time
Description
10.00 am to 12.00 pm
Structure,property,indexer
Learning:
Structure:-

Structures are used to represent a record.
User defined value types are known as structures.Structures are stored on stack and contains values directly.

eg:-
struct cycle
{
 Int val, min_speed,max_speed;

};
Static void main(string[] args)
{
  Cycle c;
   c.val=10;
   c.min_speed=30;
   c.max_speed=50;
console..writeline(“max speed is {0}”,c.max_speed);
}

 Features of c# structures:-

     1)Structures can have methods, fields, indexers, properties, events.
     2)Structures can have defined constructors and not destructors.however you cannot define a default constructor to the structure.
     3)Structure members cannot be specified as abstract, virtual, protected.

Properties:-

Properties are named members of classes,structures and interfaces.Member variable or methods in a class or structure are called fields. Properties are an extension of fields and are accessed using the same syntax. They use accessor through which the value can be read and manipulated.

Accessor:- it contains executable statements that helps in getting and setting the values of the properties.

Program:-
class Student
   {

      private string code = "N.A";
      private string name = "not known";
      private int age = 0;

      // Declare a Code property of type string:
      public string Code
      {
         get
         {
            return code;
         }
         set
         {
            code = value;
         }
      }
  
      // Declare a Name property of type string:
      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }

      // Declare a Age property of type int:
      public int Age
      {
         get
         {
            return age;
         }
         set
         {
            age = value;
         }
      }
      public override string ToString()
      {
         return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
      }
    }
    class ExampleDemo
    {
      public static void Main()
      {
         // Create a new Student object:
         Student s = new Student();
           
         // Setting code, name and the age of the student
         s.Code = "001";
         s.Name = "Zara";
         s.Age = 9;
         Console.WriteLine("Student Info: {0}", s);
         //let us increase age
         s.Age += 1;
         Console.WriteLine("Student Info: {0}", s);
         Console.ReadKey();
       }
   }
}

Output:-
Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10


Indexer:-

An indexer allows an object to be indexed as an array. When an indexer is defined for a class then class behaves like a virtual array. The instance of the class can be accessed by array access operators([ ]).

Program:-

  class IndexedNames
   {
      private string[] namelist = new string[size];
      static public int size = 10;
      public IndexedNames()
      {
         for (int i = 0; i < size; i++)
         namelist[i] = "N. A.";
      }
      public string this[int index]
      {
         get
         {
            string tmp;
 
            if( index >= 0 && index <= size-1 )
            {
               tmp = namelist[index];
            }
            else
            {
               tmp = "";
            }
 
            return ( tmp );
         }
         set
         {
            if( index >= 0 && index <= size-1 )
            {
               namelist[index] = value;
            }
         }
      }
 
      static void Main(string[] args)
      {
         IndexedNames names = new IndexedNames();
         names[0] = "Zara";
         names[1] = "Riz";
         names[2] = "Nuha";
         names[3] = "Asif";
         names[4] = "Davinder";
         names[5] = "Sunil";
         names[6] = "Rubic";
         for ( int i = 0; i < IndexedNames.size; i++ )
         {
            Console.WriteLine(names[i]);
         }
         Console.ReadKey();
      }
   }
}

Output :-

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.






Comments

Popular posts from this blog

Session 3 :- Control Structures in c#

Date Friday, November 29, 2013 Time Description 10.00 am to 12.00 pm Control structure in c# Learning :   For Loops Why is this important to learn about? Control structures are one of the most fundamental concepts in any programming language you will come across. If you want to know C#, and if you want to learn to program with any language, you must have a firm grip on the control structures of the language. Definitions of terms used . Iteration – This is the act of repeating something (that ‘something’ being code statements in the programming context). Conditional – A conditional action is said to be one that is only performed if a certain condition is true. For Loops For loops allow us to specify the number of times to repeat a block of code. It is best demonstrated with an example: for (int i = 1; i <= 5; i++) {    Console...
Oop's Concept .Net is purely object oriented Language because it strongly support four major pillars  object oriented language.   four Major Pillars are : -      1) Abstraction.      2) Encapsulation      3) Inheritance      4) Polymorphism .Net  also support three minor pillars : -     1) Strongly Type Cast     2) Persistent     3) Concurrency *** ABSTRACTION ***        In simple words  “Abstraction captures only those details about an object that are relevant to the current perspective.”   In object-oriented programming theory, abstraction involves the facility to define objects that represent abstract “actors” that can perform work, report on and change their state, and “communicate” with other objects in the system . Abstraction is a concept which hide complexity. *** ENCAPSULATION ***      Encaps...

Inheritance in c#

Inheritance is one of the pillars of Object Oriented Programming. It allows the child class to reuse, extend and modify the behavior of parent class. The class whose members are inherited is called as base class and class who inherits is called a derived class. We can inherit only one class at a time. Multiple inheritance is not supported in c#. A struct cannot inherit any base class doing so will generate a compiler error. 1.       Private members are only accessible to the derived class which is nested in the inherited base class. 2.       Protected members are accessible only in derived class. 3.       Internal members are accessible in the derived class that is visible in the same assembly of the base class. 4.       Public members are accessible in the derived class. They can be called just as if they were declared in the derived class. All Types are implicitly inherited ...