我在代碼中得到Compiler Error CS0116:在Default.aspx.cs爲什麼編譯器錯誤CS0116高於一切?
<%@ Page EnableEventValidation="false" ValidateRequest="false" Language="C#" AutoEventWireup="true"
CodeFile="../Default.aspx.cs" Inherits="x.Program" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%@ Register Assembly="Validators" Namespace="Sample.Web.UI.Compatibility" TagPrefix="cc1" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Title</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body class="mainbody"></body>
</html>
腳本代碼(請注意,「在某種程度上忽略了這個隨機文本」將被忽略,並且不生成語法錯誤):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//this random text somehow ignored (now commented)
namespace x
{
// To fix the error, you must
// enclose a method in a class:
class Program // changed from Class Program
{
void Method2(string str)
{
Console.WriteLine(str)
}
}
}
爲什麼會出現編譯器錯誤CS0116?爲什麼其他的東西被忽略(如語法錯誤)?更新:請注意,如果我刪除「此隨機文本以某種方式被忽略」,線路錯誤仍然存在。
最終更新: 解決方案是 1.顯然,應刪除多餘的行。 2.工具 - >選項 - >文本編輯器 - >基本 - > VB特定 - >漂亮列表(重新格式化)的代碼已打開並導致案件(「班級」自動更改爲「班級」)。我沒有馬上注意到它。 非常感謝。它現在作爲魅力。 @Rob Levine和@Guffa我只能接受一個答案,但你們都做得很好!
你的問題在幾個地方包含它自己的答案(錯誤消息,評論和間接你的'不知何故被忽略'的評論)。這是一個很糟糕的問題,並且首先省略了編譯器錯誤信息 – sehe 2011-04-26 11:47:51
請注意如果我刪除「這個隨機文本以某種方式忽略」,行錯誤仍然存在。不行,我會重複一遍:那條線並不重要。如果你願意,可以忽略它。該行被有意添加以強調語法錯誤被忽略的事實。總而言之,我的問題並不包含它自己的答案。所以這是一個合理的問題。 – 2011-04-26 12:05:35
根據您對現有答案的意見,您上面張貼的代碼不再是最新的。確保你應用了這兩個建議,然後用更新的代碼編輯你的問題,假設錯誤仍然存在。 – 2011-04-26 12:10:14