Quantcast
Channel: 2,000 Things You Should Know About C# » Data Types
Browsing all 56 articles
Browse latest View live

Image may be NSFW.
Clik here to view.

#767 – A struct Is Implicitly Sealed

Every struct in C#, whether it is user-defined or defined in the .NET Framework, is sealed–meaning that you can’t inherit from it.  A struct is sealed because it is a value type and all value types are...

View Article



Image may be NSFW.
Clik here to view.

#775 – Copying an Array of Anonymously-Typed Objects

You can create an implicitly-typed array that contains an array of anonymously-typed objects. var movies = new[] { new { Title = "North By Northwest", Year = 1959, Director = "Alfred Hitchcock" }, new...

View Article

Image may be NSFW.
Clik here to view.

#776 – Declaring and Using Nullable structs

You can make any value type nullable, allowing it to either be null or to contain one of its normal values. Since user-defined structs are value types, you can also make any struct nullable, using...

View Article

Image may be NSFW.
Clik here to view.

#777 – A struct Isn’t Mutable When Used as a Property

A struct is normally mutable, i.e. you can modify the values of its members directly.  Assume that we have a DogCollarInfo struct with Width and Length members.  We can create the struct and then later...

View Article

Image may be NSFW.
Clik here to view.

#778 – A struct Isn’t Mutable When Used in a Collection

A struct is normally mutable, i.e. you can modify the values of its members directly. However, if a struct is used in a collection class, like a List<T>, you can’t modify its members....

View Article


Image may be NSFW.
Clik here to view.

#779 – Methods in struct that Modify Elements Can Be Dangerous

If you have a method in a struct that modifies a data member of the struct, you can run into unexpected results, due to the value type semantics of the struct. Assume that we have a struct that...

View Article

Image may be NSFW.
Clik here to view.

#780 – The Case for Immutable structs

You can run into problems when a struct is mutable.  (E.g. when used as a property, in a collection, or when modifying a struct through a method). You can avoid problems by being careful about how you...

View Article

Image may be NSFW.
Clik here to view.

#781 – A struct Can Implement an Interface

Like a class, a struct can implement an interface.  In the example below, the DogCollar struct implements the IStrapDimensions interface, which contains a couple of properties and a method. public...

View Article


Image may be NSFW.
Clik here to view.

#782 – You Can Create an Instance of a struct Without the new Keyword

Like a class, you can create an instance of a struct using the new keyword.  You can either invoke the default parameterless constructor, which initializes all fields in the struct to their default...

View Article


Image may be NSFW.
Clik here to view.

#789 – Grouping Constants into Their Own Class

Constants are declared as class members.  They are effectively static, since there is not a different value for each instance of the class, but you declare them without using the static keyword. You...

View Article

Image may be NSFW.
Clik here to view.

#833 – Some Examples of Anonymous Object Initializers

You create an anonymously-typed object using the object initializer syntax.  The declaration consists of a series of member declarators, each of which can be: A named value (Name = value) A value...

View Article

Image may be NSFW.
Clik here to view.

#834 – Use a Generic List to Store a Collection of Objects

You can use the List<T> class, defined in System.Collections.Generic, to store a list of objects that all have the same type.  This generic list is one of several collection classes that allow...

View Article

Image may be NSFW.
Clik here to view.

#835 – A Generic List Can Store Value-Typed or Reference-Typed Objects

You can use a generic list, defined by the List<T> class, to store a dynamically sized collection of objects.  A List<T> can store any type of object, including both value-typed and...

View Article


Image may be NSFW.
Clik here to view.

#836 – Initializing a Generic List with an Object Initializer

Instead of using the Add method repeatedly to populate a generic list, you can use an object initializer to initialize the contents of the list when you declare it. The object initializer used to...

View Article

Image may be NSFW.
Clik here to view.

#837 – Object Initializers within Collection Initializers

Recall that you can initialize a collection when it is declared, using a collection initializer. List<Dog> myDogs = new List<Dog> { new Dog("Kirby", 15), new Dog("Ruby", 3) }; Also recall...

View Article


Image may be NSFW.
Clik here to view.

#838 – Object and Collection Initializers as Parameters

You can use object and collection initializers to initialize an object or collection when it is declared.  You can also use an object initializer anywhere that an instance of the corresponding object...

View Article

Image may be NSFW.
Clik here to view.

#839 – Anonymous Type Limitations

An anonymous type is a temporary data type that is inferred based on the data that you include in an object initializer. An anonymous type has a couple of  limitations.  Its only members are public,...

View Article


Image may be NSFW.
Clik here to view.

#840 – Use an Anonymous Type as a Read-Only Subset of an Object

You often use anonymous types to store some subset of data from another object.  In many cases, you’re interested in only a subset of the properties present in the original object.  With an anonymous...

View Article

Image may be NSFW.
Clik here to view.

#982 – An Enum Type Can Store a Maximum of 32 Flags

When you store a boolean value in a bool type, each boolean value uses 1 byte, or 8 bits–the size of a bool instance. You can be more efficient in storing boolean values by using each bit within a...

View Article

Image may be NSFW.
Clik here to view.

#983 – Using a BitArray to Store a Large Collection of Boolean Values

If you need to store a set of boolean values, you can store them in array of type bool.  Each element of the array would require 8 bits, despite storing a single boolean value. You can more efficiently...

View Article
Browsing all 56 articles
Browse latest View live




Latest Images