SoFunction
Updated on 2025-03-07

C# Detailed explanation of the method of deleting a certain value or a set of values ​​in an array

Recently, a permission verification function has been optimized. Before, every time other systems obtain the permission configuration of their respective systems, SSO will find the local corresponding permission file to read and parse it once.

Although this design can achieve functionality, this repeated reading strategy is not economical, especially in high concurrency, it is more likely to become a performance bottleneck.

So I optimized this business, and during the optimization process, I tried some writing methods on how to remove certain parameters in the array. The following records what I think is more elegant writing methods.

First, let’s talk about the scenario. The permissions of multiple systems are uniformly controlled by SSO. Then, after each user logs into other systems, he or she needs to request “the user’s existing permissions in the system” or “whether the user currently has the operation permissions for this function” from SSO.

Then sso needs to obtain the user's current permission information after the permission verification is passed and return it. So in the service of permission verification, I first obtain the corresponding complete permission tree based on the existing system type in the constructor. If I find that there is a missing file, I will try to read the file. If I don't have any missing, I will directly read the cache configuration.

The existing system records through enumeration, assuming that the following definitions are as follows:

public enum EnumSystemType
{
  NoMenu = 0, // Systems or programs that do not require permission trees  App01 = 1,
  App02 = 2,
  ......
}

The permission configuration of each system is saved in its own file, for example, the complete permission tree of App01 is saved in it.

Then, the content of each file is obtained according to the existing system and parsed into a permission tree to cache it. When obtaining file content, the enumeration (NoMenu) that does not need to obtain permissions needs to skip the logic of finding files to read. Here I use the function of linq to implement the logic of filtering the enum value.

using ;

private Dictionary<string, List<PermissionNode>> GetPermissions()
{
  //todo

  foreach (var fileName in (typeof(EnumSystemType))
   .Where(s => s != nameof())
   .ToArray())
  {
    //todo
  }

  //todo
}

PS.The return value is a string array.

The filtering of a single parameter is easily implemented through where. I suddenly thought of a question: How to batch filter some enum values?

After checking the information, linq has indeed a solution, which is to use "Except".

Then I will try the above example. If I want the constructor to skip NoMenu and App01 when reading, I can write it like this:

using ;

private Dictionary<string, List<PermissionNode>> GetPermissions()
{
  //todo

  var exceptPermissions = new []
  {
    nameof(),
    nameof(EnumSystemType.App01),
  };

  foreach (var fileName in (typeof(EnumSystemType))
    .Except(exceptPermissions)
    .ToArray())
  {
    //todo
  }

  //todo
}

Summarize:

1. How to remove a value in an array:Where

2. How to remove multiple values ​​in an array:linq's except

How to delete an element in an array

How to delete an element in an array, the remaining elements form a new array, and the array name remains unchanged

double[] arr = new double[n];

What needs to be deleted is the m+1 data arr[m]

Find new array arr. (New array arr contains n-1 elements)

The values ​​of m, n are known

double[] arr = new double[50];
List<double> list = ();
(5+1);
double[] newarr = ();

The above is the detailed explanation of the method of deleting a certain value and a set of values ​​in an array in C#. For more information about how to delete a certain value and a set of values ​​in an array in C#, please pay attention to my other related articles!