I'm a VB.Net guy. (because I have to be, because the person who signs my check says so. :P) I grew up in Java and I don't generally struggle to read or write in C# when I get the chance. I came across some syntax today that I have never seen, and that I can't seem to figure out.
In the following method declaration, what does < T > represent?
static void Foo < T >(params T[] x)
I have seen used in conjunction with declaring generic collections and things, but I can't for the life of me figure out what it does for this method.
In case it matters, I came across it when thinking about some C# brain teasers. The sixth teaser contains the entire code snippet.
The ' |= ' symbol is the bitwise OR assignment operator.
In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...
Summary. An operator is a symbol which operates on a variable or value. There are types of operators like arithmetic, logical, conditional, relational, bitwise, assignment operators etc. Some special types of operators are also present in C like sizeof(), Pointer operator, Reference operator etc.
In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.
what you are asking is the concept of the generics in c#. By using generics you can use this method for the types you want
suppose you have to create a function to add two numbers. In that case, your function is
//For integer : public int sum(int a, int b) { return a + b; } //For floating point numbers : public float sum( float a, float b) { return a + b; }
Following this logic, if you want a function that will sum two double type numbers you would create one more function, and so on.
Note: code above will not work with C#, but it for explaining concept easily, it just sudo code it will work with C# if you have nullable type or reference type easily or one need to write logic to convert value to primary type.
Bu with the help of generics you can replace all of these functions and write the following,
public T sum<T>(T a, T b) { return a + b; }
This will work for all numeric types, as well as for strings.
check this out for more detail : http://www.codeproject.com/kb/books/EssentialCS20.aspx
This defines a generic method, which is one form of generics, which were added to C# in C# 2.0.
The method sig should be:
static void Foo<T>(params T[] x) { // ...
This lets you pass any number of arguments of any (specific) type into the method Foo, and it acts on that array of arguments. It's similar to how generic types work, except scoped just to the method. The <T>
specifies the type of the argument being passed into the method, so you can call this like:
Foo<MyClass>(myClassInstance, myClassInstance2, mySubclassInstance);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With