Presentation is loading. Please wait.

Presentation is loading. Please wait.

CNS 3260 C# .NET Software Development

Similar presentations


Presentation on theme: "CNS 3260 C# .NET Software Development"— Presentation transcript:

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

2 Types Simple types Complex 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 Memory ref0 ref1 Memory int x int y 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 Type System Type
Description Example sbyte System.SByte 8-bit signed integral type sbyte val = 12; short System.Int16 16-bit signed integral type short val = 12; int System.Int32 32-bit signed integral type int val = 12; long System.Int64 64-bit signed integral type long val1 = 12; long val2 = 34L; byte System.Byte 8-bit unsigned integral type byte val1 = 12; ushort System.UInt16 16-bit unsigned integral type ushort val1 = 12; uint System.UInt32 32-bit unsigned integral type uint val1 = 12; uint val2 = 34U; ulong System.UInt64 64-bit unsigned integral type ulong val1 = 12; ulong val2 = 34U; ulong val3 = 56L; ulong val4 = 78UL; char System.Char 16 bit unicode character char c = ‘a’; char c = 255; Signed Unsigned Unicode

8 Simple Types (Continued)
floating-point-types Type System Type Description Example float System.Single Single-precision floating point type float val = 1.23F; double System.Double Double-precision floating point type double val1 = 1.23; double val2 = 4.56D; Decimal Type System Type Description Example decimal System.Decimal Precise decimal type with 28 significant digits decimal val = 1.23M; Boolean Type System Type Description Example bool System.Boolean Boolean type; a bool value is either true or false bool val1 = true; bool val2 = false;

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 Interfaces Arrays Delegates
object (System.Object) string user-defined classes Interfaces Arrays Delegates delegate (System.Delegate)

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

17 String Type String Type (Immutable reference type) Type System Type
Description Example string System.String Immutable string of 16-bit Unicode characters. string s1 = “Hello”; string s2 = string.Empty;

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: Two static methods: One protected method:
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: Q: What’s the difference between Equals and ReferenceEquals?
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? float to Int32? Explicit
Int32 to float? Implicit decimal to float? Explicit long to decimal? Implicit Int32 to UInt32? Explicit UInt32 to Int32? Explicit (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, ); } 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)


Download ppt "CNS 3260 C# .NET Software Development"

Similar presentations


Ads by Google