Skip to content

Desi banjara

learn and grow together

  • Azure
    • Azure Compute
      • Azure Logic Apps
      • Azure Mobile Apps
      • Azure App Service
      • Azure Serverless Computing
        • Azure Functions
    • Azure Networking services
      • Azure Networking – VNET
    • Azure Database Services
      • Azure SQL
      • Azure Data Factory
      • Azure Databricks
    • Azure Analytics Services
    • Azure Cognitive Services
    • Azure Data and Storage
    • Azure Devops
    • Azure landing zone
    • Azure IaaS
    • Azure Internet of Things (IoT)
      • Azure Machine Learning
      • Azure AI and ML services
    • Azure Migration
    • Microsoft Azure Log Analytics
  • Azure Security
    • Azure Identity and Access Management
    • Azure Active Directory
    • Azure Defender
    • Azure security tools for logging and monitoring
    • Azure Sentinel
    • Azure Sentinel – Data connectors
  • Agile Software development
    • Atlassian Jira
  • Amazon Web Services (AWS)
    • Amazon EC2
    • Amazon ECS
    • AWS Lambda
  • Google
    • Google Cloud Platform (GCP)
    • gmail api
    • Google Ads
    • Google AdSense
    • Google Analytics
    • Google Docs
    • Google Drive
    • Google Maps
    • Google search console
  • Software architecture
    • Service-oriented architecture (SOA)
    • Domain-Driven Design (DDD)
    • Microservices
    • Event-Driven Architecture
    • Command Query Responsibility Segregation (CQRS) Pattern
    • Layered Pattern
    • Model-View-Controller (MVC) Pattern
    • Hexagonal Architecture Pattern
    • Peer-to-Peer (P2P) pattern
    • Pipeline Pattern
  • Enterprise application architecture
  • IT/Software development
    • API development
    • ASP.Net MVC
    • ASP.NET Web API
    • C# development
    • RESTful APIs
  • Cybersecurity
    • Cross Site Scripting (XSS)
    • Reflected XSS
    • DOM-based XSS
    • Stored XSS attacks
    • Ransomware
    • cyber breaches
    • Static Application Security Testing (SAST)
  • Interview questions
    • Microsoft Azure Interview Questions
    • Amazon Web Services (AWS) Interview Questions
    • Agile Software development interview questions
    • C# interview questions with answers
    • Google analytics interview questions with answers
    • Javascript interview questions with answers
    • Python interview questions with answers
    • WordPress developer interview questions and answers
  • Cloud
    • Cloud computing
    • Infrastructure as a Service (IaaS)
    • Platform as a Service (PaaS)
    • Software as a Service (SaaS)
    • Zero Trust strategy
  • Toggle search form
  • CDA (Clinical Document Architecture) CDA (Clinical Document Architecture)
  • Top 50 C# interview questions with answers C# interview questions with answers
  • Microsoft SQL Server – 50+ useful Sql query SQL
  • Interview question: What are the two types of data type in C#? C# development
  • What is Azure App Services? Microsoft Azure
  • Microsoft AZ-900 Certification Exam Practice Questions – 5 Microsoft AZ-900 Certification Exam
  • Static Application Security Testing (SAST) DevSecOps
  • Microsoft AI-900 Certification Exam Practice Questions -3 Microsoft AI-900 Certification Exam

Differences between struct and classes in C# : Interview question

Posted on September 2, 2018 By DesiBanjara No Comments on Differences between struct and classes in C# : Interview question

Differences between struct and classes are:

Struct Class
Struct are value types; A variable of a structure type contains the structure’s data. Classes are reference types. A variable of a class type contains a reference to the data as a class type does.
Struct use stack allocation. Classes use heap allocation.
All struct elements are Public by default. A class variables and constants are Private by default, while other class members are Public by default.
A struct must have at least one nonshared variable or nonshared, noncustom event element. A class can be completely empty.
Struct elements cannot be declared as Protected. A class members can declare as element as Protected.
Struct variable declarations cannot specify initializers or initial sizes for arrays; class variable declarations can specify initializers or initial sizes for arrays;
Struct implicitly inherit from the System.ValueType class and cannot inherit from any other type. Classes can inherit from any class or classes other than System.ValueType
Struct are not inheritable classes are inheritable
Struct are never terminated, so the common language runtime (CLR) never calls the Finalize method on any structure classes are terminated by the garbage collector (GC), which calls Finalize on a class when it detects there are no active references remaining
A struct does not require a constructor a class does need a constructor
Struct can have nonshared constructors only if they take parameters. Classes can have them with or without parameters.
A struct procedure can handle events only if it is a static method, and only by means of the AddHandler Statement Any class procedure can handle events, using either the Handles keyword or the AddHandler statement.

