DirectorySoftwareBlog Details for "Learn C sharp"

Learn C sharp

Learn C sharp
A blog which helps novice programmers learn c sharp 2005
Articles: 1, 2, 3, 4

Articles

Common Operations
2007-06-25 05:12:00
C# Language BasicsVisual C# 2005 Express Edition Working With Numbersint i = 0;// convert from a stringint i = int.Parse("1");// convert froma string and don't throw exceptionsif (int.TryParse("1", out i)) {}i++; // increment by onei--; // decrement by onei += 10; // add 10i -= 10; // subtract 10i *= 10; // multiply by 10i /= 10; // divide by 10i = checked(i*2) // check for overfl owi = unchecked(i*2) // ignore overfl ow Dates and TimesDateTime now = DateTime.Now; // date and timeDateTime today = DateTime.Today; // just the dateDateTime tomorrow = today.AddDays(1);DateTime yesterday = today.AddDays(-1);TimeSpan time = tomorrow - today;int days = time.Days;int hours = time.Hours;int minutes = time.Minutes;int seconds = time.Seconds;int milliseconds = time.Milliseconds;time += new TimeSpan(days, hours, minutes, seconds,milliseconds); Conditional Logicif (i == 0) {}if (i <= 10) {} else {}switch (i) {case 0:case 1:break;case 2:break;default:break;} Compare Operatorsif (i == 0) /...
More About: Common , Comm , Operation
Delegates
2007-06-24 06:14:00
Topics covered: ? What are delegates? ? Understanding Delegates . ? Why delegates? ? Declaring and using Delegates. Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
Declaring and using delegates
2007-06-24 06:12:00
A delegate is declared using the keyword delegate. The general form of a delegate declaration is: Delegate ret-type name ( parameter list) Where ret-type is the type of the value returned by the... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: Delegates , Clar
Reversing a Two-Dimensional Array
2007-06-24 06:12:00
Problem You need to reverse each row in a two-dimensional array. The Array .Reverse method does not support this operation. Solution Use the following Reverse2DimArray<T> method:     public... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: Dimension , Reversi , Dime , Mens
Declaring and using delegates
2007-06-24 06:12:00
A delegate is declared using the keyword delegate. The general form of a delegate declaration is:Delegate ret-type name ( parameter list)Where ret-type is the type of the value returned by the methods that the delegate will be referring to. Name is the name of the delegate. A delegate can refer to only those methods whose signature exactly match as that specified in the delegate declaration. class Program { //Delegate shutdown declared. public delegate void Shutdown(); class CurrentProcess { public static void endCurrentProcess() { Console.WriteLine("Current process closed"); } } class BackGroundProcess { public static void endBackGroundProcess() { Console.WriteLine("...
More About: Delegates , Clar
Reversing a Two-Dimensional Array
2007-06-23 06:57:00
ProblemYou need to reverse each row in a two-dimensional array. The Array .Reverse method does not support this operation.SolutionUse the following Reverse2DimArray<T> method:    public static void Reverse2DimArray<T>(T[,] theArray)    {     for (int rowIndex = 0; rowIndex <= (theArray.GetUpperBound(0)); rowIndex++)     {     for (int colIndex = 0;     colIndex <= (theArray.GetUpperBound(1) / 2);     colIndex++)     {     T tempHolder = theArray[rowIndex, colIndex];     theArray[rowIndex, colIndex] =     theArray[rowIndex, theArray.GetUpperBound(1) - colIndex];     theArray[rowIndex, theArray.GetUpperBound(1) -colIndex] = tempHolder;     }     }    }DiscussionThe following TestReverse2DimArray method shows how the Reverse2DimArray<T> method is used:public static void TestReverse2DimArray()    {     int[,] someArray =     new int[5,3] {{1,2,3},{4,5,6},...
More About: Dimension , Reversi , Dime , Mens
Delegates
2007-06-22 12:50:00
What are delegates? If you are familiar with C/C++, in C# a delegate is similar to a function pointer. "A delegate is a pointer to a method".  Is delegate a method or a class? Delegates are... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
Reversing a Jagged Array
2007-06-22 12:50:00
Problem The Array .Reverse method does not provide a way to reverse each subarray in a jagged array. You need this functionality. Solution Use the ReverseJaggedArray<T> method:     public... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: Reversi
Delegates
2007-06-22 12:50:00
What are delegates?If you are familiar with C/C++, in C# a delegate is similar to a function pointer. "A delegate is a pointer to a method". Is delegate a method or a class?Delegates are implemented as classes derived from System.MulticastDelegate which is derived from the base class System.Delegate. But when you create an instance of a delegate, what you create is also referred to as a delegate. Understanding delegates:A delegate looks and behaves like a normal method when called. However, delegates can refer to different methods at runtime. How does the delegate refer to a method?From my previous posts, reference is nothing but a memory address. E.g. when a reference to an object is created, actually the address of the object is stored. Similarly, every method has a physical location in memory. Delegates store the address of the methods. Thus, a delegate refers to a method.Delegates can be used to refer different methods during the runtime of a program. This is really important fe...
Reversing a Jagged Array
2007-06-22 12:50:00
ProblemThe Array .Reverse method does not provide a way to reverse each subarray in a jagged array. You need this functionality.SolutionUse the ReverseJaggedArray<T> method:    public static void ReverseJaggedArray<T>(T[][] theArray)    {     for (int rowIndex = 0; rowIndex <= (theArray.GetUpperBound(0)); rowIndex++)     {     for (int colIndex = 0;     colIndex <= (theArray[rowIndex].GetUpperBound(0) / 2);     colIndex++)     {     T tempHolder = theArray[rowIndex][colIndex];     theArray[rowIndex][colIndex] =     theArray[rowIndex][theArray[rowIndex].Get UpperBound(0) -    colIndex];     theArray[rowIndex][theArray[rowIndex].Get UpperBound(0) - colIndex] =     tempHolder;     }     }    }DiscussionThe following TestReverseJaggedArray method shows how the ReverseJaggedArray<T> method is used:public static void TestReverseJaggedArray()    { ...
More About: Reversi
Delegates
2007-06-22 08:40:00
Topics covered:? What are delegates?? Understanding Delegates .? Why delegates?? Declaring and using Delegates.
Generating a Class Diagram
2007-06-21 10:54:00
Generating a Class DiagramThe Class View window is useful for displaying the hierarchy of classes and interfaces in a project. Visual Studio 2005 also enables you to generate class diagrams which depict this same information graphically (you can also use a class diagram to add new classes and interfaces, and define methods, properties, and other class members).To generate a new class diagram, click the Project menu, and then click Add New Item. In the Add New Item window select the Class Diagram template and then click Add. This action will generate an empty diagram, and you can create new types by dragging items from the Class Designer category in the Toolbox. You can generate a diagram of all existing classes by clicking and dragging them individually from the Class View window, or by clicking and dragging the namespace to which they belong. The diagram shows the relationships between the classes and interfaces, and you can expand the definition of each class to show its contents....
More About: Classes , Gene , Agra , Gram
Enumerations
2007-06-21 10:03:00
What are enumerations?Enumerations are a set of named integer constants. C# offers an intuitive way of representing numbers in your code. This can be well understood if you want to use the days in a week, one way is to assign each day an integer number ranging from 1-7, but C# allows you to use enumerations. By enumerations you can represent each number by its corresponding day as in real life. For Monday the value 1, for Tuesday value 2 and so on.Declaring an enumeration:enum name { enumeration list}The enumeration list is a comma-separated list of identifiers.enum days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }Here, Monday, Tuesday? is assigned a value starting from the default value 0.Initialize an enumeration:In above example Monday is assigned a value 0, but using 0 is not friendly in this exam...
More About: ENUM
Struct vs Classs
2007-06-21 10:03:00
QuestionStructClassIs this a value type or a reference type?A structure is a value type.A class is a reference type.Do instances live on the stack or the heap?Structure instances are called values and live on the stack.Class instances are called objects and live on the heap.Can you declare a default constructor?NoYesIf you declare your own constructor, will the compiler still generate the default constructor?YesNoIf you don't initialize a field in your own constructor, will the compiler automatically initialize it for you?NoYesAre you allowed to initialize instance fields at their point of declaration?NoYesSource: Internet
More About: Classes
Struct vs Classs
2007-06-21 10:03:00
Question Struct Class Is this a value type or a reference type? A structure is a value type. A class is a reference type. Do instances live on the stack or the heap? Structure instances are called... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: Classes
Enumerations
2007-06-21 10:03:00
What are enumerations? Enumerations are a set of named integer constants. C# offers an intuitive way of representing numbers in your code. This can be well understood if you want to use the days in... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: ENUM
Thanks!
2007-06-18 15:50:00
Everyone for being a part of C# Rocks! It feels really great to write to a real good audience, C# Rocks! has now 50+ posts and is still growing. I now its a long way to go!, but milestones are a part... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: Thanks , Hank
Thanks!
2007-06-18 15:50:00
Everyone for being a part of C# Rocks!It feels really great to write to a real good audience, C# Rocks! has now 50+ posts and is still growing. I now its a long way to go!, but milestones are a part of development isn't it?. So, once again I thank all the readers and visitors for being my valuable audience, I appreciate your each visit. I hope you continue to be a part of C# Rocks!WB
More About: Thanks , Hank
Creating out Parameters
2007-06-18 08:33:00
I assume that you properly went through my post on Using ref parameters, today I'm focussing on the out parameters.The out is short for output. When you pass an out parameter to a method, the compiler checks whether the passed parameters are initialised in the method. In the following example I have passed two arguments using the out keyword. Since, I haven't initialised the parameters in the method swap(), the compiler doesn't compile and returns two errors as follows:private void Form1_Load(object sender, EventArgs e) {   int a = 10; //Two variables int b = 20; //values before swapping Console.WriteLine("value of a = " + a + " value of b = " + b); //Calling the static swap method. //notice here that the two parameters are passed using the out keyword fun.swap( out a,out b); //values after swapping. Console.WriteLine("value of a = " + a + " value of b = ...
More About: Creating , References , Eating , Meters , Meter
Creating out Parameters
2007-06-18 08:33:00
I assume that you properly went through my post on Using ref parameters, today I'm focussing on the out parameters. The out is short for output. When you pass an out parameter to a method, the... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: Creating , References , Eating , Meters , Meter
Using ref Parameters / Passing values by reference.
2007-06-18 06:55:00
You may have noticed when you pass an argument to a method, the method creates a copy of the argument to use it in its code block, irrespective whether the argument is a value type or reference type. To understand this concept clearly, review the following example carefully, in this example two arguments are passed to the static method, swap().class Program { static void Main(string[] args) { int a = 10; //Two variables int b = 20; //values before swapping Console.WriteLine("value of a = " + a + " value of b = " + b); //Calling the static swap method. fun.swap( a , b); //values after swapping. Console.WriteLine("value of a = " + a + " value of b = " + b); Console.Read(); } class fun { //static method swap, takes two arguments. public static void swap( in...
More About: Reference , References , Values , Value , Ferenc
Using ref Parameters / Passing values by reference.
2007-06-18 06:55:00
You may have noticed when you pass an argument to a method, the method creates a copy of the argument to use it in its code block, irrespective whether the argument is a value type or reference type.... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: Reference , References , Values , Ferenc , Meters
Interface Vs Abstract class / when should I use an abstract class or interf
2007-06-15 14:28:00
After going through my posts on interface and abstract class, you can get confused which one should I implement. Well, this is quite a challenging question and only you can answer it. How? Well, when... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: Abstract , Interface , Class , Trac , T class
Interface Vs Abstract class / when should I use an abstract class or interf
2007-06-15 14:28:00
After going through my posts on interface and abstract class, you can get confused which one should I implement. Well, this is quite a challenging question and only you can answer it. How? Well, when you know what you are going to do without worrying much on how you going to achieve it, use an interface and when you know what you are going to implement use an abstract class. Still confused, continue reading, an abstract class can contain many methods, but it requires at least one method to be abstract i.e. an abstract class can provide a general functionally but the specialised functionality is specified by the derived classes. So, an abstract can contain a combination of implemented and non implemented methods. If there are no implemented method(s), then it is serving as an interface and you should consider using an interface in this case. On the other hand if it contains only implemented methods then it can't be abstract, because we need at least one method to be abstract.Whereas...
More About: Abstract , Interface , Class , Stra , Trac
Abstract Class/abstract classes and methods / Using abstract classes
2007-06-15 11:22:00
What is an abstract class/method.In my previous posts, whenever I created an object of some class type. But there are some classes for which we can't create objects. Such classes are called Abstract Class es . These classes are used as base classes in inheritance hierarchies. We refer to them as abstract classes. We can consider abstract classes as incomplete or partial classes, which require further addition. Its only function is to provide an appropriate base class from which other classes can inherit. As such, they determine the nature of the members that the derived class must implement, but it doesn't itself provide any implementation of one ore more members (methods, properties or indexers).An abstract method is created by specifying the abstract type modifier. An abstract method does not include any implementation in the base class. Thus, a derived class must override it. You might think an abstract method works like a virtual method, well quite true! Abstract methods are ...
More About: Inheritance , Methods
Abstract Class/abstract classes and methods / Using abstract classes
2007-06-15 11:22:00
What is an abstract class/method. In my previous posts, whenever I created an object of some class type. But there are some classes for which we can't create objects. Such classes are called... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: Classes , Abstract , Class , Inheritance , Methods
Common Interfaces of the .NET Framework Class Library
2007-06-15 04:52:00
Common interfaces in the .NET Framework Class Library (FCL). These interfaces are implemented and used in the same manner as those you created earlier.Common interfaces of the .NET Framework Class Library. Interface DescriptionIComparableInterface IComparable can also be used to allow objects of a class that implements the interface to be compared to one another. The interface contains one method, CompareTo, that compares the object that calls the method to the object passed as an argument to the method. Classes must implement CompareTo to return a value indicating whether the object on which it is invoked is less than (negative integer return value), equal to (0 return value) or greater than (positive integer return value) the object passed as an argument, using any criteria specified by the programmer. IComponentImplemented by any class that represents a component, including Graphical User Interface (GUI) controls (such as buttons or labels). Interface IComponent defines ...
More About: Common , Comm
Common Interfaces of the .NET Framework Class Library
2007-06-15 04:52:00
Common interfaces in the .NET Framework Class Library (FCL). These interfaces are implemented and used in the same manner as those you created earlier. Common interfaces of the .NET Framework Class... Do you know when to use an abstract class vs an interface?...Do u know how to create your own auto complete TextBox?. Log on to www.csharprox.com
More About: Common , Interface , Comm
Implementing Properties in a interface or Interface properties
2007-06-14 16:17:00
In my previous posts I have specified only methods as a part of interface, now let's specify properties inside an interface.class Program { static void Main(string[] args) { MyClass mc = new MyClass(); mc.no = 2; //set no=2 Console.WriteLine(mc.no); //returns 4 Console.Read(); } interface IProp { int no { get;//return no. set; //sets no. } }    class MyClass:IProp //Implemnet IProp { private int val; //private member public int no //property body implemented in the class { get { val += 2; return val; } set ...
More About: Interface , Properties , Pert , Rope
Using explicit implementation to remove ambiguity
2007-06-14 10:35:00
When to use an explicitly implemented interface?In my earlier posts I mentioned its better to use explicit implementation over implicit. The reason, well I have an example mentioned.class Program { static void Main(string[] args) { number n1 = new number(); //Ambiguity rises here, do u know whose cal() is it?!? Console.WriteLine( n1.cal(10)); Console.Read(); } interface IAdd { int cal(int x);//cal method }   interface IMul { int cal(int x);//cal method, same signature }   class number : IAdd, IMul //Uses both interfaces. { public int cal(int x) //Which interface's method gets called? { return x * x; }   }...
More About: Interface , Implementation
More articles from this author:
1, 2, 3, 4
51420 blogs in the directory.
Statistics resets every week.


Contact | About
© Blog Toplist 2008 - Supported by Web Catalog - SEO by FeWorks
eXTReMe Tracker