0% found this document useful (0 votes)
76 views3 pages

What Is Boxing and Unboxing in C#?: Struct

Boxing and unboxing in C# allow for implicit conversions between value types and reference types. Boxing converts a value type to a reference type, while unboxing converts a reference type to a value type. An enum in C# is a user-defined type that allows a set of named constants that underlie numeric values, making code more readable. Enums are value types that derive from System.Enum and make use of integer types by default.

Uploaded by

Asim Khattak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views3 pages

What Is Boxing and Unboxing in C#?: Struct

Boxing and unboxing in C# allow for implicit conversions between value types and reference types. Boxing converts a value type to a reference type, while unboxing converts a reference type to a value type. An enum in C# is a user-defined type that allows a set of named constants that underlie numeric values, making code more readable. Enums are value types that derive from System.Enum and make use of integer types by default.

Uploaded by

Asim Khattak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

. What is Boxing and Unboxing in C#?

 
 

Boxing and Unboxing both are used for type conversions.

The process of converting from a value type to a reference type is called boxing. Boxing is an implicit
conversion. Here is an example of boxing in C#.

1. // Boxing  
2. int anum = 123;  
3. Object obj = anum;  
4. Console.WriteLine(anum);  
5. Console.WriteLine(obj); 

The process of converting from a reference type to a value type is called unboxing. Here is an example of
unboxing in C#.

1. // Unboxing  
2. Object obj2 = 123;  
3. int anum2 = (int)obj;  
4. Console.WriteLine(anum2);  
5. Console.WriteLine(obj);  

5. What is the difference between a struct and a class in C#? 


 

Class and struct are both user-defined data types, but have some major differences:

Struct

 The struct is a value type in C# and it inherits from System.Value Type.


 Struct is usually used for smaller amounts of data.
 Struct can’t be inherited from other types.
 A structure can't be abstract.
 No need to create an object with a new keyword.
 Do not have permission to create any default constructor.

Class

 The class is a reference type in C# and it inherits from the System.Object Type.
 Classes are usually used for large amounts of data.
 Classes can be inherited from other classes.
 A class can be an abstract type.
 We can create a default constructor.

Read the following articles to learn more about structs vs classes, Struct and Class Differences in C#. 
 

6. What is the difference between Interface and Abstract


Class in C#? 
 

Here are some of the common differences between an interface and an abstract class in C#.

 A class can implement any number of interfaces but a subclass can at most use only one abstract
class.
 An abstract class can have non-abstract methods (concrete methods) while in case of interface,
all the methods have to be abstract.
 An abstract class can declare or use any variables while an interface is not allowed to do so.
 In an abstract class, all data members or functions are private by default while in an interface all
are public, we can’t change them manually.
 In an abstract class, we need to use abstract keywords to declare abstract methods, while in an
interface we don’t need to use that.
 An abstract class can’t be used for multiple inheritance while the interface can be used as
multiple inheritance.
 An abstract class use constructor while in an interface we don’t have any type of constructor.

To learn more about the difference between an abstract class and an interface, visit Abstract Class vs
Interface.  
 

7. What is enum in C#? 


 

An enum is a value type with a set of related named constants often referred to as an enumerator list.
The enum keyword is used to declare an enumeration. It is a primitive data type that is user-defined.

An enum type can be an integer (float, int, byte, double, etc.). But if you use it beside int it has to be
cast.

An enum is used to create numeric constants in the .NET framework. All the members of enum are
enum type. There must be a numeric value for each enum type.
 

The default underlying type of the enumeration element is int. By default, the first enumerator has the
value 0, and the value of each successive enumerator is increased by 1. 

1. enum Dow {Sat, Sun, Mon, Tue, Wed, Thu, Fri};    

Some points about enum,

 Enums are enumerated data types in c#.


 Enums are not for the end-user, they are meant for developers.
 Enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not
be implicitly assigned to an enum of another type even though the underlying value of their
members is the same.
 Enumerations (enums) make your code much more readable and understandable.
 Enum values are fixed. Enum can be displayed as a string and processed as an integer.
 The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and
ulong.
 Every enum type automatically derives from System.Enum and thus we can use System.Enum
methods on enums.
 Enums are value types and are created on the stack and not on the heap.

For more details follow the link, Enums in C#.  

You might also like