Oktatás * Programozás 2 * Szkriptnyelvek * levelezősök Félévek Linkek * kalendárium |
CSharp /
20180401bHow to extend a class from the standard library with an own method? For instance, the using System; using static System.Console; using System.Linq; using System.Collections; using System.Collections.Generic; namespace StringExtensions { //Extension methods must be defined in a static class public static class MyStringExtension { // This is the extension method. // The first parameter takes the "this" modifier // and specifies the type for which the method is defined. public static string Capitalize(this string s) { if (string.IsNullOrEmpty(s)) return s; // else return s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower(); } } } namespace cs2 { using StringExtensions; class Program { private static string Input(string text = "") { Write(text); return ReadLine(); } public static void Main(string[] args) { var name = Input("Your name: "); WriteLine("Hello {0}!", name.Trim().Capitalize()); } } } |
Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |