c#中this和base的区别

综合来说: 

this相当于自己的指针;base相当于父类的指针;
一般只需要对自己操作就行了。如果是你自己写的类,继承了别的类,就会用到base了。

以下为MSDN上得帮助解释;

base 关键字用于从派生类中访问基类的成员:

  • 调用基类上已被其他方法重写的方法。

  • 指定创建派生类实例时应调用的基类构造函数。

基类访问只能在构造函数、实例方法或实例属性访问器中进行。

从静态方法中使用 base 关键字是错误的。

在本例中,基类 Person 和派生类 Employee 都有一个名为 Getinfo 的方法。通过使用base 关键字,可以从派生类中调用基类的Getinfo 方法。

using System;
public class Person
{
   
 protected string ssn = "444-55-6666";
    protected string name = "John L. Malgraine";
    public virtual void GetInfo()
  
  {
  
      Console.WriteLine("Name: {0}", name);
   
      Console.WriteLine("SSN: {0}", ssn);
  
  }
}
class Employee : Person
{
  
  public string id = "ABC567EFG";
    public override void GetInfo()
  
  {
   
     // Calling the base class GetInfo method:
  
      base.GetInfo();
   
     Console.WriteLine("Employee ID: {0}", id);
  
  }
}

class TestClass
{
  
  static void Main()
  
  {
   
     Employee E = new Employee();
   
     E.GetInfo();
   
 }
}

本示例显示如何指定在创建派生类实例时调用的基类构造函数。

// keywords_base2.cs
using System;
public class BaseClass
{
  
  int num;

    public BaseClass()
    
{
      
  Console.WriteLine("in BaseClass()");
   
 }

 
   public BaseClass(int i)
   
 {
    
    num = i;
    
    Console.WriteLine("in BaseClass(int i)");
  
  }

   
 public int GetNum()
   
 {
   
     return num;
  
  }
}

public class DerivedClass : BaseClass
{
    // This constructor will call BaseClass.BaseClass()
  
  public DerivedClass() : base()
   
 {
  
  }

  
  // This constructor will call BaseClass.BaseClass(int i)
 
   public DerivedClass(int i) : base(i)
   
 {
   
 }

  
  static void Main()
   
 {
     
   DerivedClass md = new DerivedClass();
    
    DerivedClass md1 = new DerivedClass(1);
   
 }
}

Name: John L. Malgraine
SSN: 444-55-6666
Employee ID: ABC567EFG

有关其他示例,请参见 newvirtualoverride

in BaseClass()
in BaseClass(int i)

 

 

 

this 关键字引用类的当前实例。

以下是 this 的常用用途:

  • 限定被相似的名称隐藏的成员,例如:

    public Employee(string name, string alias) 
    {
        this.name = name;
        this.alias = alias;
    } 
    
  • 将对象作为参数传递到其他方法,例如:

    CalcTax(this);
    
  • 声明索引器,例如:

    public int this [int param]
    {
        get { return array[param];  }
        set { array[param] = value; }
    }
    

由于静态成员函数存在于类一级,并且不是对象的一部分,因此没有 this 指针。在静态方法中引用 this 是错误的。

在本例中,this 用于限定 Employee 类成员 namealias,它们都被相似的名称隐藏。this 还用于将对象传递到属于其他类的方法CalcTax

// keywords_this.cs
// this example
using System;
class Employee
{
  
  private string name;
  
  private string alias;
 
   private decimal salary = 3000.00m;

  
  // Constructor:
  
  public Employee(string name, string alias)
   
 {
    
    // Use this to qualify the fields, name and alias:
    
    this.name = name;
   
     this.alias = alias;
  
  }

   
 // Printing method:
    
public void printEmployee()
   
 {
    
    Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
    
    // Passing the object to the CalcTax method by using this:
   
     Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));
   
 }

   
 public decimal Salary
    
{
    
    get { return salary; }
  
  }
}
class Tax
{
    
public static decimal CalcTax(Employee E)
    
{
        return 0.08m * E.Salary;
   
 }
}

class MainClass
{
   
 static void Main()
   
 {
      
  // Create objects:
      
  Employee E1 = new Employee("John M. Trainer", "jtrainer");

      
  // Display results:
        
E1.printEmployee();
   
 }
}
Name: John M. Trainer
Alias: jtrainer
Taxes: $240.00
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一个简单的示例代码来实现AES加密和解密程序。 首先,您需要在您的项目添加System.Security.Cryptography命名空间,然后可以使用Aes类来执行加密和解密操作。 以下是一个简单的代码示例,它使用AES加密算法来加密和解密字符串: ```csharp using System; using System.Security.Cryptography; using System.Text; namespace AesEncryptionDemo { public class AesEncryption { private static readonly byte[] Salt = Encoding.ASCII.GetBytes("This is my salt value"); public static string Encrypt(string plainText, string password) { byte[] encryptedBytes; using (Aes aes = Aes.Create()) { Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, Salt); aes.Key = key.GetBytes(aes.KeySize / 8); aes.IV = key.GetBytes(aes.BlockSize / 8); ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); encryptor.Dispose(); } return Convert.ToBase64String(encryptedBytes); } public static string Decrypt(string encryptedText, string password) { byte[] encryptedBytes = Convert.FromBase64String(encryptedText); string plainText; using (Aes aes = Aes.Create()) { Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, Salt); aes.Key = key.GetBytes(aes.KeySize / 8); aes.IV = key.GetBytes(aes.BlockSize / 8); ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); plainText = Encoding.UTF8.GetString(decryptedBytes); decryptor.Dispose(); } return plainText; } } } ``` 在上面的示例,我们使用了Rfc2898DeriveBytes类来生成密钥和IV向量。这个类使用密码和盐值作为输入,并生成一个密钥和一个IV向量。在加密和解密过程,我们使用这个密钥和IV向量来执行加密和解密操作。 您可以在您的窗体应用程序使用这个示例代码来加密和解密用户输入的数据。例如,您可以在一个文本框接收用户输入的数据,然后使用Encrypt方法加密它,并将结果显示在另一个文本框。同样地,您可以使用Decrypt方法将用户输入的密文解密,并显示结果在一个文本框

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值