c# add strings
string value = "cat ";
// Step 1: append a word to the string.
value += "and ";
Console.WriteLine(value);
// Step 2: append another word.
value += "dog";
Console.WriteLine(value);
//OUTPUT:
cat and
cat and dog
c# add strings
string value = "cat ";
// Step 1: append a word to the string.
value += "and ";
Console.WriteLine(value);
// Step 2: append another word.
value += "dog";
Console.WriteLine(value);
//OUTPUT:
cat and
cat and dog
c# add method to type
using System.Linq;
using System.Text;
using System;
namespace CustomExtensions
{
// Extension methods must be defined in a static class.
public static class StringExtension
{
// This is the extension method.
// The first parameter takes the "this" modifier
// and specifies the type for which the method is defined.
public static int WordCount(this String str)
{
return str.Split(new char[] {' ', '.','?'}, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
namespace Extension_Methods_Simple
{
// Import the extension method namespace.
using CustomExtensions;
class Program
{
static void Main(string[] args)
{
string s = "The quick brown fox jumped over the lazy dog.";
// Call the method as if it were an
// instance method on the type. Note that the first
// parameter is not specified by the calling code.
int i = s.WordCount();
System.Console.WriteLine("Word count of s is {0}", i);
}
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us