html5-img
1 / 40

C# Datatypes

C# Datatypes. CNS 3260 C# .NET Software Development. Types. Simple types int, uint, float, etc. keywords represent structs in IL Complex types Classes Structs Enums Delegates Everything inherits from System.Object. C# Value vs. Reference.

bruis
Download Presentation

C# Datatypes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# Datatypes CNS 3260 C# .NET Software Development

  2. Types • Simple types • int, uint, float, etc. • keywords represent structs in IL • Complex types • Classes • Structs • Enums • Delegates • Everything inherits from System.Object

  3. C# Value vs. Reference • Value Types “directly contain” the variable data. • Example: int, float, enum, struct... • Reference Types contain a reference to the data Reference Type Value Type myObject

  4. class ValueType • ValueType is a class that inherits directly from object • You cannot inherit from ValueType • ValueType overrides System.Object methods so they make sense

  5. value-types • value-types inherit directly from ValueType • simple types • enum types • struct types • All value-types are sealed • value-types cannot be null • Assignment of a value-type results in a copy of the variable

  6. value-type Hierarchy value-type struct-type enum-type simple-type type-name type-name numeric-type bool integral-type floating-point-type decimal

  7. Simple Types integral-types Signed Unsigned Unicode

  8. Simple Types (Continued) floating-point-types Decimal Boolean

  9. numeric-type Methods • static MinValue • static MaxValue • static Parse • static Parse(string) • static Parse(string, NumberStyles) • ToString • ToString() • ToString(string format) int index = 256; index.ToString(“X”);

  10. System.Char (remember it’s 16-bit) • Has ingetral-type methods, plus... • static IsDigit() • static IsLetter() • static IsDigitOrLetter() • static IsLower() • static IsNumber() • static IsPunctuation() • static IsUpper() • static ToUpper() • static ToLower() • and more...

  11. floating-point-types • Have numeric-type methods, plus... • static Epsilon • static NaN • static NegativeInfinity • static PositiveInfinity • static IsInfinity() • static IsNaN() • static TryParse() • and more...

  12. System.Decimal (decimal) • Have numeric-type methods, plus... • static Floor • static GetBits() • static ToDouble() • static ToSingle() • static ToInt64() • static Round() • static Truncate() • and more...

  13. System.Boolean (bool) • Have numeric-type methods, plus... • static FalseString • static TrueString • and (not much) more...

  14. structs and enums • An enumeration type is a distinct type with named constants. • Every enumeration type has an underlying type, which must be byte, sbyte, short, ushort, int, uint, long or ulong. • A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types. • We’ll cover structs and enums later

  15. Reference Types • Classes • object (System.Object) • string • user-defined classes • Interfaces • Arrays • Delegates • delegate (System.Delegate)

  16. reference-type Hierarchy reference-type class-type interface-type array-type delegate-type type-name object string

  17. String Type String Type (Immutable reference type)

  18. System.String (string) • static Empty • StartsWith(string) • EndsWith(string) • IndexOf(string) • static Join(string, string[]) • Split(char[], int) • Remove(int, int) • Replace(string, string) • Insert(int, string) • and much more...

  19. Object • Four virtual methods: • Equals(object obj) • GetHashCode() • GetType() • ToString() • Two static methods: • static Equals(object objA, object objB) • static ReferenceEquals(object objA, objB) • One protected method: • Finalize()

  20. Object Virtual Methods • Equals(object obj) • Reference types: the default supports reference-equals • Value types: the default supports bitwise equality. • Can be overridden • GetHashCode() • Returns a Hash value • GetType() • Returns the exact runtime type of the calling instance • ToString() • Returns a string that represents the calling instance

  21. Object Static Methods • Equals(object objA, object objB) • This method first determines whether both parameters are null references before calling objA.Equals(objB) • ReferenceEquals(object objA, object objB) • Returns true if objA is the same instance as objB or if both are null references; otherwise, false

  22. Try this: class Class1 { [STAThread] static void Main(string[] args) { Console.WriteLine(object.Equals(null, null)); Console.WriteLine(object.ReferenceEquals(null, null)); Console.ReadLine(); } } Q: What’s the difference between Equals and ReferenceEquals? A: Equals calls objA.Equals(objB). So if objA redefined .Equals(), that overridden method will be invoked. ReferenceEquals will always tell if the references are the same.

  23. Everything derives from object • 3.ToString(); • “hello”.GetHashCode(); • 5.Equals(5); • myObject.Equals(yourObject);

  24. Boxing • Value Types which are up-cast to a reference type are boxed • This is necessary because Collections hold objects • Boxing places a copy of the variable in the object • Extra information (such as type info) may be stored with the variable, the standard is not explicit

  25. Boxing example class Test { static void Main() { int i = 123; object o = i; // boxing int j = (int) o; // unboxing } }

  26. Type Conversions Implicit Conversions Implicit conversions will occur when there is no loss of information. Example: using System; class Test { static void Main() { int intValue = 123; long longValue = intValue; Console.WriteLine("{0}, {1}", intValue, longValue); } }

  27. Conversions Explicit Conversions Explicit conversions are required when there could be loss of information. Example: using System; class Test { static void Main() { long longValue = System.Int64.MaxValue; int intValue = (int)longValue; Console.WriteLine(“(int){0} = {1}", longValue, intValue); } }

  28. Conversions Explicit or Implicit? Explicit float to Int32? Implicit Int32 to float? Explicit decimal to float? Implicit long to decimal? Explicit Int32 to UInt32? Explicit UInt32 to Int32? (See Conversions Demo)

  29. Local variables • must be assigned before they are used • no static locals • no const locals Example: void fun(){ int a, b = 3; int c = a + b; }

  30. Fields • static • instance public class Rectangle{ public int Top = 0; public int Left = 0; public int Height = 100; public int Width = 100; private static int RectangleCount; public Polygon() { ++ RectangleCount; } public ~Polygon() { -- RectangleCount; } }

  31. Local vs. Field class SomeClass{ private int index; public SomeClass(int index) { index = index; // WRONG!!! this.index = index; } }

  32. Pointers? • only in unsafe blocks (we’ll explore these later.)

  33. Passing Parameters: Value class MainClass { static void Main() { int i = 0; new MainClass().fun(i); Console.WriteLine(i.ToString()); Console.ReadLine(); } private void fun(int x) { x = 5; } } What does it mean to pass a reference by value? (See PassReference Demo)

  34. Passing references • Passing object to a method only passes reference • Fast • Easy • Somewhat misunderstood • No implicit deep copy if needed

  35. Passing Parameters: ref class MainClass { static void Main() { int i = 0; new MainClass().fun(ref i); Console.WriteLine(i.ToString()); Console.ReadLine(); } private void fun(ref int x) { int y = x; // ok x = 5; int z = x; // ok } } If a reference type is being passed, then it is a reference to the reference.

  36. Passing Parameters: out Similar to passing by ref, but the value is initially unassigned. Also, you must assign to the parameter somewhere in the method. class MainClass { static void Main() { int i = 0; new MainClass().fun(out i); Console.WriteLine(i.ToString()); Console.ReadLine(); } private void fun(out int x) { // int y = x; // not ok x = 5; int z = x; // ok } }

  37. Variable Length Arguments Uses params keyword. class Test { static void F(params int[] args) { Console.WriteLine("# of arguments: {0}", args.Length); for (int i = 0; i < args.Length; i++) Console.WriteLine("\targs[{0}] = {1}", i, args[i]); } static void Main() { F(); F(1); F(1, 2); F(1, 2, 3); F(new int[] {1, 2, 3, 4}); } }

  38. Intro to delegates • Think of a delegate as a function pointer that stores a reference to its instance. C++ function pointer: int* (*funPtr)(char arg1, float arg2); C# delegate: public delegate void funDelegate(char arg1, float arg2);

  39. Delegate Syntax • Delegate declaration declares a new type. public delegate string FunDelegate(int a, float b); public class MainClass { [STAThread] static void Main() { FunDelegate fd = new FunDelegate(fun); fd(5, 3.14159); } private string fun(int x, float f) { return (x+f).ToString(); } }

  40. Delegates • Why use delegates (or function pointers?) Keeps coupling low Makes code more extensible Asynchronous processing (See SimpleDelegateDemo)

More Related