Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:

Similar presentations


Presentation on theme: "C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:"— Presentation transcript:

1 C# D1 CSC 298 Elements of C# code (part 2)

2 C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods: to describe the behavior of the class  Visibility: public, private (default is private in C#)  Field initialization: fields are automatically initialized to a default value (0 for numbers, false for bools, '\0' for chars, and null for reference types).  use new to instantiate the class or struct  Specific to C#  properties (see later in this chapter)  enums (see later in this chapter)  delegates (events): see in a later lecture

3 C# D3 Syntax public class MyClass { public int x; //field initialized to 0 automatically string s; // s is private and null // Default constructor: given automatically if there are no // other constructors. If there are other constructors in the // class, it must be explicitly written. public MyClass(){ /*code */ } // Method public int foo(int n){ /*code */ } }

4 C# D4 Overloading  In a class (or struct), several methods may have the same name  Distinguish between the methods with the list of parameters (the access modifier or the return type won't do it) public void Frodo(int n){} private void Frodo(double x){} public int Frodo(int n){} //Error!  The compiler will tell you if there is any ambiguity  Commonly used with constructors public MyClass(){} public MyClass(string s){} public MyClass(object n){}

5 C# D5 Properties (1)  Consider the PersonInfo class that has a field age public class PersonInfo{ int age;...}  To give access to the variable age to a user  Make age public: but the user can set age to an invalid value (e.g. -10)  Define getters and setters, e.g. public int GetAge(){return age;} public void SetAge(int a){ if (a >=0 ) age = a;} Cumbersome: to add 1 to age of PersonInfo pi pi.SetAge(pi.GetAge()+1);  Better: use properties

6 C# D6 Properties (2)  A property defines get and set methods, e.g. public int Age // Age property { get{return age;} set{if (value >=0 ) age = value;} }  The user doesn't call get and set. Instead, the user manipulates the property like a field. Behind the scenes, the compiler generates a call to set or get To add 1 to age of PersonInfo pi: pi.Age += 1; Can't set age to -10: pi.Age = -10; // not done!  A property looks like a public field to the user (same convenient syntax). But the class retains control over the setting of the value of the property.

7 C# D7 const vs readonly  const (review): to define a variable whose value is defined on the line of declaration. Well suited for variables whose values are known at compilation time. const double PI = 3.1415;  But if we don't know the value at compilation time? Use a readonly variable. A value can be assigned to a readonly variable on the line of declaration or in a constructor. readonly double rate; public Mortgage(){ rate = getRateFromDataBase();}

8 C# D8 static keyword revisited  static can be applied to a field, property or method.  static means that the field, property or method belongs to the class, and is not a feature of an instance of the class. Use the class name to access it, e.g. Math.PI.  Note: a static method can only access the static features of a class.  A class can define a static constructor. In a static constructor, initialize the class static variables (e.g. a static readonly variable) static MyClass(){ /* code */ } // can't use public or private // (always private by default)

9 C# D9 Enumerations (enum)  A type to define a list of constants. public enum SummerMonths{ June=6, July, August, September} // Under the hood, June is 6, // July 7, etc... // May omit =6. Then June is 0, July 1,... // In some method SummerMonths m = SummerMonths.July; Console.WriteLine(m); // July is printed // Strongly typed m = 7; // error m = (SummerMonths)7; // OK

10 C# D10 operator overloading  C# offers the option to overload operators (not as versatile as C++, not allowed by Java).  All operators must be declared static  e.g. for a Rational class (describing rational numbers) public static bool operator<=(Rational r1, Rational r2){ /* is r1 <= r2? */ } e.g.  Often, they must be defined in pairs (, == with !=, etc…)  See example Rational.cs on the class web site.

11 C# D11 Namespace  To avoid class name collisions (likely in a large project), e.g.  if you define a String class, it might be confused with the System.String class  Put your String class in a namespace namespace SeattleCentral{ public class String{ /*code*/ } }  To use the class along with System.String, use the fully qualified class name SeattleCentral.String s1; System.String s2;  Our small projects won't require using namespaces (our classes will reside in the default namespace).

12 C# D12 Assembly  A key element of.NET  An assembly contains compiled code (IL code) and data describing the code (Metadata). It can also contain resources (icon files…)  In general, several classes are compiled within an assembly (e.g. System.dll)  View the content of an assembly with the.NET tool ildasm (intermediate language disassembler)  In.NET, all classes within one project are compiled into one assembly (you can also do it "by hand" using the command line compiler cs). .NET defines a keyword internal for assembly visibility. It is the default visibility for a top level class (i.e. which is not nested within another class).


Download ppt "C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:"

Similar presentations


Ads by Google