Académique Documents
Professionnel Documents
Culture Documents
Built-in type Anonymous type (will be discussed later) User-defined type Type defined in the .NET Framework class library
can only be used when you are to declare and initialize the local variable in the same statement. The variable cannot be initialized to null. var cannot be used on fields at class scope. Variables declared by using var cannot be used in the initialization expression. In other words, var i = i++; produces a compile-time error. Multiple implicitly-typed variables cannot be initialized in the same statement. If a type named var is in scope, then you will get a compile-time error if you try to initialize a local variable with the var keyword.
var
1/13
Exercise 1
private static void QueryNames(char firstLetter) { // Create the query. var is required because // the query produces a sequence of anonymous types. var studentQuery = from student in students where student.FirstName[0] == firstLetter select new { student.FirstName, student.LastName }; // Execute the query. foreach (var student in studentQuery) { Console.WriteLine("First = {0}, Last = {1}", student.FirstName, student.LastName); } }
1.1 Can define a nullable implicitly typed local variable? a.True b.False
Exercise 2
// Error! Must assign a value! var myData; // Error! Must assign value at exact time of declaration! var myInt; myInt = 0; // Error! Can't assign null as initial value! var myObj = null;
2/13
2.1 From this program, when compiled, there are some errors. Why?
Exercise 3
var d = new[ ] { 1, "one", 2, "two", false }; Whats erros? Error mixed types
Exercise 4
4.1
class ThisWillNeverCompile { // Error! var cannot be used as field data! private var myInt = 10; // Error! var cannot be used as a return value or parameter type! public var MyMethod(var x, var y) { } }
List all variables in this program
3/13
Question :
1. Can Auto-implemented Properties improve the performace?(no) 2. How can I make read-only or write-only auto-implemented properties? (You can make the accessor as private) 3. Why Auto-Implemented Properties?
Exercise 1
// Read-only property? Error! public int MyReadOnlyProp { get; } // Write only property? Error! public int MyWriteOnlyProp { set; } Whats erros?
Exercise 2
static void Main(string[ ] args) { ... Garage g = new Garage(); // OK, prints default value of zero. Console.WriteLine("Number of Cars: {0}", g.NumberOfCars); // Runtime error! Backing field is currently null! Console.WriteLine(g.MyAuto.PetName); Console.ReadLine();
1.1.Whats erros?
Exercise 3
static void Main(string[ ] args) { ...
4/13
c.PetName = "Frank"; // Getting the value is still OK. Console.WriteLine("Your car is named {0}? That's odd...",c.PetName); Console.ReadLine(); } 3.1.Whats erros? ( // Error! Setting the PetName is only possible // from within the Car type or by a child type! )
5/13
Exercise 1:
// Here is our only using directive. using System; namespace MyNewApp { class JustATest { void SomeMethod() { int i = 0; i.Foo(); } } }
3.1 Compile this program. Explain why this program cannot run?
Exercise 2:
static class MyExtensions { // This method allows any object to display the assembly it is defined in. public static void DisplayDefiningAssembly(this object obj) { Console.WriteLine("{0} lives here:\n\t->{1}\n", obj.GetType().Name, Assembly.GetAssembly(obj.GetType())); } public static int ReverseDigits(this int i) { char[ ] digits = i.ToString().ToCharArray();
6/13
Array.Reverse(digits); // Put back into string. string newDigits = new string(digits); // return the modified string back as an int.return int.Parse(newDigits); } }
2.1 What is written by this program?
ReverseDigits()?
char[ ] digits = i.ToString().ToCharArray() explain?( // Translate int into a string, and then get all the characters.) Input =124; output=?
2.4
Exercise 3:
public static class CarExtensions { public static int SlowDown(this Car c) { return --Speed; } }
7/13
2.1 N
//return --c.Speed;
8/13
restrictions:
Partial methods can only be defined within a partial class. Partial methods must return void. Partial methods can be static or instance level. Partial methods can have arguments (including parameters modified by this, ref, or paramsbut not with the out modifier). Partial methods are always implicitly private.
9/13
Question:
1. Collections can contain many values? Y
Exercise 1:
using System; { class MainClass { static void Main() { double price, money; int n; double leftover; Console.Write("Enter candy price: "); price = Console.ReadLine(); Console.Write("Enter number of candies: "); n = Console.ReadLine(); Console.Write("Enter your money :"); money = Console.ReadLine(); leftover = money - price * n; Console.WriteLine("You have {0} bahts left.", leftover); Console.ReadLine(); } } }
1.1 Compile this program. Explain why this program cannot run?
10/13
Exercise 2:
1. Point finalPoint = new Point { X = 30, Y = 30 }; 2. Point finalPoint = new Point() { X = 30, Y = 30 };
2.1
11/13
Restrictions
must not have formal parameters of the kind params T[] must not be assigned to object must not access ref or out parameters of the enclosing method
Question: 1. Anonymous Methods does not require the declaration of a named method? Y Exercise 1:
static void Main(string[ ] args) { Console.WriteLine("***** Fun with Anonymous types *****\n"); // Make an anonymous type representing a car. var myCar = new {Color = "Bright Pink", Make = "Saab", CurrentSpeed = 55}; // Reflect over what the compiler generated. ReflectOverAnonymousType(myCar); Console.ReadLine(); } 1.1 Is the myCar object is of type <>f__AnonymousType0`3 ?
1.2 .Is the assigned type name access directly to C# code base?
Exercise 2:
Make an anonymous type that is composed of another? var purchaseItem = new { TimeBought = DateTime.Now, ItemBought = new {Color = "Red", Make = "Saab", CurrentSpeed = 55}, Price = 34.000}; ReflectOverAnonymousType(purchaseItem);
Exercise 3:
Console.WriteLine("Same anonymous object!"); // Are they considered equal when using ==? 12/13
if (firstCar == secondCar) Console.WriteLine("Same anonymous object!"); else Console.WriteLine("Not the same anonymous object!"); // Are these objects the same underlying type? if (firstCar.GetType().Name == secondCar.GetType().Name) Console.WriteLine("We are both the same type!"); else Console.WriteLine("We are different types!"); // Show all the details. Console.WriteLine(); ReflectOverAnonymousType(firstCar); ReflectOverAnonymousType(secondCar); }
3.3 Are
13/13