Tuesday, 2024-12-24, 11:11 PM
Welcome Guest | RSS
Site menu
Tag Board
500
Our poll
How do you rate .Net classes ?
Total of answers: 208
Statistics

Total online: 3
Guests: 3
Users: 0

29-12-2010



Structure: To define a structure we have to use "struct" keyword.

Structure is a collection of members, it can contain variables, properties, methods, constructors, events, indexers, but it can not contain destructors.

Structure can contain constructor which are parameter less constructor which are parameter less constructor, that means structure cannot contain parametrized constructor.

Structure is value type which are stack based data types that means when we create an object for structure it will be allocating into stack memory.

Syntax to define a structure:
     <accessmodifier> struct <structure name>
     {
       // structure members
     }

 
To create an object for structure we don't require to use new operator.

Syntax to create an object for structure:
    <structure name> <object name>;

Structures will not support inheritance, due to that reason access modifier of a structure or structure member cannot be protected or protected internal.

In C#.NET we cannot have abstract structure and sealed structure.

namespace structureexample
{
  Public struct mystruct
   {
      Public int i;
      Public void mymethod()
        {
           Console.WriteLine("My method is calling");
        }
   }

   Class mainclass
   {
      Void main()
       {
          //creating object for structure
          mystruct structobj;
          structobj.i = 10;
         Console.WriteLine("i value is " +structobj.i)
         Structobj.mymethod();
         Console.ReadLine();
       }
   }
}
Output: i value is 10
             mymethod is calling

Difference between class & structure:



Constant: Constant can be called as a field, it is a member of a class.

Constant is representing a fixed value with a user defined name.

Constant value should be initialized at the time of declaration i.e., we are initializing in design time which cannot be changed in run time.

By default constants are static members due to that reason we have to access with the help of class name.

Eg: for constant:


namespace company
{
   Public class Employee
    {
       Public const int minimumworkhours = 9;
     }

   Class mainclass
    {
      void main()
       {
           Console.WriteLine("Every employee minimum working hours per day is " +Employee.minimumworkinghours);
           Console.ReadLine();
       }
    }
}
Output: Every employee minimum working hours per day is 9



Search

Time
 

 

Calendar
 
«  December 2024  »
SuMoTuWeThFrSa
1234567
891011121314
15161718192021
22232425262728
293031

Twitter
 

Entries archive

Copyright R N Reddy © 2024Developed by Nagendra, Venkat, Vijaya and Yaswanth
Get free updates as SMS to your mobile. Just register by clicking here