Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

* Scripting Languages
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* aktuális (2023/24/2)
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

20180401b

How to extend a class from the standard library with an own method? For instance, the string class has no Capitalize() method but you would like to use "hello".Capitalize(), which would return "Hello".

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());
        }
    }
}

Tip from here. See the current version here.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2018 April 07, 23:53