GVKun编程网logo

HTML 代码约定(html预定义的标签)

9

本文将分享HTML代码约定的详细内容,并且还将对html预定义的标签进行详尽解释,此外,我们还将为大家带来关于c#–如何在接口中配置代码约定、c#–私有静态只读字段上的代码约定和失败、C#编码约定、C

本文将分享HTML 代码约定的详细内容,并且还将对html预定义的标签进行详尽解释,此外,我们还将为大家带来关于c# – 如何在接口中配置代码约定、c# – 私有静态只读字段上的代码约定和失败、C# 编码约定、C# 编码约定(C# 编程指南)的相关知识,希望对你有所帮助。

本文目录一览:

HTML 代码约定(html预定义的标签)

HTML 代码约定(html预定义的标签)

HTML 代码约定

web 开发者常常不确定在 HTML 中使用的代码样式和语法。 (推荐学习:html教程)

在 2000 年至 2010 年之间,许多 web 开发者从 HTML 转换为 XHTML。

通过 XHTML,开发者不得不编写有效的“格式良好的”代码。

HTML5 在代码验证时会更宽松一点。

通过 HTML5,您必须创建属于自己的最佳实践、样式指南和代码约定。

智能且有未来保证

对样式的合乎逻辑的使用,可以令其他人更容易理解和使用您的 HTML。

在未来,诸如 XML 阅读器之类的程序,也许需要阅读您的 HTML。

使用格式良好的“近似 XHTML 的”语法,能够更智能。

注释:请始终保持您的样式智能、整洁、纯净、格式良好。

请使用正确的文档类型

请始终在文档的首行声明文档类型:

<!DOCTYPE html>

如果您一贯坚持小写标签,那么可以使用:

<!doctype html>

请使用小写元素名

HTML5 允许在元素名中使用混合大小写字母。

我们推荐使用小写元素名:

混合大小写名称并不好开发者习惯使用小写名(比如在 XHTML 中)小写更起来更纯净小写更易书写

不太好:

<SECTION> 
  <p>This is a paragraph.</p>
</SECTION>

很糟糕:

<Section> 
  <p>This is a paragraph.</p>
</SECTION>

还不错:

<section> 
  <p>This is a paragraph.</p>
</section>

c# – 如何在接口中配置代码约定

c# – 如何在接口中配置代码约定

我无法在课堂上配置代码合同.我已经按照文档和示例,但它不起作用.

我想将Code Contracts条件插入我的界面,这里是我的代码

界面

[ContractClass(typeof(ArticleBLLContract))]
 public interface IArticleBLL
 {
    int getArticleNSheet(string IdBox);
    IEnumerable<IArticle> getArticleValue(string IdBox,string IdLanguage);
}

合同类

[ContractClassFor(typeof(IArticleBLL))]
public sealed class ArticleBLLContract : IArticleBLL
{
    int IArticleBLL.getArticleNSheet(string IdBox)
    {
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(IdBox),"IdBox has no valid value");                        

        return default(int);
    }

    IEnumerable<Base.Article.IArticle> IArticleBLL.getArticleValue(string IdBox,string IdLanguage)
    {
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(IdBox),"IdBox has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(IdLanguage),"IdLanguagehas no valid value");

        Contract.Ensures(Contract.Result<IEnumerable<Base.Article.IArticle>>() != null,"Return value is out of Range");

        return default(IEnumerable<Base.Article.IArticle>);
    }        
}

适用合同的类

public class ArticleBLL : IArticleBLL
{

    public int getArticlNSheet(string IdBox)
    {
        try
        {
            return _Dal...
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public IEnumerable<IArticle> getArticleValue(string IdBox,string IdLanguage)
    {
        IEnumerable<IArticle> article = null;

        try
        {
            article = _Dal...

            return article;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

我试图在这一行插入一个断点

Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(IdBox),"IdBox has no valid value");

但是当我打电话给方法时,它永远不会通过这里
这是我的项目配置

有什么不对?

谢谢!

解决方法

根据迄今为止的评论总重写

我已经创建了一个类库项目,并将接口及其契约类放在那里.我已将其设置为“标准合同要求”,运行时前后检查和构建合同参考程序集(我为调试和发布设置了相同的选项).

然后,我得到了一个控制台应用程序,其中有一个实现该接口的类,并设置了“标准合同要求”,运行时前置和后置检查(再次,在调试和发布之间设置相同).

在Debug或Release模式下运行它,我在尝试调用getArticleNSheet时遇到ArgumentOutOfRangeException.

除了切换到“标准合同要求”之外,上述内容与您当前的设置不符?

而且,事实上,我以前错了.对于“标准合同要求”,我实际上能够在调试时在合同类中遇到断点.我不确定Wizardry能够做到这一点 – 因为它不是字面上运行该类中的代码 – 事实证明你可以在合同类中重写方法:

int IArticleBLL.getArticleNSheet(string IdBox)
    {
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(IdBox),"IdBox has no valid value");

        throw new NotImplementedException();
    }

您可以在Contract.Requires行上放置一个断点,它似乎点击它(在关于文件不匹配的警告之后,可能是由于重写).但假设您已传递非空字符串,则它不会抛出NotImplementedException.

c# – 私有静态只读字段上的代码约定和失败

c# – 私有静态只读字段上的代码约定和失败

我班上有一个私有静态只读字段:

public class MyClass
{
    // ISSUE #1 -- requires unproven: path != null
    private static readonly DirectoryInfo MyDirectory =
        new DirectoryInfo(Settings.Default.MyDirectoryPath);

    protected virtual void SomeMethod()
    {
        if (MyDirectory.Exists)
        {
            // ISSUE #2 -- requires unproven: !string.IsNullOrEmpty(path)
            var catalog = new DirectoryCatalog(MyDirectory.FullName);
        }
    }
}

对于问题#1,我使用空合并运算符来默认一些魔术字符串并修复它,但我真的不喜欢这个解决方案.我希望有更好的解决方案.

对于问题#2,我唯一能想到的是使用Contract.Assumes,因为如果我尝试使用Contract.Requires(MyDirectory.Exists ||!String.IsNullOrEmpty(MyDirectory.FullName));它抱怨可见性(在受保护方法的需求中使用的私有字段).

解决方法

问题#1是Settings.Default.MyDirectoryPath的结果,是Visual Studio生成的代码,没有任何合同.此问题不仅限于空字符串.许多API现在都有合同要求说TimeSpan是非负的,但直接在API中使用设置将生成代码合同警告.

解决此问题的方法是将设置包装在具有契约的方法中.例如.:

String GetMyDirectoryPath() {
  Contract.Ensures(Contract.Result<String>() != null);
  var myDirectoryPath = Settings.Default.MyDirectoryPath;
  Contract.Assume(myDirectoryPath != null);
  return myDirectoryPath;
}

请注意Contract.Assume如何真正执行您的设置验证(代码合同无法验证,因为它由外部配置文件控制).如果它是一个预期非负的TimeSpan,您可以使用Contract.Assume进行验证,从而导致ContractException或其他方法使用您自己的异常.

添加这个额外的层有点单调乏味但是因为设置是在应用程序之外定义的,所以需要在某个时刻对运行时进行验证,就像您必须验证交互式用户输入一样.

问题#2可能是因为DirectoryInfo没有定义任何合同.最简单的方法是使用Contract.Assume.这将声明您认为DirectoryInfo的预期行为,但运行时检查仍将到位以确保您的信念正确(前提是您将检查保留在代码中).

var path = MyDirectory.FullName;
Contract.Assume(!string.IsNullOrEmpty(path));
var catalog = new DirectoryCatalog(path);

C# 编码约定

C# 编码约定

 

C# 语言规范没有定义编码标准。 但是,Microsoft 使用本主题中的这些指南开发示例和文档。

编码约定可实现以下目的:

  • 它们创建一致的代码外观,从而使读者可以关注内容而非布局。

  • 它们使读者能够根据以前的经验作出假设,从而更加快速地理解代码。

  • 有利于复制、更改和维护代码。

  • 演示 C# 最佳做法。

命名约定
  • 命名指南在 名称准则 中介绍。

  • 您无需更改由 Visual Studio 设计器工具创建的对象的名称,就可以让它们符合指南。

  • 在不包括 using 语句 的简短示例中,使用命名空间限定。 如果您知道默认情况下会将某命名空间导入项目中,则无需完全限定来自该命名空间的名称。 如果限定名称太长无法放入一行,则可在点 (.) 后截断它,如以下示例所示。

    var currentPerformanceCounterCategory = new System.Diagnostics.
        PerformanceCounterCategory();
    
    
    
布局约定

好的布局使用格式设置来强调代码的结构,并使该代码更易于阅读。 Microsoft 示例符合以下约定:

  • 使用默认代码编辑器设置(智能缩进、四字符缩进、将制表符保存为空格)。 有关更多信息,请参见 “选项” 对话框 ->“文本编辑器”->“C#”->“格式设置”

  • 每行仅编写一个语句。

  • 每行仅编写一个声明。

  • 如果续行不自动缩进,将它们缩进一个制表位(四个空格)。

  • 在方法定义和属性定义之间添加至少一个空白行。

  • 使用括号突显表达式中的子句,如下面的代码所示。  

    if ((val1 > val2) && (val1 > val3))
    {
        // Take appropriate action.
    }
    
    
    
注释约定
  • 将注释放到另一行,而不要放在代码行的末尾。

  • 以大写字母作为注释文本的开头。

  • 以句点结束注释文本。

  • 在注释分隔符 (//) 和注释文本之间插入一个空格,如以下示例所示。  

    // The following declaration creates a query. It does not run
    // the query.
    
  • 请勿在注释周围创建已设置格式的星号块。

  • 语言指南

    以下各部分描述了 C# 团队准备代码示例时遵循的做法。

    String 数据类型
    • 使用 + 运算符来连接短字符串,如以下代码所示。  

      string displayName = nameList[n].LastName + ", " + nameList[n].FirstName;
      
  • 若要在循环中附加字符串,尤其是在您处理大量文本时,请使用 StringBuilder 对象。  

    var phrase = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
    var manyPhrases = new StringBuilder();
    for (var i = 0; i < 10000; i++)
    {
        manyPhrases.Append(phrase);
    }
    //Console.WriteLine("tra" + manyPhrases);
    
  • 隐式类型化局部变量
    • 如果赋值语句右侧的变量类型十分明显,或在精确的类型并不重要时,使用 隐式类型 的局部变量。  

      // When the type of a variable is clear from the context, use var 
      // in the declaration.
      var var1 = "This is clearly a string.";
      var var2 = 27;
      var var3 = Convert.ToInt32(Console.ReadLine());
      
      
  • 赋值语句右侧的类型不明显时,不要使用 var  

    // When the type of a variable is not clear from the context, use an
    // explicit type.
    int var4 = ExampleClass.ResultSoFar();
    
    
  • 不要依赖变量名来指定变量的类型。 这可能是不正确的。  

    // Naming the following variable inputInt is misleading. 
    // It is a string.
    var inputInt = Console.ReadLine();
    Console.WriteLine(inputInt);
    
    
  • 避免使用 var 取代 dynamic

  • 使用隐式类型在 forforeach 循环中确定循环变量的类型。

    下面的示例在 for 语句中使用了隐式类型。  

    var syllable = "ha";
    var laugh = "";
    for (var i = 0; i < 10; i++)
    {
        laugh += syllable;
        Console.WriteLine(laugh);
    }
    
    
    

    下面的示例在 foreach 语句中使用了隐式类型。  

    foreach (var ch in laugh)
    {
        if (ch == ''h'')
            Console.Write("H");
        else
            Console.Write(ch);
    }
    Console.WriteLine();
    
    
    
  • 无符号数据类型 数组
    • 在您初始化声明行上的数组时,请使用简洁的语法。  

      // Preferred syntax. Note that you cannot use var here instead of string[].
      string[] vowels1 = { "a", "e", "i", "o", "u" };
      
      
      // If you use explicit instantiation, you can use var.
      var vowels2 = new string[] { "a", "e", "i", "o", "u" };
      
      // If you specify an array size, you must initialize the elements one at a time.
      var vowels3 = new string[5];
      vowels3[0] = "a";
      vowels3[1] = "e";
      // And so on.
      
    委托
    • 请使用简洁的语法来创建委托类型的实例。

      // First, in class Program, define the delegate type and a method that  // has a matching signature. // Define the type. public delegate void Del( string message);

      // Define a method that has a matching signature. public static void DelMethod( string str)
      {
          Console.WriteLine( "DelMethod argument: {0}", str);
      }

      // In the Main method, create an instance of Del. // Preferred: Create an instance of Del by using condensed syntax.
      Del exampleDel2 = DelMethod;

      // The following declaration uses the full syntax.
      Del exampleDel1 = new Del(DelMethod);


    异常处理中的 try-catch 和 using 语句
    • 为大多数异常处理使用 try-catch 语句。  

      static string GetValueFromArray(string[] array, int index)
      {
          try
          {
              return array[index];
          }
          catch (System.IndexOutOfRangeException ex)
          {
              Console.WriteLine("Index is out of range: {0}", index);
              throw;
          }
      }
      
      
      
    • 使用 C# using 语句简化代码。 如果有一个 try-finally 语句,其中 finally 块中的唯一代码为对 Dispose 方法的调用,请改用 using 语句。

       
      // This try-finally statement only calls Dispose in the finally block.
      Font font1 = new Font("Arial", 10.0f);
      try
      {
          byte charset = font1.GdiCharSet;
      }
      finally
      {
          if (font1 != null)
          {
              ((IDisposable)font1).Dispose();
          }
      }
      
      
      // You can do the same thing with a using statement.
      using (Font font2 = new Font("Arial", 10.0f))
      {
          byte charset = font2.GdiCharSet;
      }
      
      
      
    && 和 || 运算符
    • 若要跳过不必要的比较来避免出现异常和提高性能,在执行比较时,请使用 && 代替 &,使用 || 代替 | ,如以下示例所示。  

      Console.Write("Enter a dividend: ");
      var dividend = Convert.ToInt32(Console.ReadLine());
      
      Console.Write("Enter a divisor: ");
      var divisor = Convert.ToInt32(Console.ReadLine());
      
      // If the divisor is 0, the second clause in the following condition
      // causes a run-time error. The && operator short circuits when the
      // first expression is false. That is, it does not evaluate the
      // second expression. The & operator evaluates both, and causes 
      // a run-time error when divisor is 0.
      if ((divisor != 0) && (dividend / divisor > 0))
      {
          Console.WriteLine("Quotient: {0}", dividend / divisor);
      }
      else
      {
          Console.WriteLine("Attempted division by 0 ends up here.");
      }
      
      
    New 运算符
    • 通过隐式类型使用简洁形式的对象实例,如以下声明所示。

       
      var instance1 = new ExampleClass();
      
      
      

      上面的行与以下声明等效。

       
      ExampleClass instance2 = new ExampleClass();
      
      
    • 使用对象初始值设定项来简化对象创建。

       
      // Object initializer.
      var instance3 = new ExampleClass { Name = "Desktop", ID = 37414, 
          Location = "Redmond", Age = 2.3 };
      
      // Default constructor and assignment statements.
      var instance4 = new ExampleClass();
      instance4.Name = "Desktop";
      instance4.ID = 37414;
      instance4.Location = "Redmond";
      instance4.Age = 2.3;
      
    事件处理
    • 如果您正在定义无需日后移除的事件处理程序,请使用 lambda 表达式。

      public Form2()
      {
          // You can use a lambda expression to define an event handler. this.Click += (s, e) =>
              {
                  MessageBox.Show(
                      ((MouseEventArgs)e).Location.ToString());
              };
      }


      // Using a lambda expression shortens the following traditional definition. public Form1()
      {
          this.Click += new EventHandler(Form1_Click);
      }

      void Form1_Click( object sender, EventArgs e)
      {
          MessageBox.Show(((MouseEventArgs)e).Location.ToString());
      }
    静态成员
    • 使用类名称调用 static 成员:ClassName.StaticMember不要从派生类访问在基类中定义的静态成员。

    LINQ 查询
    • 对于查询变量使用有意义的名称。 下面的示例使用 seattleCustomers 代表地处西雅图的客户。

       
      var seattleCustomers = from cust in customers
                             where cust.City == "Seattle"
                             select cust.Name;
      
      
      
    • 使用别名以确保通过 Pascal 大小写格式正确设置匿名类型的属性名称的大小写。

       
      var localDistributors =
          from customer in customers
          join distributor in distributors on customer.City equals distributor.City
          select new { Customer = customer, Distributor = distributor };
      
      
      
    • 在结果中的属性名称不明确时重命名属性。 例如,如果查询返回一个客户名称和一个分销商 ID,不要在结果中将它们保留为 NameID,而是将它们重命名,以阐明 Name 是客户的名称,ID 是分销商的 ID。

       
      var localDistributors2 =
          from cust in customers
          join dist in distributors on cust.City equals dist.City
          select new { CustomerName = cust.Name, DistributorID = dist.ID };
      
      
      
    • 使用隐式类型来声明查询变量和范围变量。

       
      var seattleCustomers = from cust in customers
                             where cust.City == "Seattle"
                             select cust.Name;
      
      
      
    • 将查询子句对齐在 from 子句下方,如前面的示例所示。

    • 在其他查询子句前面使用 where 子句,以确保对一组经过简化和筛选的数据执行后面的查询子句。

       
      var seattleCustomers2 = from cust in customers
                              where cust.City == "Seattle"
                              orderby cust.Name
                              select cust;
      
      
      
    • 使用多个 from 子句而不是一个 join 子句来访问内部集合。 例如,在 Student 对象集合中,每个对象可能都包含一个考试成绩集合。 在执行下面的查询时,它将返回每一个超过 90 的分数,以及获得该分数的学生的姓氏。

       
      // Use a compound from to access the inner sequence within each element.
      var scoreQuery = from student in students
                       from score in student.Scores
                       where score > 90
                       select new { Last = student.LastName, score };
      
      
      
    安全性

    遵循代码安全维护指南中的指南。


    原文链接: http://www.cnblogs.com/pugang/archive/2011/10/10/2206582.html

    C# 编码约定(C# 编程指南)

    C# 编码约定(C# 编程指南)

    C#注释约定

    • 将注释放到另一行,而不要放在代码行的末尾。

    • 以大写字母作为注释文本的开头。

    • 以句点结束注释文本。

    • 在注释分隔符 (//) 和注释文本之间插入一个空格,如以下示例所示。

    • // The following declaration creates a query. It does not run
      // the query.
    • 请勿在注释周围创建已设置格式的星号块。

     

    C#语言指南

    以下各部分描述了 C# 团队准备代码示例时遵循的做法。

    String 数据类型

    • 使用 + 运算符来连接短字符串,如以下代码所示。

    var phrase = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
    var manyPhrases = new StringBuilder();
    {
        manyPhrases.Append(phrase);
    }
    //Console.WriteLine("tra" + manyPhrases);

    关于HTML 代码约定html预定义的标签的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于c# – 如何在接口中配置代码约定、c# – 私有静态只读字段上的代码约定和失败、C# 编码约定、C# 编码约定(C# 编程指南)的相关知识,请在本站寻找。

    本文标签: