Struct vs Class in .Net Framework
2006-04-03 13:11
387 查看
Struct vs Class in .Net Framework
Posted by: Rickie Lee http://rickie.cnblogs.com
A struct is a user-defined type that is very similar to a class; it can contain constructors, fields, methods, and properties. Structs are declared using the struct keyword instead of class.
There are some differences between struct and class in .Net Framework.
1. A struct is a value type, whereas classes are reference types. Internally, structs are implicitly derived from System.ValueType.
The following example shows a simple struct declaration:
public struct Book
{
public decimal price;
public string title;
public string author;
}
2. Struct does not support inheritance. A struct can not derive from a class or from another struct. But a class can support inheritance.
3. Structs always contain by default a parameterless, default constructor, which does nothing. You’re allowed to add more overloads, but you can’t add a parameterless constructor. That is to say, structs can declare constructors, but they must take parameters.
4. Although structs are very powerful, they are mainly designed to act as containers for data rather than as fully featured objects. Because they are value types and are stored on the stack, passing them around can be very fast. While classes are reference types and are stored on the Heap.
5. Unlike classes, structs can be instantiated without using a new operator.
References:
1. Beginning ASP.NET 2.0 e-commerce in C# 2005 - Apress
2. MSDN
Posted by: Rickie Lee http://rickie.cnblogs.com
A struct is a user-defined type that is very similar to a class; it can contain constructors, fields, methods, and properties. Structs are declared using the struct keyword instead of class.
There are some differences between struct and class in .Net Framework.
1. A struct is a value type, whereas classes are reference types. Internally, structs are implicitly derived from System.ValueType.
The following example shows a simple struct declaration:
public struct Book
{
public decimal price;
public string title;
public string author;
}
2. Struct does not support inheritance. A struct can not derive from a class or from another struct. But a class can support inheritance.
3. Structs always contain by default a parameterless, default constructor, which does nothing. You’re allowed to add more overloads, but you can’t add a parameterless constructor. That is to say, structs can declare constructors, but they must take parameters.
4. Although structs are very powerful, they are mainly designed to act as containers for data rather than as fully featured objects. Because they are value types and are stored on the stack, passing them around can be very fast. While classes are reference types and are stored on the Heap.
5. Unlike classes, structs can be instantiated without using a new operator.
References:
1. Beginning ASP.NET 2.0 e-commerce in C# 2005 - Apress
2. MSDN
相关文章推荐
- Comparing the Timer Classes in the .NET Framework Class Library
- The longest class name in Java and .NET Framework
- In .net Class and Struct
- When should you use a class vs a struct in C++?
- Comparing the Timer Classes in the .NET Framework Class Library
- VS 没有 ADO.NET Entity Framework模板
- Thinking in C++: C++中关于union、struct、class的区别
- Solaris vs. Linux: Ecosystem-based Approach and Framework for the Comparison in Large Enterprise
- Net--必须知道Class和struct的关系[转]
- 编写VS.NET的Add-in帮助程序调试
- [你必须知道的.NET] 第四回:后来居上:class和struct
- Programming the Thread Pool in the .NET Framework
- NAnt intellisense in VS.NET 2003 [转]
- New C# Features in the .NET Framework 4
- Struct vs Class 作为HashTable或者Dictionary的Key
- 【MSDN文摘】Persisting Application Settings in the .NET Framework
- Add-in Express for Office and .NET v7.3支持VS 2013预览
- VS 插件 All-In-One Code Framework Sample Browser
- MSMQ&Com+ Service: How to create an Com+ Service in .NetFramework
- .NET Compact Framework 3.5 in Platform Builder 6.0 (and 5.0)