Thursday, 26 January 2017

STRUCTS IN C#


STRUCTS


Just like classes structs can have
1. Private Fields
2. Public Properties
3. Constructors
4.Methods


Object initializer syntax, intriduced in c# 3.0 can be used to initialize either a structor or a class
Note: There are several differences between classes and structs which we will be looking at in a later session.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;
namespace ConsoleApplication4
{
    public struct Customer
    {
        private int _id;
        private string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public int ID
        {
            get { return this._id; }
            set { this._id = value; }
        }
        public Customer(int Id, string Name)
        {
            this._id = Id;
            this._Name = Name;
        }
        public void PrintDetails()
        {
            Console.WriteLine("id= {0}, name = {1}", this._id, this._Name);
        }
     
    }
        public class Program
        {
            static void Main()
            {
                Customer C1 = new Customer(101, "Mallareddy");
                C1.PrintDetails();
                Console.ReadLine();
                Customer C2 = new Customer();
                C2.ID = 102;
                C2.Name = "Mitali";
                C2.PrintDetails();
                Console.ReadLine();
                Customer C3 = new Customer
                {
                    ID = 103,
                    Name = "AYAN"
                 
                };
                C3.PrintDetails();
                Console.ReadLine();
            }

        }
    }


=====================================================================

A struct is a value type where as a class is a reference type.
All the differences that are applicable to value types and reference types are also applicable to classes and structs.


Structs are stored on stack, where as classes are stored on the heap.
Value types hold their value in memory where they are declared, but reference types hold a reference to an object in memory.


Value types are destroyed immediately after the scope is lost, where as for reference types only the reference variable is destroyed after the scope is lost. the object is later destroyed by garbage collector. (We will talk about this in the garbage collection session).


When you copy a struct into another struct ,  a new copy of that struct gets created and modification on one struct will not affect the values contained by the other struct.


When you copy a class into another class, we only get a copy of the reference variable. Both the reference variable point to the same object on the heap. so , operations on one variable will affect the values contained by the other reference variable.  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4

{
    public class  Customer
    {
        public int ID{get; set;}
        public string Name{get;set;}
    }
        
}
        public class Program
        {
            static void Main()
            {
                int i = 0;
                
                if(i==10)
                {
                  int j = 20;
                    Customer C1 = new Customer();
                    C1.ID = 101;
                    C1.Name = "Mark";
                }
                Console.WriteLine("Hello");
                Console.ReadLine();
            
            }       
}

=======================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4

{
    public class  Customer
    {
        public int ID{get; set;}
        public string Name{get;set;}
    }
        
}
        public class Program
        {
            static void Main()
            {
                int i = 10;
                int j = i;
                j = j + 1;

                Console.WriteLine("i = {0} && j= {1}", i, j);


                Customer C1 = new Customer();

                C1.ID = 101;
                C1.Name = "malla";
                                                  // when we copy reference the memory is mapped to same location//
                Customer C2 = C1;
                C2.Name = "mark";
                Console.WriteLine("C1.Name = {0}, C2.Name = {1}", C1.Name, C2.Name);
                Console.ReadLine();              
                        }
}

> "classes" can have destructors but "struct" can not have destructors.


Classes vs Structs


> Structs can't have destructors, but classes can have destructors


> Structs cannot have explicit parameter less constructors where as a class can.


> Struct can't inherit from another class where as a class can, Both structs and classes can         inherit from an interface.



>Example of structs in the .NET Framework -int(System.int32), double(System.Double) etc..






No comments:

Post a Comment