Skip to main content

C# - DataTypes

                                                                                                     <<PreviousPage     NextPage>>

Data Types


Value Type 


  • Variable contains value directly 
  • Derived implicitly from the System.ValueType
  • Cannot derive a new type from a value type.


example of value type - int, float, enum.

Builtin data types



apart from the above mentioned built in data types we need to discuss two special type of data types - Struct and Enum

Struct A struct type is a value type that is typically used to encapsulate small groups of related variables

In the below example we are using a struct variable Book which has one decimal value price and two string variables title and author. Here we can see how to declare and use a struct variable. 






EnumThe enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. In the below example we can see how to declare and use enum variables.








Reference Type
  • Variables of reference types store references to their data
  • Two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable



Dynamic Type

  • The type is a static type, but an object of type dynamic bypasses static type checking
  • At compile time, an element that is typed as dynamic is assumed to support any operation






Anonymous Type

  • Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first
  • The type name is generated by the compiler and is not available at the source code level







Comments

Popular posts from this blog

C# - Decision Making

  <<PreviousPage     NextPage>> Decision Making If Else Statement If Else statement is used for decision making in c# . In the below example we will see how we can use if else statement in C# to great effect.   Nested If Else Statement We can improve upon the above example by adding nest if else statement. Nested if else statement helps to add extra if else block with in an if or else block. Switch Statement In place where we need multiple if else statements to satisfy a criteria we can add switch statement instead of if else statement.  We can create an application which would answer our question and let us know about the Date and Time. This will be a small AI sort of idea which would recognize your command and perform accordingly.  Code -    Console.WriteLine("................Welcome to Mini AI................");            str...