What is the difference between struct and class c#?



What is the difference between struct and class c#?..

Answer / Avneesh Sharma

In C#, both Struct (struct) and Class are user-defined data types. However, there are some differences:
1. By default, classes are reference types, while structs are value types.
2. Classes support inheritance with ':' operator, but structs do not. The ':' operator in structs is used for indexer declarations.
3. Classes can have a constructor, whereas structs are instantiated using the default constructor unless explicitly defined.
4. Struct fields are public by default and cannot be private or protected, while class fields can be any access modifier (public, private, protected).

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Sharp Interview Questions

Is it possible to have a static indexer in c#? Allowed in c#.

1 Answers  


When a Static Constructor is called in a Class?

1 Answers  


What is thread safe in c#?

1 Answers  


What is sql transaction in c#?

1 Answers  


To catch any possible exception What is the C# syntax written ?

1 Answers   Siebel,


Are private class-level variables inherited?

2 Answers  


What is the main method?

1 Answers  


What is the difference between directcast and ctype?

2 Answers   BirlaSoft, Wipro,


what are the bugs are faced in a project

3 Answers   Accenture,


How many types of constructor are there in c#?

1 Answers  


Why do we use stringbuilder in c#?

1 Answers  


What are the types of serialization?

1 Answers  


Categories