Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Type system


Published on

This slid is about type systems in programming language

Published in: Software, Technology
  • Be the first to comment

  • Be the first to like this

Type system

  1. 1. Type System Anoop K. C. aabte bte/ +91 9746854752
  2. 2. C# is a strongly-typed language. Every variable and constant has a type, as does every expression that evaluates to a value. The information stored in a type can include the following: The storage space that a variable of the type requires The maximum and minimum values that it can represent. The members (methods, fields, events, and so on) that it contains. The base type it inherits from. The location where the memory for variables will be allocated at run time. The kinds of operations that are permitted.
  3. 3. Example The compiler uses type information to make sure that all operations that are performed in your code are type safe. For example, if you declare a variable of type int, the compiler allows you to use the variable in addition and subtraction operations. If you try to perform those same operations on a variable of type bool, the compiler generates an error, as shown in the following example: When we declare a variable or constant in a program, we must either specify its type or use the var keyword to let the compiler infer the type.
  4. 4. After a variable is declared, it cannot be re-declared with a new type, and it cannot be assigned a value that is not compatible with its declared type. For example, you cannot declare an int and then assign it a Boolean value of true. However, values can be converted to other types, when they are assigned to new variables or passed as method arguments. A type conversion that does not cause data loss is performed automatically by the compiler. A conversion that might cause data loss requires a cast in the source code.
  5. 5. Built-in Types C# provides a standard set of built-in numeric types to represent integers, floating point values, Boolean expressions, text characters, decimal values, and other types of data. integers floating point values Boolean expressions text characters decimal values & other types of data. There are also built-in string and object types. These are available to use in any C# program
  6. 6. Custom Types We use the struct, class, interface, and enum constructs to create our own custom types. By default, the most frequently used types in the class library are available in any C# program. Others become available only when we explicitly add a project reference to the assembly in which they are defined. After the compiler has a reference to the assembly, we can declare variables (and constants) of the types declared in that assembly in source code.
  7. 7. The common type system It is important to understand two fundamental points about the type system in the .NET Framework: It supports the principle of inheritance, that means, types can derive from other types, called base types. All types derive ultimately from a single base type, which is System.Object (C# keyword: object). This unified type hierarchy is called the Common Type System (CTS). Each type in the CTS is defined as either a value type or a reference type. •Types that you define by using the struct keyword are value types •Types that you define by using the class keyword are reference types Reference types and value types have different compile-time rules, and different run-time behavior.
  8. 8. Value types Value type variables directly contain their values the memory is allocated inline in whatever context the variable is declared There is no separate heap allocation or garbage collection overhead for value- type variables. There are two categories of value types: struct and enum. Value types are sealed, that means they does not allow inheritance
  9. 9. Reference types A type that is defined as a class, delegate, array, or interface is a reference type. At run time, when we declare a variable of a reference type, the variable contains the value null until we explicitly create an instance of the object by using the new operator, or assign it an object that has been created elsewhere by using new When a reference type variable is created, the memory is allocated on the managed heap, and the variable holds only a reference to the location of the object. All arrays are reference types, even if their elements are value types. Reference types fully support inheritance.
  10. 10. Generic types A type which is declared with one or more type parameters that serve as a placeholder for the actual type (the concrete type) that client code will provide when it creates an instance of the type. For example, the .NET Framework type System.Collections.Generic.List<T> has one type parameter that by convention is given the name T. When you create an instance of the type, you specify the type of the objects that the list will contain, for example, string: List<string> strings = new List<string>(); The use of the type parameter makes it possible to reuse the same class to hold any type of element Generic collection types are strongly typed
  11. 11. Implicit Types, Anonymous Types, and Nullable Types We can implicitly type a local variable (but not class members) by using the var keyword. The variable still receives a type at compile time, but the type is provided by the compiler. Anonymous types are types those have scope only inside the method in which they are defined. The compiler itself will create an arbitrary class for such types, giving us the advantage of not having to define a class for a type that is only used inside a single method. Anonymous types are immutable. 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. The type of each property is inferred by the compiler. Ordinary value types cannot have a value of null. we can create nullable value types by affixing a ? after the type. For example, int? is an int type that can also have the value null.
  12. 12. Follow us @ Like us @ Subscribe to us @ Become a follower @ Connect to us @ Thanks in advance. | |
  13. 13. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: