Found 34494 Articles for Programming

C# program to remove n-th character from a string

Samual Sam
Updated on 23-Jun-2020 09:43:17

652 Views

To remove a character, use the remove() method and set the index from where you want to delete the character.Firstly, set the string.string str1 = "Amit"; Console.WriteLine("Original String: "+str1);To delete a character at position 4.StringBuilder strBuilder = new StringBuilder(str1); strBuilder.Remove(3, 1);You can try to run the following code to remove nth character from a string.Example Live Demousing System; using System.Text; public class Demo {    public static void Main(string[] args) {       string str1 = "Amit";       Console.WriteLine("Original String: "+str1);       StringBuilder strBuilder = new StringBuilder(str1);       strBuilder.Remove(3, 1);       str1 ... Read More

Covariance and Contravariance in C#

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

205 Views

To deal with classes effectively, use the concept of covariance and contra variance. Let us consider the following as our class. One is a base class for class Two, whereas Two is a base class for Three. class One { } class Two: One { } class Three : Two { } A base class can hold a derived class, but the opposite is not possible. With Covariance, you can pass a derived type where a base type is expected. Co-variance can be used on array, interface, delegates, etc in C#. Contra variance is for parameters. ... Read More

C# program to replace n-th character from a given index in a string

Arjun Thakur
Updated on 23-Jun-2020 09:43:40

1K+ Views

Firstly, set a string.string str1 = "Port"; Console.WriteLine("Original String: "+str1);Now convert the string into character array.char[] ch = str1.ToCharArray();Set the character you want to replace with the index of the location. To set a character at position 3rd.ch[2] = 'F';To remove nth character from a string, try the following C# code. Here, we are replacing the first character.Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       string str1 = "Port";       Console.WriteLine("Original String: "+str1);       char[] ch = str1.ToCharArray();       ch[0] = 'F';   ... Read More

Cohesion in C#

Samual Sam
Updated on 30-Jul-2019 22:30:23

1K+ Views

Cohesion in C# shows the relationship within modules. It shows the functional strength of the modules. The greater the cohesion, the better will be the program design. It is the dependency between the modules internal elements like methods and internal modules. High cohesion will allow you to reuse classes and method. An example of High cohesion can be seen in System.Math class i.e.it has Mathematical constants and static methods − Math.Abs Math.PI Math.Pow A class that does a lot of things at a time is hard to understand and maintain. This is what we call low cohesion and ... Read More

Decimal Functions in C#

Chandu yadav
Updated on 23-Jun-2020 09:44:15

188 Views

The following are some of the decimal functions in C#.Sr.No.Name & Description1Add (Decimal, Decimal)Adds two specified Decimal values.2Ceiling(Decimal)Returns the smallest integral value that is greater than or equal to the specified decimal number.3Compare (Decimal, Decimal)Compares two specified Decimal values.4CompareTo(Decimal)Compares this instance to a specified Decimal object and returns a comparison of their relative values.5CompareTo(Object)Compares this instance to a specified object and returns a comparison of their relative values.6Divide (Decimal, Decimal)Divides two specified Decimal values.7Equals(Decimal)Returns a value indicating whether this instance and a specified Decimal object represent the same value.Let us see an example of Decimal Ceiling() method in C# that returns the smallest integral value greater than or equal to ... Read More

Coupling in C#

George John
Updated on 14-Apr-2020 11:09:33

2K+ Views

Coupling shows the relationship between modules in C# or you can say the interdependence between modules.There are two types of coupling i.e tight and loose coupling.Loose CouplingLoose coupling is preferred since through it changing one class will not affect another class. It reduces dependencies on a class. That would mean you can easily reuse it.Writing loosely coupled code has the following advantages −One module won’t break other modulesEnhances testabilityCode is easier to maintainGets less affected by changes in other components.Tight CouplingIn Tight Coupling, the classes and objects are dependent on each other and therefore reduce re-usability of code.Read More

Database Operations in C#

Samual Sam
Updated on 30-Jul-2019 22:30:23

2K+ Views

The most common databases used in C# are Microsoft SQL Server and Oracle. The following is done to work with databases. Connect Set the Database Name, Optional Parameters and Credentials. The username and password is needed to set a connection to the database. The connection string would look somewhat like this. private static string _connectionString = "Data Source=.;Integrated Security=SSPI;Initial Catalog=test;Application Name=Demo;Connection Timeout2w00"; Above, the Application Name is Demo. Select Statement To fetch data from the database, the SELECT statement is used Insert The INSERT command is used to insert data in the database. Update The database ... Read More

Counters in C#

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

1K+ Views

Counters in C# are performance counters that lets you know about your applications performance. When you will build an application, whether it is a web app, mobile app or even desktop app, you would definitely need to monitor the performance. For performance counters in C#, use the System.Diagnostics.PerformanceCounter class. Set the instance of the PerformanceCounter class and work with the following properties: CategoryName, CounterName, MachineName and ReadOnly. To get the performance categories. var counter = PerformanceCounterCategory.GetCategories(); Now SET performance counters for the category processor. var counter = PerformanceCounterCategory.GetCategories() .FirstOrDefault(category => category.CategoryName == "Processor");

Date Class in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:49:22

13K+ Views

To set dates in C#, use DateTime class. The DateTime value is between 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D.Let’s create a DateTime object.Example Live Demousing System; class Test {    static void Main() {       DateTime dt = new DateTime(2018, 7, 24);       Console.WriteLine (dt.ToString());    } }Output7/24/2018 12:00:00 AMLet us now get the current date and time.Example Live Demousing System; class Test {    static void Main() {       Console.WriteLine (DateTime.Now.ToString());    } }Output9/17/2018 5:49:21 AMNow using the method Add(), we will add days in a date with the ... Read More

Events vs Delegates in C#

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

363 Views

C# events are used to resolve the hassles in Delegates. One an easily override Delegate properties and that can eventually lead to errors in the code. To avoid this, C# uses Events and defines wrappers around Delegates. Events in C# To use Event, you should define delegate first. Event is a type of Delegate and an example of event can be when a key is pressed. public delegate voide Demo(String val); public event Test TestEvent; An event can hold a delegate like this. this.TestEvent += new Demo (DemoData); ... Read More

Advertisements