Structures in c# and how it is different from classes

How to implement structure in c#, understanding on structure.different between class and structure


Category: C# Tags: C#, C++ Programming, Java J2SE


Structure in c#:

A structure is a simple user-defined type, a lightweight alternative to a class. Structures are similar  to classes in that they may contain constructors, properties, methods, fields, operators, nested types, and indexers.


   **Some important points about structure:
  • Structure don't support the inheritance and destructor.
  • a class is a reference type, a structure is a value type(We will explain it latter in this article).
  • In C++ a struct is exactly like a class, except that the visibility (public versus private) is different
    by default. In C#, structs are value types, while classes are reference types.
lets have an example:
using System;
public struct location
{
public int xval;
/*cant use 'private int xval=100' assignment in struct is invalid */
public int yval;
public location(int xcoordinate, int ycoordinate)
{
xval = xcoordinate;
yval = ycoordinate;
}
public int Xval
{
get { return xval; }
set { xval = value; }
}
public int Yval
{
get { return yval; }
set { yval = value; }
}
public override string ToString()
{
return (string.Format("({0},{1})", xval, yval));
}
}


Explaination:-

                   Here we made a class location(which representing two coordinate x and y), then the constructor for set the values of xval and yval. and also two access get-set methods to access the both xval and yval.

here override string is overriding base method of string format and it will directly print the values of xval and yval.

Now second class to use this structure(location):

class struct1
{
public void myfun(location loc)
{
loc.Xval = 50;
loc.Yval = 100;
Console.WriteLine("in my function loc: {0}", loc);
}
static void Main()
{
location loc1 = new location(200, 300);
Console.WriteLine("Loc1 location: {0}", loc1);
struct1 s1 = new struct1();
s1.myfun(loc1);
Console.WriteLine("Loc1 location: {0}", loc1);
//create struct instance without new key word
location loccc; //no call constructor
loccc.xval = 20; //we can not initialize it via property first
loccc.yval = 10; //if do not initialize any one member it will genrate error
//now we can access or reassign values via property after initialize like
loccc.Xval = 22;
Console.WriteLine("loccc: {0}", loccc);
}
}


Explaination:-

                   Now we made class struct1 to use the class location we made a function myfun to set the xval and yval in location class using object loc, and print its value.

                   In main we see here we made a object loc1 of location then we passed it in myfun and it will set the values of xval and yval in location class and print it.

Now we set the both xval and yval using loccc object.

so the output will be
Loc1 location: (200,300)
in my function loc: (50,100)
Loc1 location: (200,300)
loccc: (22,10)


       Here you can see that when we print loc1 which is 200,300 and when we print loc1 as passing in myfun it is 50,100, if we print again loc1 out myfun then it will be 200,300. And if we create loccc the initialized with 20,10 and re assign xval via 22, so the print is 22,10(keep this in mind for later).

How structure is a value type and class a reference type:

in above example we have seen that when we pass loc1 in myfun, the values of it change as localy in myfun but when we again print the values outside of myfun it print again past values.
For staructure as above example:
         xval            yval          
         200             300                 Struct initialize
          50              100                 change in myfun
         200             300                  again outside the myfun


Now we concentrate on other example of class(not structure) same as above:
using System;

public class location2
{
private int xval;
private int yval;
public location2(int xcoordinate, int ycoordinate)
{
xval = xcoordinate;
yval = ycoordinate;
}
public int Xval
{
get { return xval; }
set { xval = value; }
}
public int Yval
{
get { return yval; }
set { yval = value; }
}
public override string ToString()
{
return (string.Format("({0},{1})", xval, yval));
}
}

 

Explanation:

           Here we made same members as above in structure just only difference that was a structure and it is a class.we done all same

now again make a same class to use it

class struct2
{
public void myfun(location2 loc)
{
loc.Xval = 50;
loc.Yval = 100;
Console.WriteLine("in my function loc: {0}", loc);
}
static void Main()
{
location2 loc2 = new location2(200, 300);
Console.WriteLine("Loc2 location: {0}", loc2);
struct2 s2 = new struct2();
s2.myfun(loc2);
Console.WriteLine("Loc2 location: {0}", loc2);
}
}

Explanation:
                 Here if we check output then

Loc2 location: (200,300)
in my function loc: (50,100)
Loc2 location: (50,100)


 So we can see differences in output of first example and second
         xval            yval          
         200             300                 class initialize
          50              100                 change in myfun
          50              100                 again outside the my fun

       ** here the changes in myfun remain same in whole class globally so we can say that class is a reference type and structure(changes not glov) is a value type.


Like 1 Person
Published on 22 May 2013
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 129
Questions: 9
Given Best Solutions: 9 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x