Question What telement mean here and when it use it?

ahmedsalah

Active member
Joined
Sep 26, 2018
Messages
32
Programming Experience
3-5
I work on .net core 6 vs 2022 i see function have telement
but i don't know what this mean
can you show me please ?
and when use it
are there are any thing best from using telement or more general

What I have tried:

what mean Telement and why use it:
public List<TElement> SQLQuery<TElement>(string sql, string? connectionStringName = null, params StoredParameter[] parameters)
      {
          string ConnectionString = connectionStringName == null ? _ApplicationConfiguration.ConnectionString : _ApplicationConfiguration.GetConnectionString(connectionStringName);
          using (SqlConnection sqlConn = new SqlConnection(ConnectionString))
          {
              using (SqlCommand cmd = new SqlCommand(sql, sqlConn))
              {
                  cmd.CommandText = sql;
                  cmd.CommandType = System.Data.CommandType.StoredProcedure;
                  cmd.CommandTimeout = (int)TimeSpan.FromMinutes(5).TotalSeconds;
                  //cmd.Parameters.AddRange(parameters);
                  foreach (var parameter in parameters)
                  {
                      var paramter = cmd.CreateParameter();
                      paramter.ParameterName = parameter.ParameterName;
                      paramter.Value = parameter.ParameterValue;
                      if (!string.IsNullOrEmpty(parameter.TypeName))
                      {
                          paramter.SqlDbType = SqlDbType.Structured;
                          paramter.TypeName = parameter.TypeName;
                      }
                      cmd.Parameters.Add(paramter);
                  }

                  sqlConn.Open();
                  using (var reader = cmd.ExecuteReader())
                  {
                      var result = reader.MapToList<TElement>();
                      sqlConn.Close();
                      return result;
                  }
              }
          }
 
Read about generic methods here:
 
These are called Generics, but before I go into some light detail about them, first a couple of minor points:

* "telement" is actually TElement, and it's an important distinction to make; you'd say it like "tee element" and it means "the type of the element [in the returned list]" - you wouldn't say it like "tell a ment"

* It doesn't have to be TElement either - anything in angle brackets can be the type name placeholder. We tend to use T or TSomething where the something explains more about what the type is used for. Maybe you'll also see Something<T, U, V ...>

So..

Generics are a bit like a template; a way of getting the compiler to write code for you. In the old days we would make type specific classes for things like collections, but we ended up with lots of code that was basically the same thing apart from the type names:

C#:
  class IntCollection{
    int[] arr = new int[10];
    int idx = 0;

    void Add(int toAdd){
      arr[idx] = toAdd;
      idx++;
    }

    void RemoveLast(){
      idx--;
      arr[idx] = default;
    }
  }

Oh, now we need a string one, copy paste find/replace int->string:

C#:
  class StringCollection{
    string[] arr = new string[10];
    int idx = 0;

    void Add(string toAdd){
      arr[idx] = toAdd;
      idx++;
    }

    void RemoveLast(){
      idx--;
      arr[idx] = default;
    }
  }

Oh, now we need a bool one, a Person one, a string[] one...

We could just make an object[] based one, and use object everywhere.. But then we have to remember what it is, and fill our code with casts so it just descends into a mess..

Nothing changes from class to class, other than the type of data being stored. Here's an idea; if we turn the type into a *parameter* we can "pass in", then we could get the compiler to churn out all the classes for us (do the find replace) every time it sees us using a different type. This is cool because we have strongly typed specific collections, so no casting and intellisense is nice; Visual Studio can know what kind of objects are in our collection and give us intellisense on them.

C#:
  class GenericCollection<T>{
    T[] arr = new T[10];
    int idx = 0;

    void Add(T toAdd){
      arr[idx] = toAdd;
      idx++;
    }

    void RemoveLast(){
      idx--;
      arr[idx] = default;
    }
  }

We've made a parameter of a type here, that we "pass in" just by using it in code:

C#:
      var xcollection = new GenericCollection<Person>();

Now the compiler has all it needs to write something like this for us:

C#:
  class PersonCollection{
    Person[] arr = new Person[10];
    int idx = 0;

    void Add(Person toAdd){
      arr[idx] = toAdd;
      idx++;
    }

    void RemoveLast(){
      idx--;
      arr[idx] = default;
    }
  }

And we wrote some tiny amount of code, really..
 
Last edited:
In the same vein as your comment on the other thread about presenting things to newbies, please present C# code as looking like C# code along with the recommended Allman indentation, instead of the Java/JavaScript recommended OBS indentation.
 
Hmm.. If I understand you right, I think it's a bit of a stretch to say that this looks like C#:

C#:
class PersonCollection
{
    Person[] arr = new Person[10];
    int idx = 0;
}

but this does not:

C#:
class PersonCollection {
    Person[] arr = new Person[10];
    int idx = 0;
}

I've have edited it to match the style the OP posted, but it seems beyond the edit window grace now.. Oh well; next time!
 
Every language has it's own style and idioms. It is usually best to teach people what to expect to see while they are learning.

I say this because I unfortunately was taught C++ as C-with-classes which set me back for a while.
 
In other controversial style news, I've adopted single line statements if braces are omitted..

C#:
if(sometest) return somevalue;
else return othervalue;

foreach(var v in vs) v.Property = somevalue;

..as a counter to that "the next dev might add a statement and not realize it's not in the else block" complaint (though I feel that argument hasn't held much water for years, given that VS would auto indent the new statement at the if/else level) levelled at braceless next line indented blocks:


C#:
if(sometest)
  return somevalue;
else 
  return othervalue;

foreach(var v in vs) 
  v.Property = somevalue;
 
as a counter to that "the next dev might add a statement and not realize it's not in the else block" complaint (though I feel that argument hasn't held much water for years, given that VS would auto indent the new statement at the if/else level) levelled at braceless next line indented blocks:
Not all developers will be using Visual Studio, Visual Code, nor a text editor that understands and auto indents C#.

I still fire up notepad or vi to do quick edits.
 
Back
Top Bottom