C#System.String类(和其他)中的空方法

时间:2015-10-06 16:17:58

标签: c#

我已经学习C#大约一年了。感谢Visual Studio的“Go To Definition”选项,我最近注意到System.String类填充了空方法;他们没有“做某事”的方法体。

经过一番搜索后,我找不到一个好的答案。我最初的想法是其他类继承System.String并覆盖这些方法,但类是sealed。根据MSDN:

  

sealed修饰符可以防止其他类继承它。

那么,System.String等类中的方法如何,例如

  • public bool StartsWith(string value);

  • public int LastIndexOf(char value, int startIndex, int count);

以及这些类中的许多其他方法/重载,知道该怎么做?我猜测编译器有一些关于此的信息,但这只是猜测。我是否正确地称这些方法?他们似乎都在某事到一个字符串,但是怎么样?这个逻辑来自哪里?

2 个答案:

答案 0 :(得分:2)

您看到的不是// // TEACHTableViewController.swift // TEACH // // Created by ICST340.N1 on 9/29/15. // Copyright © 2015 IyoTugs. All rights reserved. // import UIKit import CoreData class TEACHTableViewController: UITableViewController { let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext var fetchedLastName = [String]() var fetchedFirstName = [String]() var fetchedImage = [UIImage]() override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadData:",name:"load", object: nil) let entityDescription = NSEntityDescription.entityForName("Faculty", inManagedObjectContext: managedObjectContext) let request = NSFetchRequest() request.entity = entityDescription do{ let objects = try managedObjectContext.executeFetchRequest(request) let results = objects if results.count > 0 { for var i = 0; i < results.count; i += 1{ let match = results[i] as! NSManagedObject fetchedLastName.append((match.valueForKey("lastname") as? String)!) fetchedFirstName.append((match.valueForKey("firstname") as? String)!) //added this is convert the image let image = match.valueForKey("image") as! NSData fetchedImage.append(UIImage(data: image)!) } } else { } } catch{} // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncommented this // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem() } func reloadData(notification: NSNotification){ fetchedLastName.removeAll() fetchedFirstName.removeAll() //Added this to remove all images fetchedImage.removeAll() let entityDescription = NSEntityDescription.entityForName("Faculty", inManagedObjectContext: managedObjectContext) let request = NSFetchRequest() request.entity = entityDescription do{ let objects = try managedObjectContext.executeFetchRequest(request) let results = objects if results.count > 0 { for var i = 0; i < results.count; i += 1{ let match = results[i] as! NSManagedObject fetchedLastName.append((match.valueForKey("lastname") as? String)!) fetchedFirstName.append((match.valueForKey("firstname") as? String)!) //added this is convert the image let image = match.valueForKey("image") as! NSData fetchedImage.append(UIImage(data: image)!) } } else { } } catch{} self.tableView.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return fetchedLastName.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("TEACH", forIndexPath: indexPath) as! TEACHTableViewCell let row = indexPath.row print(fetchedFirstName) cell.facultyName.text = fetchedLastName[row] + ", " + fetchedFirstName[row] cell.facultyImage.image = fetchedImage[row] return cell } } 课程的源代码,而是metadata。它与Intellisense显示的信息相同。

如果您想查看String课程的源代码,那么available online

答案 1 :(得分:2)

当您跳转到您不拥有/控制的类的定义时,Visual Studio将向您显示它的元数据。基本上是类同意的合同,类似于它有一个接口。

如果您对它的行为感到好奇,可以下载ILSpy以查看实际的实施情况。使用ILSpy进行反编译的DLL的位置将在VS

中的元数据视图的顶部提供给您

metadata extracted from dll