R N REDDY .Net Community - C#
Sunday, 2016-12-04, 11:50 PM
Welcome Guest | RSS
Site menu
Tag Board
500
Our poll
How is .Net classes by R.N.Reddy sir ?
Total of answers: 230
Statistics

Total online: 1
Guests: 1
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 2016  »
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
25262728293031

Twitter
 

Entries archive

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