Jan 14, 2020

STRUCTURE IN C-SHARP


STRUCTURE IN C-SHARP
structure in c#,c# data structures,c# struct inheritance,c# program structure,c# struct example,c# struct example
 c# struct

               " Structure is a user define type, which contain almost all the member that a class have such as in structure it can contain field, method , constructor, properties and operator method etc".
                Where in C language the structure contain only the fields.

DEFINING A STRUCTURE

                It almost similar to a class having modifiers  Structure name and of course struct it is the keyword to define a structure.
Syntax
[<modifiers>]  struct <name>
{
   Member definition;
}
The only one difference to defining a class and structure is the keyword class use class as keyword and structure use struct as keyword to defining a structure and the rest all same in both.
Instantiating instance of class and calling process of class and structure is also same we did it like this
Classname Obj=new Classname;                                     //instantiating the instance in class
Obj. CLassmembers                                                        // calling the members of class
                This gona same in structure also like this
Structurename Obj=new Structurename;                         //instantiating the instance of structure
Obj. CLassmembers                                                        // calling the members of structure

Example of structure:

Case 1 (used new keyword to creating instance)
               Field x is initialized by` constructor
namespace Teststrucrture
{
    public struct Airthemetic
    {
         int x;
        public void Add(int a, int b)
        {
            Console.WriteLine(a + b);
        }
        public void Sub(int a,int b)
        {
            Console.WriteLine(a - b);
        }
        static void Main()
        {
            Airthemetic a = new Airthemetic();
            a.Add(21, 20);
            a.Sub(34, 21);
            Console.ReadKey();
        }
    }
}

Case 2(without use new keyword)
                Field x is explicitly initialized
namespace Teststrucrture
{
    public struct Airthemetic
    {
         int x;
        public void Add(int a, int b)
        {
            Console.WriteLine(a + b);
        }
        public void Sub(int a,int b)
        {
            Console.WriteLine(a - b);
        }
        static void Main()
        {
            Airthemetic a;  a.x = 3;
            a.Add(21, 20);
            a.Sub(34, 21);
            Console.ReadKey();
        }
    }
}


difference between structure and class in c++, diff between class and structure, difference between class and structure in c#
difference between class and structure

Difference between class and structure

Class
        -          Class is a reference type 
        -          Due to reference it have dynamic memory location
        -          If we have large amount data to store in the variables then we use class. 
        -          For the creating class instance we must have to use new keyword to instantiate.’ New’ keyword is mandatory. Like this
Classname Obj=new Classname;   
        -          While instantiating the instance in class we use new keyword and constructor then we need not to initialize the fields of a class if exist.  Because while using constructor it initialize implicitly with default values.     
         -          Classes can be inherited. Its mean class do support the inheritance.
           -        Class has one implicit constructor if there is zero constructors are defined and ‘n’ number of constructor if n number of constructor is explicitly defined in a class by programmer.
Structure
        -          Structure is a value type
        -          Value type has fixed memory location.
        -          If we have small amount data to store in the memory variables then we use structure
        -          For the creating structure instance we did not need to use new keyword to instantiate.’ New’ keyword is optional.
Like this
Structurename Obj;       
       -          Because the instantiating of structure need not require the new keyword if we create instance without using new keyword then it is mandatory to initialize the fields of structure if exist explicitly. Otherwise you can’t call.   
       -          Structure cannot be inherited by other its mean structure does not support inheritance.  
        -       Structure has one implicit constructor if no constructor is defined. And ‘n+1’ constructor if n constructor is defined in a structure.




0 comments:

Post a Comment

Please do not enter any spam link in the comment box