Structs in C#

This article is for beginners. In this article we will cover :-
  • What is struct? How to define struct.
  • What is the use of "new" operator in struct.
  • Demo code with proof of concept (poc).

Introduction
Structs are similar as class. These are the value types which means they will store in the stack. It can be declare by keyword struct.
  • A struct is always derived from System.ValueType, which in turn derives from System.Object.
  • We are not permitted to define a constructor that takes zero argument inside a struct whereas we can define a parameterized constructor.
  • The new operator work differently in struct. Instead of allocating memory on the heap, the new operator simply calls the appropriate constructor, according to the parameters passed to it.
  • In struct we cannot initialize instance member variable. It will give compilation error.
  • Struct do not support inheritance.
 Demo
namespace StructDemo
{
  public struct Employee
  {
    private int _age;
 
   //public int age=5; // can't initialize instance variable it will give compilation error as: cannot have instance field initializers in structs
 
   //public Employee() { } //can't define default constructor, compilation error occurs as: Structs cannot contain explicit parameterless constructors
   
  public Employee(int age)
  {
   _age = age;
  }
  public int Age
  {
   set { _age = value; }
   get { return _age; }
  }
  public void display()
  {
   Console.WriteLine("Age::" + _age);
  }
 }
 
 class Program
 {
  static void Main(string[] args)
  {
    //creating employee object with default constructor   
   Employee e1 = new Employee();
   e1.Age = 20;
   Console.WriteLine("calling default constructor..");
   e1.display();
     
   //To call parameterized constructor use "new" operator.   
   Employee e2 = new Employee(25);
   Console.WriteLine("calling parameterized constructor..");
   e2.display();
   Console.ReadLine();
  }
 }
}
 
 Output
 












 

Comments

  1. Dot Net is an ever trending technology where it is more preferable by developers to utilize the features in the dot net language. Your article on Dot Net language proves that it is an evergreen technology in the IT market.
    Regards:
    dot net training institutes in chennai
    dotnet training in chennai

    ReplyDelete
  2. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.

    Hadoop Training in Chennai

    ReplyDelete
  3. Great blog! Really awesome I got more information from this blog. Thanks for sharing with us.
    Regards,
    Dot Net Training in Chennai

    ReplyDelete
  4. Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 
    Click here:
    angularjs4 Training in Chennai
    Click here:
    angularjs5 Training in Chennai

    ReplyDelete
  5. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    Devops training in tambaram
    Devops training in velachery
    Devops training in annanagar

    DevOps online Training

    ReplyDelete
  6. Really you have done great job,There are may person searching about that now they will find enough resources by your post
    Blue Prism Training in Pune

    Blueprism training in tambaram

    Blueprism training in annanagar

    ReplyDelete
  7. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging

    DevOps online Training|DevOps Training in USA

    ReplyDelete
  8. The site was so nice, I found out about a lot of great things. I like the way you make your blog posts. Keep up the good work and may you gain success in the long run.
    Blueprism training in tambaram

    Blueprism training in annanagar

    Blueprism training in velachery

    ReplyDelete
  9. Well done! Pleasant post! This truly helps me to discover the solutions for my inquiry. Trusting, that you will keep posting articles having heaps of valuable data. You're the best! 
    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  10. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    angularjs interview questions and answers

    ReplyDelete
  11. I am really very happy to find this particular site. I just wanted to say thank you for this huge read!! I absolutely enjoying every petite bit of it and I have you bookmarked to test out new substance you post.

    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    ReplyDelete
  12. Hello! This is my first visit to When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several your blog!
    safety course in chennai

    ReplyDelete
  13. Have you been thinking about the power sources and the tiles whom use blocks I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out the new stuff you post
    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  14. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    Data science Course Training in Chennai | Data Science Training in Chennai
    RPA Course Training in Chennai | RPA Training in Chennai
    AWS Course Training in Chennai | AWS Training in Chennai

    ReplyDelete
  15. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.

    GST Course in Delhi

    ReplyDelete
  16. Useful Information :

    Looking for the Best Digital Marketing Company in Vijayawada and Hyderabad affiliate agency in south INDIA, Digital Marketing in Vijayawada @ praiseads.com

    ReplyDelete
  17. Thanks a lot for sharing such a good source with all, i appreciate your efforts taken for the same. I found this worth sharing and must share this with all.




    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery





    ReplyDelete

Post a Comment

Popular posts from this blog

How to start a free blog with Blogger in simple steps

How to verify/add a blogger blog in Google Webmaster Tool or Search Console. How to verify your site's ownership.