Every struct has an implicit public constructor without parameters. This constructor initializes all the struct’s data elements to their default values. You cannot redefine this behaviour
Instances and Variables: Because structs are value types, each struct variable is permanently bound to an individual struct instance. But classes are reference types, and an object variable can refer to various class instances at different times.

This distinction affects your usage of struct and classes in the following ways:

  • Initialization

A struct variable implicitly includes an initialization of the elements using the struct’s parameterless constructor. Therefore,

struct s ; is equivalent to struct s1 = new struct() ;

  • Assigning Variables.

When you assign one struct variable to another, or pass a structure instance to a procedure argument, the current values of all the variable elements are copied to the new structure. When you assign one object variable to another, or pass an object variable to a procedure, only the reference pointer is copied.

  • Assigning Null.

You can assign the value “null” to a struct variable, but the instance continues to be associated with the variable. You can still call its methods and access its data elements, although variable elements are reinitialized by the assignment.

In contrast, if you set an object variable to “null “, you dissociate it from any class instance, and you cannot access any members through the variable until you assign another instance to it.

  • Multiple Instances.

An object variable can have different class instances assigned to it at different times, and several object variables can refer to the same class instance at the same time. Changes you make to the values of class members affect those members when accessed through another variable pointing to the same instance.

Struct elements, however, are isolated within their own instance. Changes to their values are not reflected in any other structure variables, even in other instances of the same Struct declaration.

  • Equality

Equality testing of two struct must be performed with an element-by-element test.

Two object variables can be compared using the Equals method. Equals indicates whether the two variables point to the same instance.

 

C# development, Interview questions, IT/Software development Tags:C#, C# development, Interview questions, struct, Structure

Post navigation

Previous Post: C# interview Questions – What is struct in C#?
Next Post: SOLID Design Principles

Related Posts

  • Top 20 GIT Interview Questions GIT
  • Interview question: Which class acts as a base class for all arrays in C#? C# development
  • Asp.Net WebApi Interview Questions – Cont. ASP.NET Web API
  • Interview question: What are nullable types in C#? C# development
  • Interview question: What is the difference between GetType() and typeof()? C# development
  • Interview question: How to sort an array in C#? C# development

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.



Categories

  • Agile Software development
  • Amazon AWS Certification Exam
  • Amazon EC2
  • Amazon ECS
  • Amazon Web Services (AWS)
  • Apache Kafka
  • API development
  • API development
  • Apple Mac
  • ASP.NET Core
  • ASP.Net MVC
  • ASP.NET Web API
  • Atlassian Jira
  • AWS DevOps Engineer Professional Exam
  • AWS Lambda
  • AZ-300: Microsoft Azure Architect Technologies Exam
  • Azure
  • Azure Active Directory
  • Azure AI and ML services
  • Azure Analytics Services
  • Azure App Service
  • Azure Application Gateway
  • Azure Archive Storage
  • Azure Blob Storage
  • Azure Cognitive Services
  • Azure Compute
  • Azure Container Instances (ACI)
  • Azure Core Services
  • Azure Cosmos DB
  • Azure Data and Storage
  • Azure Data Factory
  • Azure Data Lake Storage
  • Azure Database Services
  • Azure Databricks
  • Azure DDoS Protection
  • Azure Defender
  • Azure Devops
  • Azure Disk Storage
  • Azure File Storage
  • Azure Functions
  • Azure IaaS
  • Azure Identity and Access Management
  • Azure Internet of Things (IoT)
  • Azure Key Vault
  • Azure Kubernetes Service (AKS)
  • Azure landing zone
  • Azure Load Balancer
  • Azure Logic Apps
  • Azure Machine Learning
  • Azure Machine Learning
  • Azure Migration
  • Azure Mobile Apps
  • Azure Networking – VNET
  • Azure Networking services
  • Azure Pricing and Support
  • Azure Queue Storage
  • Azure Resource Manager
  • Azure Security
  • Azure Security Center
  • Azure Security Information and Event Management (SIEM)
  • Azure security tools for logging and monitoring
  • Azure Security, Privacy, Compliance, and Trust
  • Azure Sentinel
  • Azure Sentinel – Data connectors
  • Azure Serverless Computing
  • Azure Service Level Agreement (SLA)
  • Azure SLA calculation
  • Azure SQL
  • Azure SQL Database
  • Azure Storage
  • Azure Stream Analytics
  • Azure Synapse Analytics
  • Azure Table Storage
  • Azure Virtual Machine
  • Azure VNET
  • Business
  • C# development
  • C# interview questions with answers
  • CDA (Clinical Document Architecture)
  • ChatGPT
  • CI/CD pipeline
  • CISSP certification
  • CKEditor
  • Cloud
  • Cloud computing
  • Cloud Computing Concepts
  • Cloud services
  • COBIT
  • Command Query Responsibility Segregation (CQRS) Pattern
  • Configure SSL offloading
  • Content management system
  • Continuous Integration
  • conversational AI
  • Cross Site Scripting (XSS)
  • cyber breaches
  • Cybersecurity
  • Data Analysis
  • Database
  • DevOps
  • DevSecOps
  • Docker
  • DOM-based XSS
  • Domain-Driven Design (DDD)
  • Dynamic Application Security Testing (DAST)
  • Enterprise application architecture
  • Event-Driven Architecture
  • git
  • GIT
  • gmail api
  • Google
  • Google Ads
  • Google AdSense
  • Google Analytics
  • Google analytics interview questions with answers
  • Google Cloud Platform (GCP)
  • Google Docs
  • Google Drive
  • Google Maps
  • Google search console
  • Healthcare Interoperability Resources
  • Hexagonal Architecture Pattern
  • HL7 vs FHIR
  • HTML
  • Information security
  • Infrastructure as a Service (IaaS)
  • Internet of Things (IoT)
  • Interview questions
  • Introduction to DICOM
  • Introduction to FHIR
  • Introduction to HL7
  • IT governance
  • IT Infrastructure networking
  • IT/Software development
  • Javascript interview questions with answers
  • Kubernetes
  • Layered Pattern
  • Leadership Quote
  • Life lessons
  • Load Balancing Algorithms
  • Low-code development platform
  • Microservices
  • Microservices
  • Microsoft
  • Microsoft 365 Defender
  • Microsoft AI-900 Certification Exam
  • Microsoft AZ-104 Certification Exam
  • Microsoft AZ-204 Certification Exam
  • Microsoft AZ-900 Certification Exam
  • Microsoft Azure
  • Microsoft Azure certifications
  • Microsoft Azure Log Analytics
  • Microsoft Cloud Adoption Framework
  • Microsoft Exam AZ-220
  • Microsoft Exam AZ-400
  • Microsoft Excel
  • Microsoft Office
  • Microsoft Teams
  • Microsoft Teams
  • Microsoft word
  • Model-View-Controller (MVC) Pattern
  • Monitoring and analytics
  • NoSQL
  • OpenAI
  • OutSystems
  • Peer-to-Peer (P2P) pattern
  • Pipeline Pattern
  • PL-100: Microsoft Power Platform App Maker
  • PL-200: Microsoft Power Platform Functional Consultant Certification
  • PL-900: Microsoft Power Platform Fundamentals
  • Platform as a Service (PaaS)
  • postman
  • Postman
  • Postman
  • Project management
  • Python interview questions with answers
  • Ransomware
  • Reflected XSS
  • RESTful APIs
  • Rich Text Editor
  • SC-100: Microsoft Cybersecurity Architect
  • Scrum Master Certification
  • Service-oriented architecture (SOA)
  • Software architecture
  • Software as a Service (SaaS)
  • SonarQube
  • Splunk
  • SQL
  • SQL Azure Table
  • SQL Server
  • Static Application Security Testing (SAST)
  • Stored XSS attacks
  • Table Storage
  • Test Driven Development (TDD)
  • TinyMCE
  • Top technology trends for 2023
  • Uncategorized
  • User Experience (UX) design
  • Version control system
  • visual studio
  • WCF (Windows Communication Foundation)
  • Web development
  • WordPress
  • WordPress developer interview questions and answers
  • Zero Trust strategy



Recent Posts

  • Get started with Azure Cosmos DB
  • Azure Cosmos DB
  • How can I run web and api projects together in visual studio
  • Azure DDoS Protection
  • Azure Security Information and Event Management (SIEM)
  • Infrastructure as Code (IaC) DevSecOps
  • Azure DDoS Protection Azure DDoS Protection
  • Azure Data Factory Azure Data Factory
  • Dynamic Application Security Testing (DAST) Dynamic Application Security Testing (DAST)
  • Get started with Azure Cosmos DB Azure Cosmos DB
  • Infrastructure as a Service (IaaS) Infrastructure as a Service (IaaS)
  • Interview question: What is the difference between dynamic type variables and object type variables? C# development
  • Azure Internet of Things (IoT) Azure Internet of Things (IoT)

Copyright © 2023 Desi banjara.

Powered by PressBook News WordPress theme